What is the difference between RemoteWebDriver and WebDriver?
Asked Answered
I

1

6

I actually couldn't find a good explanation of whats the difference between RemoteWebDriver and a WebDriver in Selenium.

Here's a code where eclipse told me to cast WebDriver to RemoteWebDriver.

(!((RemoteWebDriver) driver).getSessionId().toString().contains("null"))

So why shouldn't I just use RemoteWebDriver instead of WebDriver?

Interpret answered 16/8, 2019 at 17:42 Comment(2)
remote is for running the driver using browsers on other machines.Incorruptible
Check Java docs seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/… and seleniumhq.github.io/selenium/docs/api/java. e.g. Search Context Interface -> Webdriver Interface -> RemoteWebdriver Class -> Chromium driver Class -> Chrome driver ClassDarrick
A
14

RemoteWebDriver is a concrete class that implements interface WebDriver.

RemoteWebDriver class contains additional methods that are not declared by interface WebDriver. The method 'getSessionId()' is one of them.

Hence, your object needed to be explicitly downcasted to use getSessionId method since WebDriver itself does not have knowledge of any method or variable which is purely defined by RemoteWebDriver.

Coming to the question - "why shouldn't I just use RemoteWebDriver instead of WebDriver?"

Yes you may use RemoteWebDriver instead of WebDriver, however it makes the code non-compliant with the design principle - 'Code to the interface'

Your code will work fine though without any issues.

However, it will not have flexibility to use other driver implementations that may come in future (though very unlikely) which implements WebDriver but does not extend RemoteWebdriver. In such case, a variable of type RemoteWebDriver cannot be assigned to an object of the class is which WebDriver's implementation but not extending RemoteWebDriver

Annorah answered 18/8, 2019 at 9:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.