PAGE 18 :FILE UPLOAD IN SELENIUM USING Autoit

Autoit Setup for Selenium Webdriver




WHAT IS AUTOIT

AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokesmouse movement andwindow/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying “runtimes” required!
In layman’s term AutoIt is just another automation tool like Selenium but unlike Selenium it is used for Desktop Automation rather Web Automation.  It is a powerful tool and it just not automate desktop windows, button & form, it automates mouse movements & keystrokes too. Just like Selenium IDE, it also gives you the recording capability which generates the scripts for you to use the same script in you test.

AUTOIT FEATURES

1) Easy to learn: It is just another scripting language and very easy to use. Under help menu of AutoIt it gives you all the functions and methods you can use with detailed explanation and examples.
2) Simulates keystrokes: Where ever it is required to use keystrokes in your test, you can use this for example pressing enter on any dialog box and typing username and password on the popup which you cannot simulate with Selenium.
3) Simulate mouse movements: Like keystrokes there can be situations when you are required to simulate the mouse movements and it is the easiest way out for those situations.
4) Scripts can be compiled into standalone executable: It means that you do not require any IDE to run your scripts, you can easily convert your automation scripts into .exe files which can be run on their own.
5) Windows Management: You can expect to move, hide, show, resize, activate, close and pretty much do what you want with windows. Windows can be referenced by title, text on the window, size, position, class and even internal Win32 API handles.
6) Windows Controls: Directly get information on and interact with edit boxes, check boxes, list boxes, combos, buttons, status bars without the risk of keystrokes getting lost.  Even work with controls in windows that aren’t active!
7) Detailed help file and large community-based support forums: You think of any action on windows, you will get it on help file. You face any issue or get stuck anywhere, the large group of users are there to help you.
In short, any windows,  mouse & keystrokes simulation which we cannot handle with Selenium that can be handled with AutoIt. All we need to do is to use the script in Selenium which is generated with the help of AutoIt tool.

Steps to Download AutoIt v3

1) Go to AutoIt website and navigate to download page. It will display the latest version. The top most download is for AutoIt, click on it.
Auto
2) It will open up a pop up box, click on ‘Save File‘ button.
Auto-1
3) Once the download is complete, double click on the .exe file and it will ask for your agreement on their terms and condition. Click on ‘I Agree‘ button.
Auto-2
4) Complete the process and click on the ‘Finish‘ button at the end of the installation.
Auto-4
5) Go to your program menu and look at the AutoIt folder. If you have 32 bit system, then your folder will look like the image below.
Auto-5
If you have 64 bit system and during installation you have choose default x86 configuration, then your folder will  look like the image below.
Auto-5.1

RECORDING OF AUTOIT SCRIPT

Yes I know that you are surprised to see that AutoIt also have recording feature which helps in generating scripts automatically. But for that we need to use its extension called AutoIt Script Editor. Although script editor is also get installed with AutoIt but it comes with very limited feature.

STEPS TO DOWNLOAD AUTOIT SCRIPT EDITOR

1) Go to AutoIt website and navigate to download page. It will display the latest version. The second download is for AutoIt Script Editor, click on it.
AutoS
2) A new page will open, click on the top most link of ‘SciTE4AutoIt3.exe‘ and follow the process until the installation is finished.
AutoS-1



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 :

3 comments:

Candela Érika García said...

I can't use the "Open File" window in Chrome and Edge, AutoIt just doesn't find it by the title :(

Amrit Upadhyay said...

@Candela Érika García , will help you out in your problem .
Please wait for a couple of days for me to create a video and share to you to make sure you can understand it better.

Thanks,
Amrit Upadhyay

gp007 said...

You won't believe me, but I was planning to write a blog very similar to the one you have posted here. Great work!

Selenium Training In Chennai

Java Training In Chennai

About Me

My photo
Pune, Maharastra, India
You can reach me out at : jimmiamrit@gmail.com