Sunday 31 August 2014

Multiple set's of test data in single sheet

If you are new to the below information(i.e. using library 'AH-DDF-<<VERSION>>.jar') refer URL: http://automation-home.blogspot.in/2014/06/TestAutomationFrameWork.html

Follow below process for reading different sets of test data in single excel sheet.


Note: Header should be in String format, if the header content is not String format then convert into String format.

Sample Program:

package examples.test.excel;

import java.util.HashMap;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import automation_home.ddf.constants.ExcelConstants;
import automation_home.ddf.wrapper.Wrapper;
import automation_home.ddf.wrapperimpl.ExcelWrapper;;

public class TestData2{

  @DataProvider
  public Object[][] dp1() throws Exception {
 Wrapper wrapper = new ExcelWrapper();
 wrapper.setParameter(ExcelConstants.FILE_PATH, "D:\\Eclipse Workspce\\Sample Project-DDF\\src\\examples\\test\\excel\\TestFile2.xls");
 wrapper.setParameter(ExcelConstants.SHEET_NAME, "FlightInfo1");

 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_HEADER_NAME, "Execution");
 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_YES,"RUN");
 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_NO,"NO-RUN");
 return wrapper.retrieveTestData();
  }

  @Test(dataProvider="dp1", enabled=true)
  public void test1(HashMap<String, String> h){
 System.out.println("FlightName : "+h.get("FlightName"));
 System.out.println("FlightNumber : "+h.get("FlightNumber"));
 System.out.println("AirPort : "+h.get("AirPort"));
 System.out.println("Date : "+h.get("Date"));
 System.out.println("----------------------------------------");
  }


  @DataProvider
  public Object[][] dp2() throws Exception {
 Wrapper wrapper = new ExcelWrapper();
 wrapper.setParameter(ExcelConstants.FILE_PATH, "D:\\Eclipse Workspce\\Sample Project-DDF\\src\\examples\\test\\excel\\TestFile2.xls");
 wrapper.setParameter(ExcelConstants.SHEET_NAME, "FlightInfo1");

 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_HEADER_NAME, "Execution");
 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_YES,"RUN");
 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_NO,"NO-RUN");

 wrapper.setParameter(ExcelConstants.INITIAL_POSITION, "9,0");
 return wrapper.retrieveTestData();
  }

  @Test(dataProvider="dp2", enabled=true)
  public void test2(HashMap<String, String> h){
 System.out.println("1 : "+h.get("1"));
 System.out.println("2 : "+h.get("2"));
 System.out.println("3 : "+h.get("3"));
 System.out.println("4 : "+h.get("4"));
 System.out.println("5 : "+h.get("5"));
 System.out.println("6 : "+h.get("6"));
 System.out.println("7 : "+h.get("7"));
 System.out.println("8 : "+h.get("8"));
 System.out.println("----------------------------------------");
  }


  @DataProvider
  public Object[][] dp3() throws Exception {
 Wrapper wrapper = new ExcelWrapper();
 wrapper.setParameter(ExcelConstants.FILE_PATH, "D:\\Eclipse Workspce\\Sample Project-DDF\\src\\examples\\test\\excel\\TestFile2.xls");
 wrapper.setParameter(ExcelConstants.SHEET_NAME, "FlightInfo1");

 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_HEADER_NAME, "Execution");
 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_YES,"RUN");
 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_NO,"NO-RUN");

 wrapper.setParameter(ExcelConstants.INITIAL_POSITION, "16,0");
 return wrapper.retrieveTestData();
  }

  @Test(dataProvider="dp3", enabled=true)
  public void test3(HashMap<String, String> h){
 System.out.println("TrainName : "+h.get("TrainName"));
 System.out.println("TrainNumber : "+h.get("TrainNumber"));
 System.out.println("Date : "+h.get("Date"));
 System.out.println("----------------------------------------");
  }

  @DataProvider
  public Object[][] dp4() throws Exception {
 Wrapper wrapper = new ExcelWrapper();
 wrapper.setParameter(ExcelConstants.FILE_PATH, "D:\\Eclipse Workspce\\Sample Project-DDF\\src\\examples\\test\\excel\\TestFile2.xls");
 wrapper.setParameter(ExcelConstants.SHEET_NAME, "FlightInfo1");

 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_HEADER_NAME, "Execution");
 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_YES,"RUN");
 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_NO,"NO-RUN");

 wrapper.setParameter(ExcelConstants.INITIAL_POSITION, "22,0");
 return wrapper.retrieveTestData();
  }

  @Test(dataProvider="dp4", enabled=true)
  public void test4(HashMap<String, String> h){
 System.out.println("a : "+h.get("a"));
 System.out.println("b : "+h.get("b"));
 System.out.println("c : "+h.get("c"));
 System.out.println("----------------------------------------");
  }
}

