Thursday, December 8, 2016

Selenium 3 quickStart

                    UI Automation using Selenium 3


Whats in Selenium 3


  • Java 8 Support
  • With Selenium RC APIs removed . So no backward compatibility .
  • Support for Firefox is via Mozilla’s geckodriver
  • Support for for Safari is provided on macOS (Sierra or later) via Apple’s own safaridriver
  • Support for Edge is provided by Microsoft through their webdriver server
  • Only Internet Explorer 9 and above versions are supported. Earlier versions may work, but are no longer supported



What is GeckoDriver?

Gecko is a web browser engine used in many applications developed by Mozilla Foundation and the Mozilla Corporation, most noticeably the Firefox web browser, its mobile version other than iOS devices, their email client Thunderbird and many other open source software projects. You can get more information about Gecko here – https://en.wikipedia.org/wiki/Gecko_(software)

GeckoDriver is a proxy for using W3C WebDriver-compatible clients to interact with Gecko-based browsers i.e. Mozilla Firefox in this case. This program provides the HTTP API described by the WebDriver protocol to communicate with Gecko browsers. It translates calls into the Marionette automation protocol by acting as a proxy between the local and remote ends.

How things worked before GeckoDriver and Selenium 3.0

If you are new to Selenium and you have started directly with Selenium 3.0, you would not know how Firefox was launched with the previous versions of Selenium (version 2.53 and before). It was a pretty straight forward process where you were not required to use GeckoDriver or any other driver. You just write the code to instantiate the WebDriver and open Firefox. 


What happens when you don’t use GeckoDriver with Selenium 3.0

To try this out, all that you need to do is point your JAR files to the latest version of Selenium 3.0 and then run the same code that is given above. You will now notice that Google.com page would not open in a new Firefox window. Instead you will see an error message like the one shown below –

java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases


Selenium 3 quick Start


1) download Geko driver 
https://github.com/mozilla/geckodriver/releases

2) Download Selenium 3 files
http://www.seleniumhq.org/download/

3) Download Chrome Driver 
https://sites.google.com/a/chromium.org/chromedriver/

4)Add Maven Info

Create a maven java project and add the below dependencies in POM.xml


dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.0.1</version>
    </dependency>  


<dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-firefox-driver</artifactId>
        <version>3.0.1</version>
    </dependency> 



 <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>htmlunit-driver</artifactId>
        <version>2.23</version>
    </dependency> 



<dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>3.0.1</version>
    </dependency> 


Other dependency http://repo1.maven.org/maven2/org/seleniumhq/selenium/


<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.8</version>
  <scope>test</scope>
</dependency>


More info at http://www.seleniumhq.org/download/maven.jsp

Then you can create a java class & run it 


Java class

package com.sel.three.trial;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.*;

public class MyTest {

String driverPath = "C:\\GE\\Tools\\selenium3\\geckodriver11_1\\";
public WebDriver driver;

@BeforeTest
public void launchBrowser() {
System.out.println("launching firefox browser"); 
System.setProperty("webdriver.gecko.driver", driverPath+"geckodriver.exe");
driver = new FirefoxDriver();
}

@Test
public void openApplication() {
driver.navigate().to("http://www.google.com");
}

@AfterTest
public void closeDriver() {
if(driver!=null) {
driver.close();
}
}
}