AutoIt What ? & Why ?

Auto It is an open source tool, used to automate the operating system based applications

Sometime we might face scenarios in selenium like automating window pop ups at that time selenium will not help us to automate such pop ups,so we have to use third party tools like AutoIt or sikuli.

Let us discuss AutoIt application , It is basically used to simulate the keystrokes and mouse action and also we can create GUI but our site limit till mouse actions and keystrokes

AutoIt has built In feature to compile script and generate .exe executable file. You not need any extra add-on to do all these things.

Some window Pop ups for information

Download pop up
selenium server

Save as pop up save as pop up

Window Applications notepad application

To install AutoIt follow the below steps


1.Goto https://www.autoitscript.com/site/autoit/downloads/

2.Click download AutoIt button
download autoit

3.After downloading goto the downloaded folder and right click on the autoIt application and select Open and follow the download instructions install autoit

3.1.It may take 2 minutes to install installing autoit

4.Click Finish button
5.Once the installation is complete ,Goto Start ->All Programs->AutoIt v3->select sciTE Script Editor start script editor

6.Script editor looks Like below one here we are going to write our script to perform action on the window appliaction script editor

How to handle two Browser Windows in Selenium

Inspect Window Pop Ups with AutoIT

Before we perform any action with AutoIt we need to find the object on which we are going to perform the action like as in selenium.

So to inspect element in AutoIt we are using AutoIt Window Info application ,we do not need to download it separately as it comes with AutoIt application .
To open the AutoIt Window Info application

Goto Start->All Programs->AutoIt V3->AutoIt Window Info window info

AutoIt Window Info

AutoIt v3 comes with a standalone tool called the AutoIt Window Info Tool (Program Files\AutoIt3\Au3Info.exe). Au3Info allows you to get information from a specified window that can be used to effectively automate it. Information that can be obtained includes:

1.Window titles

2.Text on the window (visible and hidden)

3.Window size and position

selenium server

4.Contents of the status bar

5.Position of the mouse pointer

6.Color of the pixels underneath the mouse pointer

7.Details of the Control underneath the mouse pointer
selenium server

WinActivate

1.Click the web present in the finder tool and drag it to to the required application,you may see that apllication highlighting athe part we are finding the info.

2.Now you can see all the information of the notepad in the finder info above image
highlight information

Finding Info

1.If we want to perform any action on application first of all we need to activate that application 2.Let us write script to activate the notepad win activate command


		WinActivate("Untitled - Notepad")
		


3.To run the script goto Tools->Goto
wrun scripts

4.AutoIt activates Notepad when notepad becomes active it comes to the front.You may see result in the AutoIt bottom

AutoIt Mouse Actions

In most of the application we have click on a button for handling button we need to use mouse actions

So first of all we need to activate the window or pop up,then only we can proceed with our script.

Click on the Save as button
1.Consider below example where we are clicking on the save as button ,so I found the information with help of Finder Tool and write the command like below
window info

1.1.Navigate to mouse option in the finder Tool
window info

We use MouseClick() method for clicking,MouseClick() clicks on the application if it is opened. window info


//MouseClick method to click 
MouseClick("which button",x,y,No.of clicks,clicking speed)

2.But which button you want to click "Right click or Left Click" ?

2.1.Where to Click ?

2.2.How many clicks you want to click ?

2.3.On what speed you want to click ?

For above Queries below one is the answer.

3.Normally we can set up the buttons as "Primary or Secondary" by default(right hand user) Primay=Left Button,Secondary=Right Button.

Sometimes Left hand users might have changed the button options in control panel ,In situauation we need to give option like as in configuration

3.1.Second one parameter is x coordinate and third parameter is y coordinate

3.2.whether you want single click or double click or triple click

3.3.it specifies the speed of the click

window info

Code for Clicking


						//for comments in AutoIt use # infront of the line 
						//do not use //(double slashes)
						//activates the Save As title(pop up)
						WinActivate("Save As")
						//right clicks on a position 657,564 ,it is single click with speed 1
						MouseClick("Primary",657,564,1,1)

					

But if the application is not opened means AutoIt will give error and also we cannot ensure that because selenium runs faster so there is chance to fail the script if the application takes sometime extra to open the the dialog box.

WinWaitActive

