Thursday 9 February 2017

SELENIUM: How to launch Firefox browser in selenium 3?



How to launch Firefox browser in selenium 3?
------------------------------------------------------------


Gecko Driver is the link between your tests in Selenium and the Firefox browser.
GeckoDriver is a proxy for using W3Consortium WebDriver-compatible clients to interact with Gecko-based browsers i.e. Mozilla Firefox in this case.
As Selenium 3 will not have any native implementation of Firefox, we have to direct all the driver commands through Gecko Driver. Gecko Driver is an executable file that you need to have in one of the system path before starting your tests.
 Firefox browser implements the WebDriver protocol using an executable called GeckoDriver.exe. This executable starts a server on your system. All your tests communicate to this server to run your tests.
It translates calls into the Marionette automation protocol by acting as a proxy between the local and remote ends.
Geckodriver provides HTTP API described by the WebDriver protocol to communicate with Gecko browsers, such as Firefox (Version after 47).
Marionette (the next generation of FirefoxDriver) is turned on by default from Selenium 3. Even if you are working with older versions of Firefox browser, Selenium 3 expects you to set path to the driver executable by the webdriver.gecko.driver.

How to use GeckoDriver to launch Firefox?
You will first need to download GeckoDriver and then set its path. There are three different ways to use GeckoDriver with Selenium 3:
1.     With setting system properties in the test
2.     With setting system properties by Environment Variable
3.     With setting up Browser Desired Capabilities

 With setting system properties in the test:
1.First you need to download the GeckoDriver.exe file.
Download the file from https://github.com/mozilla/geckodriver/releases
2.write System.setProperty(String key.String path);
          Ex:System.setProperty("webdriver.gecko.driver",  "C:\\Users\\ramesh\\Desktop\\java\\desk\\geckodriver-v0.11.1-win64\\geckodriver.exe");
      3.And now launch the firefox browser.

          WebDriver driver=new FirefoxDriver();

If you are not writing System.setProperty(-,-),so, it will throw exception "java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property;"
Note: The other important changes in Selenium 3.x are listed below:
·        Java version should be 8 and earlier
·        Support for Firefox is via Mozilla’s geckodriver.
·        Only versions 9 or above of IE are supported..
·        Use FF version 46 and earlier.


Example:
package com.rameshsoft.rameshselenium;

import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class ThreadPoolSize {
         
          @BeforeMethod
          public void before()
          {
                   System.out.println("Before test running");
          }
          @AfterMethod
          public void after()
          {
                   System.out.println("After test running");
          }
         
          @Test()
          public void execute() throws InterruptedException
          {                  
System.out.println("Running Thread id is: "+thread_id);
                   System.setProperty("webdriver.gecko.driver", "C:\\Users\\ramesh\\Desktop\\java\\desk\\geckodriver-v0.11.1-win64\\geckodriver.exe");
                   WebDriver driver=new FirefoxDriver();
                   driver.manage().window().maximize();                   driver.manage().timeouts().implicitlyWait(50,TimeUnit.SECONDS);                  
          driver.get("https://www.gmail.com");                                      driver.findElement(By.id("Email")).sendKeys("rameshsoft.selenium");                  
                   driver.findElement(By.id("next")).click();
                   Thread.sleep(2000);
                   driver.findElement(By.id("Passwd")).sendKeys("12345");
                   driver.findElement(By.id("signIn")).click();
                  
                   driver.quit();
          }

}

No comments:

Post a Comment