Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we do not write RemoteWebdriver driver = new ChromeDriver();

this question popped up in my mind that why we do not write RemoteWebdriver driver = new ChromeDriver(); what is the harm what are the advantages why we do not write it like this while creating our driver instance.

We do write like:

Webdriver driver = new ChromeDriver(); 
ChromeDriver driver = new ChromeDriver();

but not like this:

RemoteWebdriver driver = new ChromeDriver(): 

I am new to this so any help will be appreciated.

Thanks

like image 735
sanjay Avatar asked Dec 18 '25 09:12

sanjay


1 Answers

When initializing ChromeDriver and assigning it to a variable whose type is ChromeDriver, you are able to use all of the concrete methods available to that type. This is useful if you need to configure the Chrome browser window:

ChromeDriver driver = new ChromeDriver();

// Configure Google Chrome

WebDriver is an interface. ChromeDriver is a concrete class which implements the WebDriver interface. When initializing a WebDriver variable to a new instance of ChromeDriver you are simply providing concrete implementation for the WebDriver abstraction. This is done when you want to test in Chrome, but you do not need Chrome specific configurations, and the methods provided by the WebDriver interface suffice for your tests.

WebDriver driver = new ChromeDriver();

// Use the WebDriver abstraction without needing to know it is Chrome

The RemoteWebDriver class is the abstract class from which ChromeDriver is derived. It has some concrete implementation related to making web service calls to a "web driver" process running on the same computer. It also contains abstractions that concrete classes like ChromeDriver must implement.

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 driver = new ChromeDriver();

// test code tends to only call methods defined in the WebDriver interface,
// so why bother with a concrete class?
like image 176
Greg Burghardt Avatar answered Dec 20 '25 01:12

Greg Burghardt