Selenium - How to initialize Chrome and Firefox WebDriver
Selenium - How to initialize Chrome and Firefox WebDriver:
What is WebDriver:
Its important we understand what is WebDriver, before jumping to use it to initialize the browsers.
WebDriver API was introduced with Selenium 2.0 and was the primary feature of it. WebDriver is designed to provide a simpler, more concise programming interface, overcome the limitations of Selenium RC (such as Single Host origin policy) and provide better support for modern advanced web applications. It was developed to better support dynamic web pages where elements of a page may change without the page itself being reloaded.
The way Selenium RC worked was injecting javascript functions into the browser when the browser was loaded and then used its javascript to execute the commands in the browser. WebDriver makes direct calls to the browser and drives the browser directly using the browser’s built in support for automation
One of the most common interview question is whether WebDriver is a class or interface? WebDriver is an interface against which tests should be written. The implementing classes are ChromeDriver, FirefoxDriver, RemoteWebDriver etc.
For running automation in chrome browser we need chromedriver. ChromeDriver, developed by Chromium team, is a standalone server which implements WebDriver's wire protocol for Chromium. This is needed to by selenium driver to interact and send commands to chrome browser. You can download the latest version of chromedriver for your operating system from here.
Coming to Firefox, we didn't need any driver file for firefox until Selenium 2 version, but with Selenium 3, we don't have native Firefox browser support. As you might be aware, Firefox runs on Gecko browser engine developed by Mozilla foundation and the company has released the GeckoDriver which can be used to interact with Gecko based browsers such as Firefox. You can download the latest version of geckodriver for your operating system from here.
Once you have downloaded the driver files for the browsers, make sure they are executable.
If you are on linux, you can use the below command to make it executable.
sudo chmod +x ~/Downloads/chromedriver
sudo chmod +x ~/Downloads/geckodriver
Now that you understand the need of the driver file and have downloaded it to your machine, lets move to the coding part of it.
First and foremost you need to set the webdriver.chrome.driver path or webdriver.gecko.driver path to the path of the corresponding driver file you downloaded.
Next you need to create and set the Desired Capabilities with the values you deem necessary for your application. This is an optional step.
Once you have your capabilities ready you can then instantiate the WebDriver for the required browser.
Below is a code snippet for the same. You can add or remove the capabilities as per your requirement. The code is also available in my github repository at this location. Feel free to download the project and try running in your local. Let me know if you face any issues.
What is WebDriver:
Its important we understand what is WebDriver, before jumping to use it to initialize the browsers.
WebDriver API was introduced with Selenium 2.0 and was the primary feature of it. WebDriver is designed to provide a simpler, more concise programming interface, overcome the limitations of Selenium RC (such as Single Host origin policy) and provide better support for modern advanced web applications. It was developed to better support dynamic web pages where elements of a page may change without the page itself being reloaded.
The way Selenium RC worked was injecting javascript functions into the browser when the browser was loaded and then used its javascript to execute the commands in the browser. WebDriver makes direct calls to the browser and drives the browser directly using the browser’s built in support for automation
One of the most common interview question is whether WebDriver is a class or interface? WebDriver is an interface against which tests should be written. The implementing classes are ChromeDriver, FirefoxDriver, RemoteWebDriver etc.
For running automation in chrome browser we need chromedriver. ChromeDriver, developed by Chromium team, is a standalone server which implements WebDriver's wire protocol for Chromium. This is needed to by selenium driver to interact and send commands to chrome browser. You can download the latest version of chromedriver for your operating system from here.
Coming to Firefox, we didn't need any driver file for firefox until Selenium 2 version, but with Selenium 3, we don't have native Firefox browser support. As you might be aware, Firefox runs on Gecko browser engine developed by Mozilla foundation and the company has released the GeckoDriver which can be used to interact with Gecko based browsers such as Firefox. You can download the latest version of geckodriver for your operating system from here.
Once you have downloaded the driver files for the browsers, make sure they are executable.
If you are on linux, you can use the below command to make it executable.
sudo chmod +x ~/Downloads/chromedriver
sudo chmod +x ~/Downloads/geckodriver
Now that you understand the need of the driver file and have downloaded it to your machine, lets move to the coding part of it.
First and foremost you need to set the webdriver.chrome.driver path or webdriver.gecko.driver path to the path of the corresponding driver file you downloaded.
Next you need to create and set the Desired Capabilities with the values you deem necessary for your application. This is an optional step.
Once you have your capabilities ready you can then instantiate the WebDriver for the required browser.
Below is a code snippet for the same. You can add or remove the capabilities as per your requirement. The code is also available in my github repository at this location. Feel free to download the project and try running in your local. Let me know if you face any issues.
public static WebDriver initializeLocalWebDriver(BrowserName browserName, String driverPath) { WebDriver driver = null; DesiredCapabilities capabilities = new DesiredCapabilities(); switch (browserName) { case FIREFOX: System.setProperty("webdriver.gecko.driver", driverPath + "geckodriver"); FirefoxProfile profile = new FirefoxProfile(); profile.setPreference("browser.link.open_newwindow.restriction", 0); //To open new tab instead of new window on clicking on any link. capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); capabilities.setCapability(FirefoxDriver.PROFILE, profile); capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.DISMISS); driver = new FirefoxDriver(capabilities); break; case CHROME: System.setProperty("webdriver.chrome.driver", driverPath + "chromedriver"); capabilities = DesiredCapabilities.chrome(); final ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.addArguments("enable-automation"); chromeOptions.addArguments("--disable-infobars"); chromeOptions.addArguments("--disable-extensions"); chromeOptions.addArguments("--start-maximized"); Map prefs = new HashMap<String, Object>(); prefs.put("profile.default_content_setting_values.geolocation", 2); // 1:allow 2:block To Block Geo Location chromeOptions.setExperimentalOption("prefs", prefs); capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions); capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.DISMISS); driver = new ChromeDriver(capabilities); break; default: System.out.println("Driver Factory not implemented for " + browserName + " browser"); System.exit(1); } driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); return driver; }
Comments
Post a Comment