Upgrading to Selenium 3 with Gecko Driver

Table of contents
Reading Time: 2 minutes

In this blog I will be discussing about the latest version of selenium i.e Selenium 3 . To use selenium 3 , we need Gecko driver to run the test cases in Mozilla browser.

So,the first question that arises in our mind is “What is Gecko?” 

Gecko is the name of the layout engine developed by the Mozilla Project. … 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.

ThWebDriver protocol is implemented by Firefox using an executable called GeckoDriver.exe . It starts a server on your system. All your tests communicate to this server to run the tests. It acts as a proxy between the local and remote ends and translates calls into the Marionette automation protocol. To use Marionette or Firefox with Selenium 3 all you need is:

  1. Install geckodriver.exe on your system.
  2. Add path of geckodriver.exe in your code.
  3. Use Firefox in your code.

Currently I am using Firefox 49.0 & Ubuntu 16.04 LTS

Here is the sample code:

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

/**
 * Created by swati on 18/10/16.
 */

public class FirstTest {

    public static void main(String [] args){

        System.setProperty("webdriver.gecko.driver","/home/sid/Downloads/geckodriver");
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        WebDriver driver = new FirefoxDriver(capabilities);
        String url = "http://www.amazon.in/";
        driver.get(url);
        System.out.println("Successfully opened the website www.amazon.com");
        try {
            Thread.sleep(5);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String title = driver.getTitle();
        int titleLength = driver.getTitle().length();
        System.out.println("Title of the page is : " + title);
        System.out.println("Length of the title is : "+ titleLength);
        String actualUrl = driver.getCurrentUrl();
        if (actualUrl.equals(url)){
            System.out.println("Verification Successful - The correct Url is opened.");
        }else{
            System.out.println("Verification Failed - An incorrect Url is opened.");
            System.out.println("Actual URL is : " + actualUrl);
            System.out.println("Expected URL is : " + url);
        }
        String pageSource = driver.getPageSource();
        int pageSourceLength = pageSource.length();
        System.out.println("Total length of the Page Source is : " + pageSourceLength);
        driver.close();
    }
}

Enjoy 🙂

You can find a demo project using selenium 3 with gecko driver on my repo : Selenium3withGeckoDriver

Reference:

https://github.com/mozilla/geckodriver

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading