FirefoxDriver driver = new FirefoxDriver();
If you look at the code for Selenium you will see they have an interface called WebDriver. FirefoxDriver, InternetExplorerDriver, HtmlUnitDriver, RemoteWebDriver all implement WebDriver.
If I'm writing a test suite, I ultimately want to run the suite on Firefox, Internet Explorer, Chrome, etc. but if I implement my framework using FirefoxDriver, I have to edit the code to make it work on InternetExplorerDriver. The proper solution is to write your code using:
WebDriver driver = new FirefoxDriver();
You might argue that I need to change my code still. Okay, so you would implement it using:
WebDriver driver; if(browserType.equals("firefox")) { driver = new FirefoxDriver(); } else if(browserType.equals("ie")) { driver = new InternetExplorerDriver(); } else { driver = new RemoteWebDriver(); }
Now all subsequent code will use driver as if it was merely a WebDriver. However, if you look at RemoteWebDriver you will see that it also implements JavascriptExecutor. So if I wanted to use:
driver.executeScript(script, args);
I'll get an error because WebDriver does not support executeScript. My driver is REALLY a RemoteWebDriver and does support executeScript. So how do I access that functionality? Quite simple:
((RemoteWebDriver)driver).executeScript(script, args);
Even better would be:
if(driver instanceof RemoteWebDriver) { ((RemoteWebDriver)driver).executeScript(script, args); } else if(driver instanceof FirefoxDriver) { ((FirefoxDriver)driver).executeScript(script, args); } else if(driver instanceof InternetExplorerDriver) { ((InternetExplorerDriver)driver).executeScript(script, args); }
Now if you run the test suite against a browser that does not support a particular feature, it will skip that part of the code automatically.
And that is proper use if polymorphism.