Output:

FlightName : India Air Lines
FlightNumber : 10011.0
AirPort : Bangalore
Date : Thu Apr 10 00:00:00 PDT 2014
----------------------------------------
FlightName : Emirates
FlightNumber : 10012.0
AirPort : Hyderabad
Date : Sun Jun 22 00:00:00 PDT 2014
----------------------------------------
1 : Emirates1
2 : 10012.0
3 : Hyderabad
4 : Sun Jun 22 00:00:00 PDT 2014
5 : Emirates
6 : 10012.0
7 : Hyderabad
8 : Sun Jun 22 00:00:00 PDT 2014
----------------------------------------
TrainName : MUMBAI EXPRESS
TrainNumber : 16332.0
Date : Sun Jun 22 00:00:00 PDT 2014
----------------------------------------
a : 3.0
b : 6.0
c : 7.0
----------------------------------------
PASSED: test1({AirPort=Bangalore, FlightName=India Air Lines, Date=Thu Apr 10 00:00:00 PDT 2014, FlightNumber=10011.0})
PASSED: test1({AirPort=Hyderabad, FlightName=Emirates, Date=Sun Jun 22 00:00:00 PDT 2014, FlightNumber=10012.0})
PASSED: test2({3=Hyderabad, 2=10012.0, 1=Emirates1, 7=Hyderabad, 6=10012.0, 5=Emirates, 4=Sun Jun 22 00:00:00 PDT 2014, 8=Sun Jun 22 00:00:00 PDT 2014})
PASSED: test3({TrainNumber=16332.0, Date=Sun Jun 22 00:00:00 PDT 2014, TrainName=MUMBAI EXPRESS})
PASSED: test4({b=6.0, c=7.0, a=3.0})

===============================================
    Default test
    Tests run: 5, Failures: 0, Skips: 0

===============================================

Note:
1. With the above approach as the data grow's more and more, value for parameter 'ExcelConstants.INITIAL_POSITION' should be updated.
2. Above approach works with "AH-DDF-3.0.4.jar" or higher version.

Download zip file "SingleExcelWithMultiData.zip" for sampe test data and source code for accessing testdata from repository.


Friday 29 August 2014

Verifying the text using browser popup 'Open' file functionality

Read text from Notepad

Here's the program to demonstrate reading the text data from notepad using "java.awt" API.

This is done by following 3 steps:
1. Open the file present in the machine using notepad application.
2. Copy text from notepad using keyboard events(i.e. Cntrl+C).
3. Once the text data is copied, close the notepad and print the captured text(i.e. data captured from notepad).

Sample File:


Sample Program:

package pack1;

import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.KeyEvent;
import java.io.IOException;

/** Reading data from notepad.
 *
 * @author http://automation-home.blogspot.in/
 */
public class ReadTextFromNotepad {

static Runtime rs = Runtime.getRuntime();
static Robot robot = null;
static String filePath = null;
public static void main(String[] args) {

try {
robot = new Robot();

//Note: file should present at path "C:\\Content.txt"
filePath = "C:\\Content.txt";

//Opens the file in notepad.
openNotepad();

//Reads the data from notepad.
String[] notepadContent = readFromNotepad();

//Close the notepad.
closeNotepad();

//Print the data(i.e. text captured from notepad).
for (String text : notepadContent) {
System.out.println(text);
}

} catch (Exception e) {
System.out.println(e);
}
}

/** Opens the file in notepad.
*
*/
public static void openNotepad() {
try {
rs.exec("notepad "+filePath);
robot.delay(500);
} catch (IOException e) {
e.printStackTrace();
}
}

/** Read's the data from notepad.
*
* @return notepadContent as String[]
*/
public static String[] readFromNotepad() {

// Copy text from notepad.
String[] notepadContent = null;
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_C);

robot.keyRelease(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_CONTROL);

Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(DataFlavor.stringFlavor);
try {
String strContent = (String) contents
.getTransferData(DataFlavor.stringFlavor);
notepadContent = strContent.split("\n");
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return notepadContent;
}

/** Closes the opened notepad application.
*
*/
public static void closeNotepad() {
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_F);
robot.keyPress(KeyEvent.VK_X);

robot.keyRelease(KeyEvent.VK_X);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_ALT);
}
}

Output:




Note:
1. Before executing above sample program, file should be present in the file path(i.e. specified in the variable "filePath").
2. While the program is executing don't move the mouse or type(i.e. don't take over the keyboad/mouse control).


Verifying the text using browser popup 'Open' file functionality


Most of the time, we verify the text present in the file by downloading the file(i.e. saving the file into the machine) and reading the data present in the file using the 'java.io.*' API.

