Wednesday 10 August 2016

Executing the webdriver script using webdriver 3.0.0-beta2 version

Below steps guides you for executing the java webdriver script with webdriver version '3.0.0-beta2'.

Prerequisite:
- To work with webdriver version '3.0.0-beta2', requires java 1.8 version installed and java path is set to java 1.8.
For installing java 1.8 and path setting, refer link http://automation-home.blogspot.com/2016/08/java-8-installation-and-path-setting.html
- Eclipse IDE is already installed.

Download java webdriver client libraries:
Download java webdriver libraries from http://www.seleniumhq.org/download
  Click 'Download' button for 'java' to download the selenium webdriver java client libraries.
  
Extract the downloaded zip file:
Extract the downloaded 'selenium-java-3.0.0-beta2.zip' file.

 

Open the Eclipse IDE:
Right click on the project and select 'Properties'.

set the path for jar files present in the extracted zip file 'selenium-java-3.0.0-beta2.zip'.

Sample Program:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WSTest {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new FirefoxDriver();
        // driver.manage().window().maximize();
        driver.get("http://www.google.com");

        Thread.sleep(3000);
        driver.close();
        driver.quit();
    }
}

Execute the script.
        Thread.sleep() is added for demo, in real time projects its not recommended to use Thread.sleep().


'UnsupportedClassVersionError' is occurred while executing the script, 
even after java 1.8 is installed and path setting is done in environment variables.

Verify Eclipse IDE is mapping to Java 1.8.

 If java version is not 1.8, then update java version to 1.8



If eclipse ide is mapping to older version of java, then update it to java 1.8 



Re Execute the script: 
 
Exception in thread "main" 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
.......
........
will be displayed.

Re-execute the script: 
Re-execute the script after performing below steps


Extract the downloaded 'geckodriver-v0.10.0-win64.zip' file and copy the 'geckodriver.exe' in your webdriver project.

Add below one line code in your webdriver automation script.
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "\\geckodriver.exe");

Sample Program:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WSTest {
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "\\geckodriver.exe");

        WebDriver driver = new FirefoxDriver();
        // driver.manage().window().maximize();
        driver.get("http://www.google.com");

        Thread.sleep(3000);
        driver.close();
        driver.quit();
    }
}

Re-execute the script now. 
You can observe the 'JavaScript warning' in console, but the script is executed successfully.

JavaScript warning: https://normandy.cdn.mozilla.net/static/bundles/selfrepair-72948156b77d6ce320e0.1e946d807ad4.js, line 11001: mutating the [[Prototype]] of an object will cause your code to run very slowly; instead create the object with the correct initial [[Prototype]] value using Object.create

Check the Firefox version

Update the Firefox version.

Re-execute the script again, after updating the Firefox version
Observe there is no 'JavaScript warning' in console and script is executed successfully(i.e. launching 
the firefox browser, access url http://www.google.com and close browser). 

By default firefox browser is maximized with out using the statement 'driver.manage().window().maximize();'.

Summary: To work with latest version of webdriver 3.0.0-beta2, it requires.
- Install Java 1.8.
- Open eclipse ide and update the JDK compiler compliance level to java 1.8.
- Download webdriver java(3.0.0-beta2) client libraries and set it in java build path.
- Download the geckodriver.exe and set it System.setProperty using 'webdriver.gecko.driver'.
- Upgrade the Firefox version to latest version.
- By default firefox browser is maximized(i.e no need to use statement driver.manage().window().maximize();)

Java 8 installation and path setting



Install Java 8 from http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
 Click 'jdk-8u101-windows-x64.exe' to install java 8 (64 bit) for Windows OS.


Click 'Save File'
Click downloaded 'jdk-8u101-windows-x64.exe' file to install.

Click 'Next' button.

Click 'Next' button.


Click 'Next' button.


Click 'Close' button.

Open the command prompt and type command 'java -version' and
verify java 8 path is set.
Note: In my machine java 7 is already installed and java 7 path is already set.

After executing the command 'java -version', if you can't view the 
java version as 1.8 that means java 8 path is not set.

 You can set the java 8 path in environment path.
Note: Above screenshot is captured from Windows 8 OS.

Click 'Advanced' tab.
Click 'Environment Variables' button.

Update the java path to java 8(if java path is set for older version of java). 
If you are setting the java path for first time, then you can create a new variable 'JAVA_HOME' and
 enter variable value referring the '<<Path of java 8 jdk>>'


Open the command prompt, if command prompt is already opened then close 
the command prompt and open the new command prompt.
After command prompt is opened, execute the command 'java -version'.
If java version 1.8 is displayed, that means java path is set to java 8.