Why Selenium WebDriver is an Interface?
Tags: testing, java
Published: 2021-02-17 12:24:57

### [_WEBDRIVER INTERFACE_]()

| _KEYWORDS / QUESTIONS_                                                             |
| :--------------------------------------------------------------------------------- |
| `Interface WebDriver` `ChromeDriver` `ChromeDriverService`                         |
| `System.setPropert(key, value)`                                                    |
| `chromedriver` `geckodriver` `msedgedriver` `phantomjs`                            |
| `WebDriver driver;` `driver = new ChromeDriver();` `driver = new FirefoxDriver();` |

---

### _NOTES_

> All browser specific classes (_ChromeDriver_, _FirefoxDriver_ etc) implemented a common interface called _WebDriver_ interface. [info](https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/WebDriver.html)

- WebDriver interface contains commonly used methods for browser automation.

  Ex:

  _WebDriver Methods_

| _Return Type_              | _Methods_                  |
| :------------------------- | :------------------------- |
| void                       | close()                    |
| WebElement                 | findElement​(By by)        |
| java.util.List<WebElement> | findElements​(By by)       |
| void                       | get​(java.lang.String url) |
| java.lang.String           | getTitle()                 |
| tbd                        | tbd                        |

- We can create different browser object using same object type WebDriver.

  Ex:

  `WebDriver driver;`

  `driver = new ChromeDriver();`

  `driver = new FirefoxDriver();`

```java
package io.webdriver.chrome;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeDriverService;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.GeckoDriverService;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;

import java.io.File;

public class WebDriverInterface {
    public static void main(String[] args) {
        WebDriver driver;

        System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, getDriver("chromedriver").getAbsolutePath());
        System.setProperty(GeckoDriverService.GECKO_DRIVER_EXE_PROPERTY, getDriver("geckodriver").getAbsolutePath());
        System.setProperty(EdgeDriverService.EDGE_DRIVER_EXE_PROPERTY, getDriver("msedgedriver").getAbsolutePath());
        System.setProperty(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, getDriver("phantomjs").getAbsolutePath());


        driver = new ChromeDriver();
        System.out.println(driver.toString());
        driver.quit();

        driver = new FirefoxDriver();
        System.out.println(driver.toString());
        driver.quit();

        driver = new EdgeDriver();
        System.out.println(driver.toString());
        driver.quit();

        driver = new PhantomJSDriver();
        System.out.println(driver.toString());
        driver.quit();
    }

    /**
     * Based on the OS, we get browser driver location.
     *
     * @param driverName a string. ex: "chromedriver"
     * @return the relative (partial) path of the driver. ex: driver/chromedriver
     */
    private static File getDriver(String driverName) {
        if (System.getProperty("os.name").contains("Mac OS X")) {
            return new File(new File("driver"), driverName);
        } else {
            return new File(new File("driver"), driverName + ".exe");
        }
    }

}
```

```shell
ChromeDriver: chrome on WINDOWS (24a35fb050dc5e8d46ba8c0eede0eb1f)
FirefoxDriver: firefox on WINDOWS (413c13b1-7984-41e4-a7cc-a31b85438e71)
EdgeDriver: msedge on WINDOWS (83af823aa10d801182ad9726e3ed911b)
PhantomJSDriver: phantomjs on XP (01d457b0-7114-11eb-a16e-d3a30713c1eb)
```

***

| _SUMMARY_                                                                               |
| :-------------------------------------------------------------------------------------- |
| WebDriver is an Interface.                                                              |
| ChromeDriver, FirefoxDriver etc classes implemented WebDriver interface.                |
| WebDriver has common methods for all browsers. Ex: get(), getTitle(), findElement() etc |