Tuesday 2 December 2014

Download file using Selenium WebDriver with AutoIt and Sikuli

Selenium can not handle the Download file window. We can use third party tools to handle download file window. Some of the tools are AutoIT and Sikuli.

AutoIT: Save the below script with extension as download_file.au3 and compile it then it will generate download_file.exe file.
Local $pathToSave=$CmdLine[1]
Local $windHandle=WinGetHandle("[Class:IEFrame]", "")
Local $winTitle = "[HANDLE:" & $windHandle & "]";
;get coordinates of default HWND
Local $ctlText=ControlGetPos ($winTitle, "", "[Class:DirectUIHWND;INSTANCE:1]")

; wait till the notification bar is displayed
Local $color= PixelGetColor ($ctlText[0],$ctlText[1])
while $color <> 0
sleep(500)
$ctlText=ControlGetPos ($winTitle, "", "[Class:DirectUIHWND;INSTANCE:1]")
$color= PixelGetColor ($ctlText[0],$ctlText[1])

wend
; Select save as option
WinActivate ($winTitle, "")
Send("{F6}")
sleep(500)
Send("{TAB}")
sleep(500)
Send("{DOWN}")
sleep(500)
Send("a")

WinWait("Save As")
; activate Save As window
WinActivate("Save As")
; path to save the file is passed as command line arugment
ControlFocus("Save As","","[CLASS:Edit;INSTANCE:1]")
Send($pathToSave)
sleep(500)
;click on save button
ControlClick("Save As","","[TEXT:&Save]")

; wait till the download completes
Local $sAttribute = FileGetAttrib($pathToSave);
while $sAttribute = ""
sleep(500)
$sAttribute = FileGetAttrib($pathToSave)
wend
sleep(2000)
;close the notification bar
WinActivate ($winTitle, "")
Send("{F6}")
sleep(300)
Send("{TAB}")
sleep(300)
Send("{TAB}")
sleep(300)
Send("{TAB}")
sleep(300)
Send("{ENTER}")
Now write the sample java code to use it.
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class DownloadFile_IE_AutoIT {

 public static void main(String[] args) throws InterruptedException {
  System.setProperty("webdriver.ie.driver",System.getProperty("user.dir")+"\\IEDriver.exe");
  WebDriver driver = new InternetExplorerDriver();
  driver.manage().window().maximize();
  driver.get("http://www.seleniumhq.org/download/");
  driver.findElement(By.xpath("//*[@id='mainContent']/p[9]/a[2]")).sendKeys(Keys.ENTER);
  try {
   String path="E:\\AutoIT\\test232";
   String file="E:\\AutoIT\\download_file.exe"+" "+path;
         Runtime.getRuntime().exec(file);
     } catch (IOException e) {
   e.printStackTrace();
  }
 }
}
Sikuli: Download Sikuli from https://launchpad.net/sikuli/+download
Once you install Sikuli in system. Write a below script to handle the download window in IE.
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Key;
import org.sikuli.script.Screen;


public class DownloadFileIE_Sikuli {

 public static void main(String[] args) throws FindFailed {
  System.setProperty("webdriver.ie.driver",System.getProperty("user.dir")+"\\IEDriver.exe");
  WebDriver driver = new InternetExplorerDriver();
  Screen screen = new Screen();  
  driver.manage().window().maximize();
  driver.get("http://www.seleniumhq.org/download/");
  driver.findElement(By.xpath("//*[@id='mainContent']/p[9]/a[2]")).sendKeys(Keys.ENTER);
  
  screen.rightClick("D:\\SaveAsIE1.png");
  screen.keyDown(Key.DOWN);
  screen.click("D:\\SaveAsIE2.png");
  
  screen.click("D:\\FileName.png");
  screen.type("a", Key.CTRL);
  screen.type(Key.BACKSPACE);
  screen.type("IEdriver");
  screen.click("D:\\SaveFile.png");
  System.out.println("File downloaded");
 }
}
Below are the images used in script:






Handle Authentication Window using AutoIT and Sikuli

We can handle authentication window using:
1. AutoIT
2. Sikuli

AutoIT: Download AutoIT from http://www.autoitscript.com/site/autoit/downloads/
Once you install AutoIT in system. Write a autoit script to handle the authentication window.
WinWaitActive("Windows Security")
Send("username")
Send("{TAB}")
Send("password")
Send("{ENTER}")
Save this file as “auth.au3"
Right click on the file and chose “Compile Script (x86)” option and it will make “auth.exe”
Now write the sample java code to use it
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class WinAuthentication_Autoit {

 public static void main(String[] args) {
  System.setProperty("webdriver.ie.driver",System.getProperty("user.dir")+"\\IEDriver.exe");
  WebDriver driver = new InternetExplorerDriver();
  driver.get("http://codesorcery.net/old/authentication/secret/");
  try {
   Runtime.getRuntime().exec("E:\\AutoIT\\Auth.exe");
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}


Sikuli: Download Sikuli from https://launchpad.net/sikuli/+download
Once you install Sikuli in system. Write a below script to handle the authentication window.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Screen;


public class WinAuthentication_Sikuli {

 public static void main(String[] args) throws FindFailed, InterruptedException {
  Screen screen = new Screen();
  System.setProperty("webdriver.ie.driver",System.getProperty("user.dir")+"\\IEDriver.exe");
  WebDriver driver = new InternetExplorerDriver();
  driver.get("http://codesorcery.net/old/authentication/secret/");
  screen.type("D:\\Authentication_Username.png"),"username");
  screen.type("D:\\Authentication_Password.png","password");
  screen.click("D:\\Authentication_OK.png");
 }

}

Below are the images used in script:




Monday 10 November 2014

Setting up TestNG with Eclipse

Installation of TestNG plug-in with Eclipse:
There is no need to download any Jar file or exe file for installation.  We need to just utilize the “Install New Software” option available in the Eclipse.

Installation steps:
1. Click Help –> Install New Software



2. Type “http://beust.com/eclipse” in the “Work with” edit box and click ‘Add’ button
3. In the ‘Name’ column we can see “TestNG” –> Select this and click ‘Next’ button

4.  Click Next and click on the radio button “I accept the terms of the license agreement”
5. Click ‘Next’ button
6. Click ‘Finish’

This will install the TestNG plug-in for Eclipse.

After the installation, it will ask for restart of Eclipse.  Then restart the Eclipse.

Once the Eclipse is restarted, we can see the TestNG icons and menu items as in the below figures.



Monday 3 November 2014

Count a page load time with selenium webdriver

Suppose their is a situation to check time taken for a web page to load. e.g., Check login time into gmail.
import java.awt.AWTException;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TrackWebtime {
 public static void main(String[] args) throws IOException, AWTException{

 WebDriver driver =new FirefoxDriver();
 driver.get("http://www.gmail.com");
 
 long start = System.currentTimeMillis();

 driver.findElement(By.id("Email")).clear();
 driver.findElement(By.id("Email")).sendKeys("username");
 driver.findElement(By.id("Passwd")).clear();
 driver.findElement(By.id("Passwd")).sendKeys("password");

 Robot robot = new Robot();
 robot.keyPress(java.awt.event.KeyEvent.VK_ENTER);

 long finish = System.currentTimeMillis();
 
 long TotalTime = finish - start;

 System.out.println("Total Time for page load - "+TotalTime);

 }
}


Multi Selection

List listItems = driver.findElements("find your select by id or xpath or etc");
Actions builder = new Actions(driver);
builder.clickAndHold(listItems.get(1)).clickAndHold(listItems.get(2)).click();
Action selectMultiple = builder.build();
selectMultiple.perform();