bhargav desai
bhargav desai

Reputation: 119

Object Instantiation - Selenium Web Driver

This is a common question, but i am not clear on the answers I found on the internet or StackOverflow.

When we instantiate an object in selenium WebDriver (say selenium webdriver and Java), we generally write (as a normal practice),

WebDriver driver = new FirefoxDriver(); 

Or use any other implementation of WebDriver interface (Chrome, IE, Safari, AndroidDriver, etc).

Why we don't use something like below

FirefoxDriver fx = new FirefoxDriver();

WebDriver being an interface, only the methods of WebDriver that are implemented by the specific browser implementation class would be accessible. The methods of the, say FireFoxDriver, that are not in WebDriver interface would not be accessible by the reference.

Please do correct me if I'm wrong.

Upvotes: 1

Views: 4442

Answers (2)

pleft
pleft

Reputation: 7905

You have to use the interface WebDriver instead of the implementation. This is a general technique named:

code/programming to the interface

so if in the future you ever need to switch to a different driver, let's say switch from ChromeDriver to FirefoxDriver your code will remain intact.

Upvotes: 1

Void
Void

Reputation: 83

You always need to call the WebDriver as it contains all the methods you need for testing.

Here is the example:

protected static WebDriver driver = new ChromeDriver();

or

protected static WebDriver driver = new FirefoxDriver();

Upvotes: 2

Related Questions