Wednesday 3 April 2013

Uploading a file using Autoit & WebDriver

I have been reading many mails from various selenium groups where people asking for solution on uploading files through desktop in case sendkeys is not working where the input element is not associated with the browse button since a window's pop up is opened which only accepts a stand-alone file.

Some lines from Auto-it website:


AutoIt v3 is a 3rd party freeware BASIC-like scripting language tool designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). 


Here you need to integrate Auto-it to make it work. Auto-it comes with various components and a complete workflow:

Steps to follow:

Step 1: Download Auto-it from here
Step 2: You will get an installer, just double click and run the executable file.
Step 3: Following components will be installed on your machine


             SciTe Script Editor - Use for writing scripts & compiling programs.
             AutoIT window info - Locate class & property of the window's based element etc.





















Step 4: Write the script using the SciTe Script Editor. Copy and paste the below script in the editor and save it with .au3 format and then compile it by clicking on Tools menu --> Compile




Opt("MustDeclareVars", 1);0=no, 1=require pre-declare

Main()


Func Main()
  
    Local Const $dialogTitle = $CmdLine[2]
    Local Const $timeout = 5

    Local $windowFound = WinWait($dialogTitle, "", $timeout)
    
   
    $windowFound = WinWait($dialogTitle, "", $timeout)
    Local $windowHandle

    If $windowFound Then
       
        $windowHandle = WinGetHandle("[LAST]")
        WinActivate($windowHandle)
   
        ControlSetText($windowHandle, "", "[CLASS:Edit; INSTANCE:1]", $CmdLine[1])
        ControlClick($windowHandle, "", "[CLASS:Button; TEXT:&Open]")        
        
    Else
     
        MsgBox(0, "", "Could not find window.")
        Exit(1)
     EndIf
EndFunc    


Step 5: After compile the script you will get a .exe file that is created on the same location where the script is placed



import java.io.IOException;

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


public class testImageUpload {



public static void main(String[] args) throws IOException {

WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.imageshack.us/");

driver.findElement(By.xpath("//*[@id='SWFUpload_0']")).click();

Process process = new ProcessBuilder("C:\\Users\\selenium\\auto.exe",
                "C:\\Users\\selenium\\test.png", "Open").start();   


}

}

Image upload example used for demonstration in this article is at:
http://imageshack.us/




Magic of Actions class with WebDriver

Most of us have been facing issues while automating elements like:

  1. Sliders
  2. Resizing an Element
  3. Drag & Drop 
  4. Hovering a mouse, specially in a case when dealing with mouse over menus.
This had been a complex task for any automation tool to handle such components. With WebDriver using the Actions class the same can be handled very easily and without applying any complex logics to it.

Such Elements like:

SLIDERS












Test sites:

Slider:  http://jqueryui.com/resources/demos/slider/default.html


Resizable:  http://jqueryui.com/resources/demos/resizable/default.html

Drag & Drop: http://jqueryui.com/resources/demos/droppable/default.html


Call the actions class and pass the driver object in the constructors.

Actions action = new Actions(driver);


Important Methods that will do the magic:

For Dragging & Dropping an element from source to target:

action.dragAndDrop(WebElement source, WebElement target)

For Sliding and Resizing an Element

action.dragAndDropBy(WebElement source, int xOffset, int yOffset)


Code:

Dragging & Dropping an Element:



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


public class testDragandDrop {


public static void main(String[] args) throws InterruptedException {


WebDriver driver = new FirefoxDriver();
driver.get("http://jqueryui.com/resources/demos/droppable/default.html");


WebElement draggable = driver.findElement(By.xpath("//*[@id='draggable']"));
WebElement droppable = driver.findElement(By.xpath("//*[@id='droppable']"));


Actions action = new Actions(driver);

action.dragAndDrop(draggable, droppable).perform();





}

}



Sliding an Element:



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


public class testSlider {

/**
* @param args
* @throws InterruptedException 
*/
public static void main(String[] args) throws InterruptedException {




WebDriver driver = new FirefoxDriver();
driver.get("http://jqueryui.com/resources/demos/slider/default.html");

WebElement slider = driver.findElement(By.xpath("//*[@id='slider']/a"));

Actions action = new Actions(driver);
Thread.sleep(3000);
action.dragAndDropBy(slider, 90, 0).perform();



}

}



Re-sizing an Element:



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


public class testResizable {


public static void main(String[] args) throws InterruptedException {

WebDriver driver = new FirefoxDriver();
driver.get("http://jqueryui.com/resources/demos/resizable/default.html");


WebElement resize = driver.findElement(By.xpath("//*[@id='resizable']/div[3]"));

Actions action = new Actions(driver);
action.dragAndDropBy(resize, 400, 200).perform();


}

}




Selenium training