Sie sind auf Seite 1von 15

8.

AutoIt

8.1 Introduction
In this tutorial we will just not look at the code for automating downloads, uploads and handling
user authentication through Selenium using AutoIt tool rather we will going to learn how to use
AutoIt in detail, so that we can create our own scripts for any windows operation.

Need of third party tools in Selenium

Web applications do not always confine themselves to working entirely on the web. Sometimes
they need to interact with the desktop to do things like downloads & uploads. Automating these
sorts of workflow is tricky in Selenium. Selenium is confined to automating browsers, so desktop
windows are out of scope. If you are looking to automate workflows that go from browser to
desktop and back in Selenium, then a little AutoIt is in order.

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 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). 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.

8.2 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.
8. AutoIt

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.

8.3 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.

2) It will open up a pop up box, click on ‘Save File’ button.

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.

4) Complete the process and click on the ‘Finish’ button at the end of the installation.

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.

If you have 64 bit system and during installation you have choose default x86 configuration, then
your folder will look like the image below.

8.4 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.
8. AutoIt

8.5 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.

2) A new page will open, click on the top most link of ‘SciTE4AutoIt3.exe’ and follow the
process until the installation is finished.

8.6 Upload file in Selenium using AutoIt

Uploading of file is a four steps process:

Step 1: Identify the Windows control

Step 2: Build an 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 Practice Form page of ToolsQA.

2) Click on ‘Browse’ button of Profile Picture column. 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 we write out automation script of AutoIt.

Before writing scripts we should know that what we need to write, as we are new with this tool
and we even do not know what language it follows and how the script look like. To get the help
for it, open ‘Help’ from SciTE Script Editor.

As we are interested in firing commands, just type ‘Commands’ on the search text field of the
Help. It will give all the commands you can use in your test.
8. AutoIt

Open few commands and check out the description, it explains the syntax, parameter & detailed
description of each method with a descriptive example.

4) Now the first task we need to perform is to click on the ‘Edit’ field of the upload file windows
box. To get the command for the focus on the edit field, open ‘ControlFocus’ from the help.
8. AutoIt

It says that we need “title”, “text” and “ControlID” to use the ControlFocus method. To obtain
these we need help to identify windows object. 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 Program > AutoIt 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:

1 ControlFocus ( "File Upload", "", "Edit1")


2
3 Or
4
5 ControlFocus("[CLASS:#32770]", "", "Edit1")
6
7 Or
8. AutoIt

8
9 Local $hWnd = WinWait("[CLASS:#32770]", "", 10)
10
11 ControlFocus(hWnd, "", "Edit1")

To test the above commands you need to Save the test first in ‘.au3’ format. After saving the file
try testing all of the above commands. Just press F5 or go to Tools > Go on the SciTE Script
Editor. It will execute the statement write at that moment and will display you the result in the
same SciTE Script Editor window.

Make sure that upload file window is opened on the back ground and the control is not over the
edit field.

Note: If you scroll down the help window for any command, you will get a detailed example
which is quite easy to use and understand. That’s why in the beginning of this tutorial I said that
don’t just use the AutoIt script, learn how to build it.

Let’s complete the test now and include the steps for choosing a file & pressing Open button.

ControlSetText: This command is used for setting the text on the edit field. ControlClick: This
command is used for click action.

Final script will be like this:

1 ; Wait 10 seconds for the Upload window to appear


2
3 Local$hWnd=WinWait("[CLASS:#32770]","",10)
4
5 ; Set input focus to the edit control of Upload window using the handle returned by WinWait
6
7 ControlFocus($hWnd,"","Edit1")
8
9 ; Wait for 2 seconds.
10
11 Sleep(2000)
12
13 ; Set the File name text on the Edit field
14
15 ControlSetText($hWnd, "", "Edit1", "SomeFile.txt")
16
17 Sleep(2000)
18
19 ; Click on the Open button
8. AutoIt

20
21 ControlClick($hWnd, "","Button1");

Or
1 ; Wait 10 seconds for the Upload window to appear
2
3 WinWait("[CLASS:#32770]","",10)
4
5 ; Set input focus to the edit control of Upload window using the handle returned by WinWait
6
7 ControlFocus("File Upload","","Edit1")
8
9 Sleep(2000)
10
11 ; Set the File name text on the Edit field
12
13 ControlSetText("File Upload", "", "Edit1", "SomeFile.txt")
14
15 Sleep(2000)
16
17 ; Click on the Open button
18
19 ControlClick("File Upload", "","Button1");

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“.
8. AutoIt

Note: 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:

1 Runtime.getRuntime().exec("D:\AutoIt\AutoItTest.exe");

A complete test will look like this:


1 package practiceTestCases;
2
3 import java.io.IOException;
4
5 import java.util.concurrent.TimeUnit;
6
7 import org.openqa.selenium.By;
8
9 import org.openqa.selenium.WebDriver;
10
11 import org.openqa.selenium.firefox.FirefoxDriver;
12
13 public class AutoIt {
14
15 private static WebDriver driver = null;
16
17 public static void main(String[] args) throws IOException, InterruptedException {
18
19 driver = new FirefoxDriver();
20
21 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
22
23 driver.get("http://toolsqa.com/automation-practice-form");
24
25 driver.findElement(By.id("photo")).click();
26
27 Runtime.getRuntime().exec("D:\AutoIt\AutoItTest.exe");
28
29 Thread.sleep(5000);
8. AutoIt

30
31 driver.close();
32
33 }
34
35 }

