Georgi Georgiev
Georgi Georgiev

Reputation: 1623

WebDriver nonsense

I am not an OOP guru and maybe there is something deeper that I am not getting, but here is what bothers me a lot:

I don't see any reason why ALL examples and tutorials for Selenium WebDriver are using WebDriver objects, example:

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

In the example above, we need to add more code for casting the WebDriver to TakesScreenshot. Why do this when we can just use the FirefoxDriver object directly:

FirefoxDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = driver.getScreenshotAs(OutputType.FILE);

All driver objects(except for GhostDriver) implement ALL WebDriver methods + eventually some more in addition. So, basically I can use FirefoxDriver or ChromeDriver ANYWHERE a WebDriver objects is needed, because they implement the WebDriver interface indirectly(all drivers inherit from RemoteDriver, which implements the WebDriver interface), right?

So, why not just use the 'real' drivers instead of creating them as WebDriver and then complicate the code with castings and so on?

Can you give me any practical example when it is better to use WebDriver instead of FirefoxDriver(for example)?

Upvotes: 0

Views: 104

Answers (1)

Sandipan Pramanik
Sandipan Pramanik

Reputation: 348

@CuriousGuy, here is an example here you need to instantiate driver object based on the configuration ( passes as parameter) on testNG suite to test the UAT in different browser. TestNG suite

<suite parallel="test" Thread-count=2>
<test>
    <parameter name="browser" value="FF"/>
    <classes>
        <class name="package.MyClassname"/>
    </classes>
</test>
<test>
    <parameter name="browser" value="Chrome"/>
    <classes>
        <class name="package.MyClassname1"/>
    </classes>
</test>

We are selecting the browser object dynamically ( polymorphism concept of OOP )

class TestInit{
WebDriver driver;
@BeforeClass
@Parameters({"browser"})
public void intBowser(String browser){
    switch(browser){
        case "FF" :
            driver = new FireforDriver();
            break;
        case "Chrome":
            System.setProperty("webdriver.chrone.driver","path to your chromeDriver.exe");
            driver = new ChromeDriver();
            break;
        default:
            Logger.log("No available Driver");
    }
    driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.navigate().to("http://www.seleniumhq.org/");
}
@AfterClass
public void cleanup(){
    driver.close();
    driver.quit();
}
}

class Myclassname extends TestInit {
@Test
public void yourTestMethod(){
    // Dynamically selected driver object will be available here. 
}
}

Hope that, WebDriver is not nonsense, it makes full sense :) in practical use.

Upvotes: 1

Related Questions