WebDriver API and its hierarchy

What is SearchContext?

It is an interface and it has two methods : findElement() and findElements().

It has two child interfaces : WebDriver and WebElement.

What is WebElement?

It is an interface and it has several methods that help performing certain actions on elements on the web page. Every element on a web page is an WebElement in Selenium.

What is WebDriver?

It is an interface. Selenium has three components, namely - WebDriver, IDE and Grid. WebDriver is the one that drives the browser. It has methods to manipulate the browser components.

It has child classes, such as : ChromeDriver, ChromiumDriver, EdgeDriver, EventFiringWebDriver, FirefoxDriver, InternetExplorerDriver, RemoteWebDriver, SafariDriver.

RemoteWebDriver : It is a class, that is used to launch the browser remotely.

ChromiumDriver : It is a class, it has extends RemoteWebDriver class. It has two child classes - ChromeDriver and EdgeDriver.

Why "WebDriver driver = new ChromeDriver()" ?

Why do we write : WebDriver driver = new ChromeDriver() ; (or FirefoxDriver(), EdgeDriver() etc), but not any other combinations? Let's try out some combinations from the above hierarchy: (we will use ChromeDriver for examples purpose)

SearchContext driver01 = new ChromeDriver();
SearchContext driver02 = new RemoteWebDriver();

In this case, driver will have only two methods : findElement() and findElements() coming from SearchContext. Not recommended.

ChromiumDriver driver = new ChromeDriver();

Here, only ChromiumDriver specific methods will be available to driver. Cross browser testing won't be possible. Not recommended.

ChromeDriver driver = new ChromeDriver();

Here, only ChromeDriver specific methods will be available to driver. Cross browser testing won't be possible.

RemoteWebDriver driver = new ChromeDriver();

RemoteWebDriver has implementations of all WebDriver methods. It is a valid top casting.

WebDriver driver01 = new RemoteWebDriver("remoteAddress",capabilities);
WebDriver driver02 = new ChromeDriver();

Here, driver will have implementations of all WebDriver methods. The first one will launch browser remotely. It is a valid top casting. The second one will launch the browser natively.