Friday, April 17, 2015

File Upload in Selenium using Autoit



If you don't know how to setup AutoIt Click Here


Upload file in Selenium using AutoIt

Uploading of file is a four steps process:
Step 1: Identify the Windows control
Step 2: Build a AutoIt script using identified windows control
Step 3: Compile the .au3 script and convert it in to .exe file
Step 4: Call the .exe file in to the Selenium test case

Step 1: Identify the Windows control

1) Navigate to http://www.tinyupload.com/ page.
2) Click on ‘Choose file‘ button. It will open a Windows box for ‘File Upload‘.
3) Now go to your Start  > All Program > AutoIt v3 and open ‘SciTE Script Editor‘. It will open an editor window where you copy the below script.

Autoit Script
$count = 0
While $count <> 10
$hdl=WinActivate("File Upload")

If $hdl <> 0 Then
ControlFocus("File Upload","","Edit1")
Sleep(500)
ControlSetText("File Upload","","Edit1","D:\test1.xml")
Sleep(500)
ControlClick("File Upload","","Button1")
Exit
   EndIf
Sleep(1000)
$count=$count+1
WEnd


Steps to Write Scripts:

To identify objects, AutoIt has given us Windows Info tool, it is same like Object Spy in QTP and Element Inspector in any Browser. To open it go to Start > All ProgramAutoIt v3 > AutoIt Window Info.
5) Now drag the ‘Finder Tool‘ box to the object in which you are interested.
You can see that Windows Info tool has populated all the information which is required to use the method.


Step 2: Build an AutoIt script using identified windows control

Take the information from the ‘Window Info” tool and fill in the ControlFocus method <ControlFocus ( “title”, “text”, controlID )>. For “title” we can use ‘Class’, ‘hWnd’ or ‘title’, “text” is optional and ‘controlId’ is “Edit1″ (Class name + Class Instance),  so our final statement will be like these:

ControlFocus("File Upload","","Edit1")
Sleep(500)
The above script is only for File name text box. 
ControlSetText: This command is used for setting the text on the edit field. ControlClick: This command is used for click action.
ControlSetText("File Upload","","Edit1","D:\test1.xml")
Sleep(500)
For clicking on Open button:
ControlClick("File Upload","","Button1")

The script in the editor looks like below.


Thats it .

After that click on tools -> Compile -> Build 

Step 3: Compile the .au3 script and convert it in to .exe file

Now save the above script, if in case you have not saved the name earlier, please save it in .au3 format. The next step is to convert it in to .exe format. For that you need to right click on the .au3 file and select “Compile Script“.
Auto-10Note: Make sure that you select ‘Compile Script’ as per your machine configuration. Select normal version if you are on 32 bits, and select x64 or x86 if you are on 64 bits.

Step 4: Call the .exe file in to the Selenium test case

Once you done with the compiling, it will create the ‘.exe’ file with the same name under the same folder and that ‘.exe’ file will be called in the Selenium Test Script by using the following script:
Process p=Runtime.getRuntime().exec("D:\\autoit\\upload.exe");

Source code to call .exe in selenium

package autoit;

import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class demoupload {

public static void main(String[] args) throws IOException, InterruptedException {
// TODO Auto-generated method stub
WebDriver driver=new FirefoxDriver();
driver.get("http://www.tinyupload.com/");
driver.manage().window().maximize();
driver.findElement(By.name("uploaded_file")).click();
Process p=Runtime.getRuntime().exec("D:\\autoit\\upload.exe");
p.waitFor();
}

}

Result :




1 comment:

  1. How can we change to Autoit script if we want to have variable file path which can be passed during call of exe in selenium test??

    ReplyDelete