Why Selenium WebDriver is an Interface?

jagadeeshshetty

Jagadeesh

Posted on February 17, 2021

Why Selenium WebDriver is an Interface?

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

  • WebDriver interface contains commonly used methods for browser automation.

Ex:

WebDriver Methods

Return Type Methods
void close()
WebElement findElementโ€‹(By by)
java.util.List 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();

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");
        }
    }

}
Enter fullscreen mode Exit fullscreen mode
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)
Enter fullscreen mode Exit fullscreen mode

SUMMARY
WebDriver is an Interface.
ChromeDriver, FirefoxDriver etc classes implemented WebDriver interface.
WebDriver has common methods for all browsers. Ex: get(), getTitle(), findElement() etc
๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
jagadeeshshetty
Jagadeesh

Posted on February 17, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

ยฉ TheLazy.dev

About