Skip to main content

File Upload in Selenium using Autoit

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 :

Comments

Popular posts from this blog

Reporting in Selenium

How do you Handle reporting in your project ? Method 1 : Maven - Testng - Surefire report The project will be created in Maven with  SureFire Plugin  and  SuiteXmlFile  tag in the POM.xml . The testng.xml will be called in the POM.            <plugin>                 <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-surefire-plugin</artifactId>            <version>2.18.1</version>                 <configuration>                     <suiteXmlFiles>                         <suiteXmlFile>                             ...

Handling Modal Dialog Window in Selenium Webdriver

Handling Modal Dialog Window in Selenium Webdriver In this tutorial we are going to see how to handle " Modal Dialog Window "  using Selenium Webdriver Disadvantage using Selenium Use Can't handle elements which is inside  Modal Dialog Window. Using Robot Class Using Robot Class we can handle  Modal Dialog Window. In this tutorial Open  http://vodkabears.github.io/remodal/# Click on  Show  button In the  Modal Dialog Window.  Click on Follow button  Source Code : import java.awt.AWTException; import java.awt.Robot; import java.awt.Toolkit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import java.awt.datatransfer.StringSelection; import java.awt.event.KeyEvent; public class robot_demo { public static void main(String[] args) throws InterruptedException, AWTException { WebDrive...

Autonium

About Autonium Autonium is a test automation framework for web applications. This framework is built by using Java and Selenium. It uses Maven project structure. Easy to understand and use it. Having basic idea on Java, Selenium, Maven and TestNG is good enough to start with Autonium. Let’s start Automation with Autonium --------------------------------------------------- Advantages Provides support to all major browsers –  Firefox, Chrome, IE and Safari . Capability of executing scripts on  Remote Machines  by using Selenium Server. Users can make use of Selenium Web driver methods and Autonium methods. Autonium simplifies  Test data management . Test data can be read from properties files. Scripts can be developed without  hard coding  test data . Identifiers can also be read from properties files and  hard coding   of identifiers can be avoided. Test data and Identifier v...