In this post, we will study the Selenium WebDriver commands used to launch browsers in detail. We will also learn the different additional customization required for launching certain browsers like - Chrome and InternetExplorer.
As we have studied in previous tutorials that Selenium WebDriver calls the native methods of the different browsers to automate them. Hence, in Selenium we have different WebDrivers for different browsers like - FirefoxDriver for Firefox browser, ChromeDriver for Google Chrome, InternetExplorerDriver for Internet Explorer etc. Now let's take an example of launching Firefox browser and understand the command in detail-
This is the java implementation of launching a browser in Selenium. Here, 'WebDriver' is an interface and we are creating a reference variable 'driver' of type WebDriver, instantiated using 'FireFoxDriver' class.
For those who are not very proficient in Java, an interface is like a contract that classes implementing it must follow. An interface contains a set of variables and methods without any body(no implementaion, only method name and signature). We cannot instantiate objects from interfaces. Hence, the below line of code is incorrect and throws compile time error saying "Cannot instantiate the type WebDriver".
For instantiation of driver object, we need classes like FirefoxDriver or ChromeDriver which have implemented the WebDriver interface. In other words, these driver classes have followed the contract of WebDriver by implementing all the methods of the WebDriver interface. Thus making all the different types of driver classes uniform, following the same protocol.
You may ask certain questions here - "Why do we need to create a reference variable of type 'WebDriver'?", "Why can't we directly create an object of a particular driver class?", "Is there anything wrong with creating driver object of type FirefoxDriver directly?"-
The answer is, the above statement is perfectly correct. We can create objects of a particular browser's driver and use it throughout our automation. But having a driver object referencing to 'WebDriver' interface helps in cross browser testing. We can use the same driver object for different browsers later, instantiating it with some other desired driver class.
Firefox is one of the most widely used browsers in automation. It doesn't require any additional customization or setting of property. The below code snippet will launch the Firefox browser and open a webpage-
For running Chrome browser in Selenium, we need to set the webdriver.chrome.driver system property to point to a chromeDriver executable file-
Code snippet to launch Chrome browser-
Like ChromeDriver, InternetExplore driver also requires setting up the "webdriver.ie.driver" property with the location of IEDriverServer.exe. The IEDriverServer.exe can be downloaded from here. Following code snippet can be used to launch IE browser-
Like Firefox browser, the Safari browser doesn't require any additional configuration and can be directly launched by instantiating with SafariDriver. The following code snippet can be used to launch the Safari browser-