Skip to content

WebDriver driver = new ChromeDriver() – Why we write in Selenium Scripts #5

@anhhanuman

Description

@anhhanuman

https://www.tutorialspoint.com/can-we-cast-an-object-reference-to-an-interface-reference-in-java-if-so-when
FirefoxDriver driver = new FirefoxDriver();

The FirefoxDriver instance which gets created based on above statement will be only able to invoke and act on the methods implemented by FirefoxDriver and supported by Firefox Browser only. We know that FirefoxDriver is a class and it implements all the methods of WebDriver interface. Using this statement, we can run our scripts only on Firefox Browser.

To act with other browsers we have to specifically create individual objects as below:

ChromeDriver driver = new ChromeDriver();

InternetExplorerDriver driver = new InternetExplorerDriver();

We don’t just run our scripts only on single browser. We use multiple browsers for Cross Browser Compatibility. We need the flexibility to use other browsers like ChromeDriver() to run on Chrome Browser and InternetExplorerDriver() to run on IE Browser and so on.

So, once you initiate a Firefox browser using FirefoxDriver driver = new FirefoxDriver(); same object cannot be used to initiate Chrome Browser (you have to rename it)

ChromeDriver driver = new ChromeDriver();

To solve this we use “Webdriver driver = new FirefoxDriver();”

Lets see this now.

WebDriver driver = new FirefoxDriver();

We can create Object of a class FirefoxDriver by taking reference of an interface (WebDriver). In this case, we can call implemented methods of WebDriver interface.

As per the above statement, we are creating an instance of the WebDriver interface and casting it to FirefoxDriver Class. All other Browser Drivers like ChromeDriver, InternetExplorerDriver, PhantomJSDriver, SafariDriver etc implemented the WebDriver interface (actually the RemoteWebDriver class implements WebDriver Interface and the Browser Drivers extends RemoteWebDriver). Based on this statement, you can assign Firefox driver and run the script in Firefox browser (any browser depends on your choice).

We will see RemoteWebDriver in later section below.

If you define driver as a WebDriver, switching will be very easy. If we use this statement in our script then the WebDriver driver can implement any browser. Every browser driver class implements WebDriver interface and we can get all the methods. It helps you when you do testing on multiple browsers.

Example:

WebDriver driver = new FirefoxDriver();
driver.quit();
driver = new ChromeDriver();

WebDriver is an interface and all the methods which are declared in Webdriver interface are implemented by respective driver class. But if we do upcasting,we can run the scripts in any browser .i.e running the same automation scripts in different browsers to achieve Runtime Polymorphism.

https://www.softwaretestingmaterial.com/webdriver-driver-new-firefoxdriver/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions