AutoIt What ? & Why ?

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

Sometimes, we might face scenarios in selenium like automating window popups at that time selenium will not help us to automate such popups, 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 a built-in feature to compile the script and generate a .exe executable file. You do not need any extra add-on to do all these things.

Some window popups 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
  3. download autoit
  4. After downloading go to the downloaded folder and right-click on the autoIt application and select Open and follow the download instructions
  5. install autoit
  6. It may take 2 minutes to install
  7. installing autoit
  8. Click Finish button
  9. Once the installation is complete , Goto Start ->All Programs->AutoIt v3->select sciTE Script Editor
  10. start script editor
  11. Script editor looks Like below one here we are going to write our script to perform an action on the window application
  12. script editor

How to handle two Browser Windows in Selenium

Inspect Window popups 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. The Position of the mouse pointer

6. The 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 application highlighting the 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 a 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 the 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 the 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 do 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) Primary=Left Button, Secondary=Right Button.

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

3.1.Second one parameter is x coordinate and the 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 an error and also we cannot ensure that because selenium runs faster so there is a chance to fail the script if the application takes sometime extra to open 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 the 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)

					

coordinates in AutoIT

In the above example we have written a code to click on the button with the 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 the same position, sometimes it may be somewhere else on the screen so there will be a change in coordinates.


In other cases, the monitor size also matters I am having a small screen but someone who is having cannot handle the pop-up with the same code.

co ordibnate in autoit

How to handle it ?
To handle such scenario we need to use the coordinates of the pop-up like below
coordinate

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

2.Select Options->Coord Mode->Window

window in AutoIt

3.Now find Coordinates with the 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 coordinates

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 coordinates 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 coordinates 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 the Buttons.

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 coordinates but we cannot say 100% it is going to be the same coordinate 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()

With 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()

With 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 the 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 a .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 a .au3 file, you can execute your .au3 files only in systems where you have AutoIt installed but in practice we do not install any AutoIt in the system then how to run in these script 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 go to the Location where you are saving your .au3 files

converted to .exe in autoit

4.Now you can see a .exe file in the 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 the other application
  • Python
  • .exe does not work in mac

Take Web Page Screenshot and Compare Screenshots in selenium

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Protractor Training

new tutorial I am starting new Protractor training batch on Weekends. It is two days course from 01-june-2019 to 02-june-2019 based on class room (BANGALORE)

Interested people can confirm the seat by calling to +91 8971673487 or whatsapp to +91 9003381224

Find the course content : View Content