WinWaitActive is another method present in the AutoIt it will wait till the application or pop-up loads then only it performs the action.
This also works in same way as WinActivate in remaining aspects window info

Read & Write Barcode using Selenium Webdriver

Code for Clicking


						//for comments in AutoIt use # infront of the line 
						//do not use //(double slashes)
						//activates the Save As title(pop up)
						WinWaitActive("Save As")
						//right clicks on a position 657,564 ,it is single click with speed 1
						MouseClick("Primary",657,564,1,1)

					

Co-Ordinates in AutoIT

In the above example we have wriiten a code to click on the button with help of coordinates but can we tell surely it will work ??
No,because we cannot say all the time the pop going to open in same position , sometimes it may be somewhere else on the screen so there will be change in coordinates.
In other cases the moniter size also matters I am having small sceen but someone who is having cannot handle the pop-up with same code.

co ordibnate in autoit

How to handle it ?
To handle such scenario we need to used the co ordinates of the pop-up like below
coordinate

To obtain above one follow below steps
1.Open->AutoIt Window Info

2.Select Options->Coord Mode->Window

window in AutoIt

3.Now find Coordinates with help of Finder Tool.

4.It calculates the Pop as (0,0) coordinates and calculates the distance for the button(497,467)

5.Now we changed the mode in the AutoIt Window Info application

6.We have to inform the script that we are using window co-ordinates

7.For that we need to include below line in to our script


		AutoItSetOption("MouseCoordinateMode",0)
	


7.1.If you want info about MouseCoordMode please go through the help document Mouse coordinate mode in AutoIt

8.Sample program
Mouse coordinate mode in AutoIt

Code Example for Co-Ordinates in AutoIT


						//for comments in AutoIt use # infront of the line donot use //
						//activates the Save As title(pop up)
						AutoItSetOption("MouseCoordMode",0)
						WinWaitActive("Save As")
						//right clicks on a position 657,564,it is single click with speed 1
						MouseClick("Primary",530,470,1,1)

					

Key Strokes

In most of the applications we have to enter text on text fields like while saving an image we need to give name for it and location for it.
For Handling Key Strokes we are using send("key sequence")

1.First activate the window

2.Find the Co-ordinates of the text field

3.Click on that text Field using MouseClick

text bar in autoit