8. 7 Upload file in Selenium with AutoIt Recorded Script

Before using this feature, please make sure that you have installed AutoIt Script Editor,
otherwise the recorder option will not display on editor window.

1) Open the Script Editor window, save the blank file with ‘.au3′ extension and then go to Tools
> AU3Recorder or Alt + F6 on AutoIt Script Editor. A window will open for recording.

2) Click on Logo to start recording. You need to make sure that mouse interaction has to be
avoided wherever it is possible and encourage keyboard use. After starting the recording, do your
things on the already opened window of file upload and stop recording by clicking on the same
logo. Your script will somewhat look like this:
8. AutoIt

Note: If in case the upload file window was not already opened and you have opened it after
starting recording, it will capture those steps too, in that case you need to make sure that you
remove the code generated for those steps only before compiling it.

Note: Mouse interaction may vary in different screen resolution windows, avoid using it and
encourage keyboard use.

3) Now save the generated script, compile it and use it in your Selenium test script accordingly.

Attention: If you are on 64 bit system and WinWaitActivate gives an error, change it to
WinWaitActive then.

8. 8 Handle Windows based Authentication Pop Up in Selenium using AutoIt


The main use of AutoIt script in Selenium is to handle Windows based pop ups like Username &
Password Authentication. Most of the times AutoIt does the trick but sometimes not. I have seen
few discussions where users are saying that AutoIt is not working, as Selenium script gets stuck
at the pop up and does not move forward till the time any action is performed on the
Authentication window. At the end of this tutorial you will see the workaround for that situation
as well.

1) Open the URL on which the authentication is required and open the AutoIt Window Info tool
to get the name of the class and the text of the authentication window.
8. AutoIt

2) Drag the ‘Finder Tool’ box to the object in which you are interested and it display you the
information.

Note: You must have noticed that the Info tool has not picked up any information for the Control.
Most probably in this case the AutoIt script will not work but we still have a workaround for
this..

Class information and Instance information is left as blank for ‘Basic Control Info’. In this case
we will not be able to send the username and password using ‘ControlSetText’, as we do not
have the details for it. But as it has identified the main authentication window, we can simply use
the keyboard strokes to send the username and password.

3) Let’s record the steps to send the username & password to the authentication window, for that
please activate the Recorder by Tools >AU3Recorder or Alt + F6 on AutoIt Script Editor.
8. AutoIt

4) Top left signal says that the recording is on. Now type the username and password and do not
use mouse to click on the any field, simply use keyboard’s tab button to do the thing.

5) Once you done with your recording, your code will look like this.
8. AutoIt

Note: Password I entered is ‘Test123′, for capital ‘T’ I had to press Shift button.

6) 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 do right click on
the .au3 file and select “Compile Script“.

Note: 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.

7) 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:

1 Runtime.getRuntime().exec("D:\AutoIt\AutoItTest.exe");

A complete test will look like this:


1 package practiceTestCases;
2
3 import java.io.IOException;
4
5 import java.util.concurrent.TimeUnit;
6
8. AutoIt

7 import org.openqa.selenium.By;
8
9 import org.openqa.selenium.WebDriver;
10
11 import org.openqa.selenium.firefox.FirefoxDriver;
12
13 public class AutoIt {
14
15 private static WebDriver driver = null;
16
17 public static void main(String[] args) throws IOException, InterruptedException {
18
19 driver = new FirefoxDriver();
20
21 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
22
23 driver.get("http://www.example.com");
24
25 Runtime.getRuntime().exec("D:\AutoIt\AutoItTest.exe");
26
27 Thread.sleep(5000);
28
29 driver.close();
30
31 }
32
33 }

Main Trick

If in case your script did not work, all you need to do is to call the AutoIt script first before
opening the URL. AutoIt script takes few seconds to start, in the meantime your Selenium script
will open the url and it will display the authentication window.
1 package practiceTestCases;
2
3 import java.io.IOException;
4
5 import java.util.concurrent.TimeUnit;
6
7 import org.openqa.selenium.By;
8
8. AutoIt

9 import org.openqa.selenium.WebDriver;
10
11 import org.openqa.selenium.firefox.FirefoxDriver;
12
13 public class AutoIt {
14
15 private static WebDriver driver = null;
16
17 public static void main(String[] args) throws IOException, InterruptedException {
18
19 driver = new FirefoxDriver();
20
21 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
22
23 Runtime.getRuntime().exec("D:\AutoIt\AutoItTest.exe");
24
25 driver.get("http://www.example.com");
26
27 Thread.sleep(5000);
28
29 driver.close();
30
31 }
32
33 }

Authentication can be handled with the Browser settings as well, please visit Http Proxy
Authentication with Selenium Webdriver