Below program demonstrates verifying the text present in the file without saving the file i.e. by using browser's 'Open' file functionality.

Manual Steps:
1. Open the URL 'https://code.google.com/p/selenium/downloads/detail?name=downloads_location&can=2&q='
2. Click on download button/link
3. Popup appears(by default 'Open with' radio button is selected).
4. Click on popup 'OK' button (or) Keyboard 'Enter' key
5. File is opened.
6. Verify the text in the opened file and close the file

Sample automation script to demonstrate the above manual steps:

package wd;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.KeyEvent;
import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class OpenFile {

    static WebDriver driver = null;
    static Robot robot = null;
    static String URL = "https://code.google.com/p/selenium/downloads/detail?name=downloads_location&can=2&q=";
   
    public static void main(String[] args) throws InterruptedException, AWTException {
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        robot = new Robot();
       
        downloadFile();
        String[] notepadContent = readFromNotepad();

        //Close the notepad.
        closeNotepad();
       
        String expectedResults = "see description of the new location at https://code.google.com/p/selenium/downloads/list";
        if(expectedResults.equalsIgnoreCase(notepadContent[0])){
            System.out.println("Success text matches");
        }else{
            System.out.println("Failure text matches");
        }
       
         quitDriver();
    }
   
    public static void quitDriver() throws InterruptedException{
        Thread.sleep(1000);
        driver.close();
        driver.quit();
    }
   
    /** Open the file in web page.
     *
     * @throws InterruptedException
     */
    public static void downloadFile() throws InterruptedException{
        driver.get(URL);
        Thread.sleep(1000);
        WebElement webElement = driver.findElement(By.xpath("//div[@class='box-inner']/a"));
        webElement.click();
        Thread.sleep(1000);
       
        //Open the file.
        robot.keyPress(KeyEvent.VK_TAB);
        robot.keyPress(KeyEvent.VK_TAB);
        robot.keyPress(KeyEvent.VK_TAB);
        Thread.sleep(1500);
        robot.keyPress(KeyEvent.VK_ENTER);
        Thread.sleep(1500);
    }
   
    /** Read's the data from notepad.
    *
    * @return notepadContent as String[]
    */
    public static String[] readFromNotepad() {

    // Copy text from notepad.
    String[] notepadContent = null;
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_A);
    robot.keyPress(KeyEvent.VK_C);

    robot.keyRelease(KeyEvent.VK_C);
    robot.keyRelease(KeyEvent.VK_A);
    robot.keyRelease(KeyEvent.VK_CONTROL);

    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable contents = clipboard.getContents(DataFlavor.stringFlavor);
   
    try {
        String strContent = (String) contents.getTransferData(DataFlavor.stringFlavor);
        notepadContent = strContent.split("\n");
    } catch (UnsupportedFlavorException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return notepadContent;
    }

    /** Closes the opened notepad application.
    *
    */
    public static void closeNotepad() {
    robot.keyPress(KeyEvent.VK_ALT);
    robot.keyPress(KeyEvent.VK_F);
    robot.keyPress(KeyEvent.VK_X);

    robot.keyRelease(KeyEvent.VK_X);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_ALT);
    }
}





Output:



Note:
1. Above program is verified only on windows machine.
2. Program/script work's only with text files(i.e. txt files).
3. While the program is executing don't move the mouse or type(i.e. don't take over the keyboad/mouse control).


Wednesday 20 August 2014

Java Webdriver control hangs, while downloading file in InternetExplorer-9

Webdriver control hangs(i.e. suspended) while performing the download file action in InternetExplorer-9.

Prerequisite:
1. "selenium-java-<<Version>>.jar" is set in java build path.
2. "IEDriverServer.exe" is set.

Sample Program:
package SampleTest.WD;

