Initiating Browser in Selenium WebDriver

Main Download page for all drivers: http://www.seleniumhq.org/download/

How to initiate Firefox browser:


Since Selenium 3 has launched, Firefox browser initialization became the most common problem among the Selenium testers. Selenium 3 does not support default Firefox Driver. Going forward Marionette is supported in lieu of Firefox. It is the next generation of FirefoxDriver. In this article I will cover how to use Gecko Driver or Marionette Driver to initialize Firefox browser in Selenium.

What is GeckoDriver?

Gecko is the name of the layout engine developed by the Mozilla Project. It was originally named NGLayout. Gecko's function is to read web content, such as HTML, CSS, XUL, JavaScript, and render it on the user's screen or print it. In XUL-based applications Gecko is used to render the application's user interface as well. Read more here.

What is Marionette?

Marionette is an automation driver for Mozilla's Gecko engine. It can remotely control either the UI or the internal JavaScript of a Gecko platform, such as Firefox. It can control both the chrome (i.e. menus and functions) or the content (the webpage loaded inside the browsing context), giving a high level of control and ability to replicate user actions. In addition to performing actions on the browser, Marionette can also read the properties and attributes of the DOM. Read more here.

This is how we initiate Firefox browser:
  1. Download the latest GeckoDriver from here.
  2. Set the DesiredCapabilities
  3. Set FirefoxDriver as WebDriver driver
Sample Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
String ffd = "path to geckodriver.exe";
//System.setProperty("webdriver.gecko.driver", ffd);
System.setProperty("webdriver.firefox.marionette", ffd);
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.startup.homepage_override.mstone", "ignore");
profile.setPreference("startup.homepage_welcome_url.additional",  "about:blank");
capabilities.setCapability("marionette", true);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);

driver = new FirefoxDriver(capabilities);


How to initiate IE browser:


Running tests in IE Explorer is quite easy. Internet Explorer cannot be launched directly, we have to communicate with the Internet explorer via Internet explorer driver. Internet explorer driver server is the link between your tests in Selenium and the Internet explorer browser. As selenium WebDriver has no native implementation of IE, we have to direct all the driver commands through IE driver server. IE driver server is an executable file that you need to have in one of the system path before starting your tests.

This is how we initiate IE browser:
  1. Download the latest Internet Explorer Driver Server from here. Just choose the version based on whether you are on the 32 bit or a 64 bit operating system and download.
  2. Set the DesiredCapabilities
  3. Set InternetExplorerDriver as WebDriver driver
Sample Code:

1
2
3
String ied = "path to IEDriverServer.exe";
System.setProperty("webdriver.ie.driver", ied);
driver=new InternetExplorerDriver();


How to initiate Edge browser:


To use WebDriver with Microsoft Edge, you'll need to do the following: (as mentioned on the Microsoft site)
  1. Install Windows 10.
  2. Download the correct Microsoft WebDriver server version for your build.
  3. How to find your correct build number: Go to Start > Settings > System > About and locate the number next to OS Build on the screen. This is your build number (14393.222). Having the correct version of WebDriver for your build ensures it runs correctly.
  4. Download a WebDriver language binding of your choice. Currently C#, Java Selenium, and JavaScript language bindings are supported.
  5. Download a testing framework of your choice.

Sample Code:


1
2
3
String edged = "path to MicrosoftWebDriver.exe";
System.setProperty("webdriver.edge.driver", edged);
driver= new EdgeDriver();


How to initiate Chrome browser:


Chrome browser implements the WebDriver protocol using an executable called ChromeDriver.exe. This executable starts a server on your system. All your tests communicate to this server to run your tests.

This is how we initiate Chrome browser:
  1. Download the latest Chrome Driver Server from the Chromium project here.
  2. Set the DesiredCapabilities
  3. Set ChromDriver as WebDriver driver
Sample Code:


1
2
3
String cd = "path to chromedriver.exe";
System.setProperty("webdriver.chrome.driver", cd);
driver=new ChromeDriver();


You can see the re-usable code for initializing web browsers here.

No comments:

Post a Comment