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?
Selenium RemoteWebDriver is used to execute the browser automation suite on a remote machine. In other words, RemoteWebDriver is a class that implements the WebDriver interface on the remote server. The browser driver classes like FirefoxDriver, ChromeDriver, InternetExplorerDriver, etc.
Anything that is present on the web page is a WebElement such as text box, button, etc. WebElement represents an HTML element. Selenium WebDriver encapsulates a simple form element as an object of the WebElement. It basically represents a DOM element and all the HTML documents are made up by these HTML elements.
Selenium IDE is for less-technical testers to create a visual, grid-like example of what they want to test. WebDriver should be used for more complex tests that need to loop, perform setup or interact with external systems.
The reason you do not often see a new ChromeDriver being assigned to a RemoteWebDriver variable is because from the perspective of a test, there is no functional difference between a WebDriver object and a RemoteWebDriver object. The level of abstraction provided by RemoteWebDriver is simply not very useful.
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 ofWebDriver
?"
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With