import java.io.File;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class AppTest {
public static void main(String[] args)  {
try{
File file = new File("Drivers\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver= new InternetExplorerDriver();
driver.get("http://docs.seleniumhq.org/download/");
driver.manage().window().maximize();
Thread.sleep(1000);
                 
                //Click on the link "32 bit Windows IE"
driver.findElement(By.xpath("//*[@id='mainContent']/p[8]/a[1]")).click();
Thread.sleep(1000);
                
                //Click on the link "CHANGELOG".
driver.findElement(By.xpath(".//*[@id='mainContent']/p[8]/a[3]")).click();
}catch (Exception e) {
System.out.println(e);
}
}
}

While executing the above program, webdriver clicks on the link "32 bit Windows IE".
Download options are displayed with "Open", "Save" and "Cancel" buttons, after that webdriver control hangs at IE9 browser and doesn't perform any actions on the browser's(i.e. "CHANGELOG" link is not clicked)


Waited for long time and closed the browser manually, after closing the browser below error message is displayed in the EclipseIDE console.

Error Message:
Started InternetExplorerDriver server (64-bit)
2.42.0.0
Listening on port 25142
org.openqa.selenium.NoSuchWindowException: Unable to find element on closed window (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 18 milliseconds
Build info: version: '2.42.2', revision: '6a6995d31c7c56c340d6f45a76976d43506cd6cc', time: '2014-06-03 10:52:47'
System info: host: 'HW1179', ip: '172.16.6.196', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_23'
Session ID: bc5470cb-0a5d-41dd-9b4f-4593c56d347d
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{platform=WINDOWS, javascriptEnabled=true, elementScrollBehavior=0, enablePersistentHover=true, ignoreZoomSetting=false, ie.ensureCleanSession=false, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss, version=9, ie.usePerProcessProxy=false, cssSelectorsEnabled=true, ignoreProtectedModeSettings=false, requireWindowFocus=false, handlesAlerts=true, initialBrowserUrl=http://localhost:25142/, ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, ie.browserCommandLineSwitches=, takesScreenshot=true}]


Workaround:

Below sample program show's workaround using "java.awt.Robot" API with Webdriver API.

Workaround with below sample program:

package SampleTest.WD;

import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.File;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class AppTest {
public static void main(String[] args)  {
try{
File file = new File("Drivers\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver= new InternetExplorerDriver();
Robot r = new Robot();
 
driver.get("http://docs.seleniumhq.org/download/");
driver.manage().window().maximize();
Thread.sleep(1000);
//Click on the link "32 bit Windows IE".
                //Click the "TAB" button until control reaches the link "32 bit Windows IE". 
for(int i=1;i<=31;i++){
r.keyPress(KeyEvent.VK_TAB);
Thread.sleep(100);
}
r.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(2000);
//Save the file, using ALT+S 
/*r.keyPress(KeyEvent.VK_ALT);
r.keyPress(KeyEvent.VK_S);
r.keyRelease(KeyEvent.VK_S);
r.keyRelease(KeyEvent.VK_ALT);*/
 
//If saving the file with ALT+S doesn't work, then go with below steps.
r.keyPress(KeyEvent.VK_ALT);
r.keyPress(KeyEvent.VK_N);
r.keyRelease(KeyEvent.VK_N);
r.keyRelease(KeyEvent.VK_ALT);
 
r.keyPress(KeyEvent.VK_TAB);
r.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(2000);
 
//Click on the link "CHANGELOG".
driver.findElement(By.xpath(".//*[@id='mainContent']/p[8]/a[3]")).click();
}catch (Exception e) {
System.out.println(e);
}
}
}

Note:
1. While executing the program(using java.awt.Robot API), user(i.e. You) should not perform any operations like moving the mouse, clicking the keyboard key, etc.
2. Another workaround is by using webdriver with sikuli API. 




Tuesday 19 August 2014

Sikuli Installation

Introduction: 
      Sikuli works based on the concept of image recognition. Sikuli compares the input image with the complete UI displayed on the screen and performs the actions (ex: clicking on the UI based in the input image).
Sikuli can be integrated with the Java+Webdriver automation to perform the task's which are not supported by webdriver.

Environment:
Windows XP/Windows 7
Java 6/7
Note: Environment is specified as Window XP/7 with Java 6/7, since installation is done on windows machine.

Installation:
Sikuli can be installed in 2 way's
    Option 1: Downloading jar file(i.e. "sikuli-setup.jar").
    Option 2: Downloading zip file.

The current page illustrates the installation process with option-1(i.e. installing sikuli with "sikuli-setup.jar").

Option 1: Installing Sikuli using "sikuli-setup.jar" file.

Step 1: Download "sikuli-setup.jar" file.
sikuli-setup.jar can be downloaded from below URL's.

Step 2: Create Installation folder.
  • Once the "sikuli-setup.jar" file is downloaded in to your download's folder.
  • Create the folder for installing the Sikuli (ex: D:\Sikuli Installs).
  • Copy "sikuli-setup.jar" from download's folder to installation folder (ex: D:\Sikuli Installs).


Step 3: Install Sikuli.

Double click on the "sikuli-setup.jar" placed in installation folder.

"SikuliX-1.0.1-SetupLog.txt" and "runSetup.cmd" file is created.


Double click on "runSetup.cmd" file.

Click "Setup Now" button.

Click "Yes" button.




Click "OK" button.

Click "OK" button.




Working with Sikuli IDE:

Double click on "runIDE.cmd".




Simple step for clicking the windows "Start" button (i.e. WindowsXP start button).
Select "Mouse Actions" --> "Click()" method. 

Select the image (i.e. "Start" button). 

Execute the statement present in Sikuli IDE by clicking the "Run" button.