Why do we need to call WebDriver driver = new ChromeDriver()

We typically use WebDriver driver = new ChromeDriver(); to launch the Chrome browser. While it is also valid to use ChromeDriver driver = new ChromeDriver();, using the WebDriver interface as the reference type—i.e., WebDriver driver = new ChromeDriver();—is preferred for cross-browser compatibility and to ensure access to WebDriver methods in a consistent way.

This works because an interface in Java can be used to reference an object of any class that implements it. Although we cannot instantiate an interface directly, we can initialize an interface reference with an object of a class that implements it, such as ChromeDriver.

Similarly, we can launch other browsers using:

  • WebDriver driver = new FirefoxDriver(); for Mozilla Firefox
  • WebDriver driver = new EdgeDriver(); for Microsoft Edge

This approach ensures our automation code remains flexible and easy to switch between different browsers with minimal changes.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Subscribe to our YouTube Channel: Testing Talks Latest
Testingtalkslatest.com - A project by CreativeHub IT Solutions.
Contact Us At: support@testingtalkslatest.com
Our Partner websites - Classified Hub , CodesToolbox
Scroll to Top
0
Would love your thoughts, please comment.x
()
x