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 FirefoxWebDriver driver = new EdgeDriver();for Microsoft Edge
This approach ensures our automation code remains flexible and easy to switch between different browsers with minimal changes.