Friday 23 August 2013

Selenium WebDriver Configuration Maven/Eclipse/Java

  1. Create a folder [POMFOLDER] to contain your Selenium project files, within this folder create a file [pom.xml] file & Paste following code in it.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
MySel20Proj
MySel20Proj
1.0
org.seleniumhq.selenium
selenium-java
2.21.0
com.opera
operadriver
com.opera
operadriver
0.13
                                 
                
                 
  1. Type “Download Eclipse” on google.co.in or go to URL - http://www.eclipse.org/ downloads/ & download Eclipse Classic 3.7.1. Unzip eclipse.zip file. Create a shortcut of “eclipse.exe”, right click on “Shortcut to eclipse.exe” icon & click on properties. In Target field append value [ -vm "C:\Program Files\Java\jdk1.7.0_02\bin\javaw" -vmargs -Xms256m -Xmx768m -XX:PermSize=256M -XX:MaxPermSize=512M ].
  2. Download [jdk1.7.0_02] & install it.
  3. Download [apache-maven-3.0.4-bin.zip] & unzip it. Open Readme.txt and follow the steps to install maven.
  4. Go to Environment Variables [My Computer -> Properties -> Advance -> Environment Variables -> System Variable, update/create following variables -
                JAVA_HOME = C:\Program Files\Java\jdk1.6.0_25 ‘path of JDK
                PATH=C:\SeleniumProject\apache-maven-3.0.4\bin;%PATH% ‘path of                               Maven Folder
                CLASSPATH= %JAVA_HOME%\lib ‘Path of JDK Library folder
                M2_HOME = C:\SeleniumProject\apache-maven-3.0.4 ‘Path of Maven

  1. Open Command prompt (Start -> Run -> CMD), type mvn clean install. It takes 5-10 minutes to install, check for [build successful] message at the end of deployment.
     7.   Go the folder where you had created pom.xml file (Refer Step 1) & type mvn ec             lipse:eclipse -DdownloadSources=true  -DdownloadJavadocs=true.  It 
           takes 5-10 minutes to install, check for [build successful] message at the end of 
           deployment. 
  1. Open Eclipse by clicking on [Shortcut to eclipse.exe], Go to [File -> Import -> General -> Existing Projects into Workspace], click on [Next] button, Click on [Browse] button & select folder [POMFOLDER] (refer step 1).
  2. Under [Eclipse -> Project Explorer] tab Maven project [MySel20Proj] is displayed.
  3. Go to [Help -> Install New Software] & Enter URL - http://download.eclipse.org/technology/m2e/releases & click on [Next -> Finish] button, Eclipse prompts for restart, restart it.
  4. Create a [Source Folder] (Right click on MySel20Proj, go to New & click on Source Folder)
  5. Create a Class File (Right click on Source Folder, go to New & click on Class). New Java Class wizard get opened, enter class name, select checkbox for public static void main (String[] args) & click on Finish button.
  6. You are ready to write first selenium program, paste following code within public static void main function –
                                       WebDriverdriver = new FirefoxDriver();

                                       driver.get("http://www.google.com");

  1. Click on Crtl+F11, Firefox browser gets opened & URL www.google.com is displayed.

Selenium WebDriver :- What are Drivers for Firefox, IE, Chrome, Opera, Safari

List of Driver for Firefox, Chrome, Opera and Safari browser are as follows -


Browser
Name of Driver Server
Remarks
HTMLUnit
(none)
Driver Server is not required
Firefox
(none)
Driver Server is not required
Internet Explorer
Internet Explorer Driver Server
Available in 32 and 64-bit versions
Chrome
ChromeDriver

Opera
OperaDriver
PhantomJS
GhostDriver
 It is another headless browser just like HTMLUnit.
Safari
SafariDriver

Selenium WebDriver : - How to capture screenshot

#SeleniumWebDriver  #TakeScreenshot #TheBuddhaTree

The Problem
When we do nightly test execution, we need historical evidence to analyze & debug script failure. Sometime, it can be challenging to track down the issue that caused a failure using failure message along with stack trace. Especially when you run the test again and it passes.

A Solution
A simple way to gain insight into your test failures is to capture screenshots of AUT at the moment of failure and add the screenshots into test execution result. 

How to Implement using Selenium
Selenium WebDriver provides the TakesScreenshot interface for capturing a screenshot of a web page. This helps in test runs, showing exactly what happened when an exception or error occurred during execution, and so on.


TakesScreenshot interface provides the getScreenshotAs() method to capture a screenshot of the page displayed in the driver instance.  Example code is as follows –


import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class TakeScreenshotOfApplication {
                WebDriver driver;

    @Test
       public void compareTitle() {
              driver = new FirefoxDriver();

              driver.get("http://google.co.in");

            // compare title & take screenshot of application if it fails
       if (driver.getTitle().contains("facebook")) {
                 System.out.println("Title matched!!");
       } else {
                 System.out.println("Title did not match");
                 takeScreenshotMethod();
            }
        }

        private void takeScreenshotMethod() {
             // code to take screen shot of browser
            File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

             // file name where image to be saved
             String screenshotpath = "C:\\google.png";
               try {
                       // save image to drive
                        FileUtils.copyFile(scrFile, new File(screenshotpath));
               } catch (IOException e) {
                       e.printStackTrace();
              }
          }

}

Expected Behavior
  1. Load the URL
  2. Take Screenshot
  3. Save Screenshot Image to local drive
Happy Learning
}

Selenium : Read Excel file using JExcel API

Code to read  value from Excel file using selenium WebDriver & Java is as follows -

inputWorkbook = new File("C:\\Controller.xls"); //specify excel file path

    Workbook w;
               
    try {
               
            try {

                        w = Workbook.getWorkbook(inputWorkbook);

                        sheet = w.getSheet("Sheet1"); //Worksheet name

                        } catch (IOException e) {
                                                                       
                                    error("Reading Master Sheet :-" +e);
                        }
                 
                        suite = new String[sheet.getRows()][sheet.getColumns()];
              
                        for (int j = 0; j < sheet.getColumns(); j++){
                                   
                                    for (int i = 0; i < sheet.getRows(); i++){
                     
                                                Cell cell = sheet.getCell(j, i);
                     
                                                if (cell.getContents()!= null){
                         
                                                            suite[i][j]=cell.getContents();
                                                }
                   
                                    }
                 
                        }
              
Note – Suite[][] is two dimensional array which stores value read from Excel Worksheet.

Maximize screen of internet explorer 10 with selenium

Code to maximize browser window in Selenium is -


driver.manage().window().maximize();