4.Send the Keys using Send() method


		Syntax: Send("hello)
		


sending keys in autoit


AutoItSetOption("MouseCoordMode",0)
WinWaitActive("Save As")
MouseClick("Primary",298,406,1,1)
Send("hello")
		


In Some Cases you might be sending an "Enter" key from keyboard in that cases we need to use "{}" to send the keys


AutoItSetOption("MouseCoordMode",0)
WinWaitActive("Save As")
MouseClick("Primary",298,406,1,1)
Send("hello")
Send("{Enter}")


Now send the text to the notepad when it opens


sending keys in autoit


AutoItSetOption("MouseCoordMode",0)
WinWaitActive("Untitled - Notepad")
MouseClick("Primary",20,20,1,1)
Send("I am note pad")
Send("{Enter}")
Send("{Enter}")
Send("there is one empty line is there between me and first line")

Ways to Click a button in AutoIT

In most of the applications we have to enter text and Clicking Button.
For Handling Key Strokes we are using send("key sequence" and for Clicking we used MouseClick() methods . So far we have seen methods, those are based on co-ordinates but we cannot say 100% it is going to be same co-ordinate all the times

In the below example we are going to see ways in which we can send keys or clicking on a button.

Advanced AutoIt

1.WinActivate()

In this method we will activate the window using WinActivate() method based on their title and then we will click using MouseClick() method based on the co-ordinates
click using WinActivate method

Download the selenium server using AutoIt(just Pop-up)
1.Goto "https://seleniumhq.org/download/"

2.Click on The version link

downlaod selenium with AutoIt

3.It opens a Dialog box .
pop up in autoit

4.Handle that dialog box with selenium

	
WinActivate("Opening selenium-java-2.45.0.zip")
AutoItSetOption("MouseCoordMode",0)
MouseClick("Primary",290,160,1,1)

2.ControlClick()

In this method we will not activate the window but we will click on the particular object either button or text field based on their id or Class+instance.please not that we did not use WinActivate() in this. Important:to use this method please Uncheck the "use Spy" option from AutoIt Window Info application.

ControlClick("title of the window",'',"ID of the element")

Handle the Save As Dialog Box.
1.Inspect the Button

save as pop up in autoit

2.Click On the Save Button

save as pop up in autoit

	
ControlClick("Save As",'',"Text:&save;ID:1")
	

I hope it works for every one.

3.Send()

In this method we will activate the window using WinActivate() method based on their title and then we will click using Send() method based the keys like how many tabs and then enter

	
WinActivate("title of the window")
Send("{tab}{Enter}")
	

Download the selenium server using AutoIt(just Pop-up)
1.Goto "https://seleniumhq.org/download/" 2.Click on The version link

downlaod selenium with AutoIt

3.It opens a Dialog box .

pop up in autoit

4.Handle that dialog box with selenium

5.Note check the active element then press the tabs and enter manually once then write the program(for this pop up active element is Cancel button as you can see the blue line over the button)
send keys to pop up in autoit

	
WinActivate("Opening selenium-server-standalone-2.45.0.jar")
Send("{TAB}{TAB}{ENTER}")

Convert .au3 to .exe in AutoIT

Normally we write script in AutoIt whenever we need to handle the pop-up or some window application.
But when you save the AutoIt script it saves as .au3 file, you can execute your .au3 files only in systems where you have AutoIt installed but in practical we do not install any AutoIt all the system then how to run in those systems.


For this we need to convert the file to a format which can be executed in all the systems.
Everyone knows that .exe file works in all the systems,without using any application.

Steps to convert the .au3 to .exe file

1.Write your script in AutoIt

program in autoit

2.Goto Tools->Compile

Convert .au3 to .exe in autoit

3.Now goto the Location where you are saving your .au3 files

converted to .exe in autoit

4.Now you can see an .exe file in same location

5.Double click on the .exe file ,It runs and does the same action as we did it in Tools->Go

6.You can also open the .exe application by RightClick->Open

open .exe file

<

Invoke AutoIt

To use the AutoIt .exe file in our selenium programs we need to invoke the .exe file during the execution time .

Steps to handle selenium server download pop-up

1.Write the script to handle the dialog box in your AutoIt

save as pop up in autoit

2.Convert the file to .exe file

save as pop up in autoit

3.Write you selenium programs till the clicking on the link

save as pop up in autoit

4.Write the below code to invoke the AutoIt .exe file and it throws IOException

Runtime.getRuntime().exec("C:\\Users\\path\\a IT\\script\\selenium_download_autoit.exe");

5.Total code may look like this

Code Example for AutoIT Invocation in Selenium


						import java.io.IOException;

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

public class SeleniumDownload 
{
	public static void main(String[] args) throws IOException 
	{
		WebDriver driver=new FirefoxDriver();
		driver.get("https://seleniumhq.org/download/");
		driver.findElement(By.xpath("//p[contains(text(),'Download version')]//a[text()='2.45.0']")).click();
		Runtime.getRuntime().exec("C:\\Users\\script\\selenium_download_autoit.exe");
	}

}

					

Headless browser in Selenium webdriver

Handle Notepad with AutoIT

Scenario: open notepad and enter "This is test Text" and save notepad

Open NotePad


						Syntax: Run("notepad.exe")
						


Wait for notepad to open using its title


						Syntax: WinWaitActive("Untitled - Notepad")
						


Once notepad is opened curser in notepad points in edit area, now send the text you need to enter


						Syntax: Send("This is test Text")
						


Now Close the notepad, so that we get do you want to save option


						Syntax: WinClose("Untitled - Notepad")
						


Wait for the do you want to save popup


						Syntax: WinWaitActive("Notepad", "Save")
						


Press key based on the option, windows underscores the key in an option


						Syntax: Send("!n")
						


Code Example for Notepad Handling in AutoIT


						Run("notepad.exe")
						WinWaitActive("Untitled - Notepad")
						Send("This is some text.")
						WinClose("Untitled - Notepad")
						WinWaitActive("Notepad", "Save")
						Send("!n")

					

Drawbacks of AutoIT


Below are few drawbacks

    We can convert the .au3 files to .exe files only
    We can use .exe files in windows only
    We cannot run .exe files in linux without using a other application
    Python
    .exe does not work in mac

Take Web Page Screenshot and Compare Screenshots in selenium

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
Copyright © CherCher Tech