How To Upload Files Using AutoIt In Selenium | How To Handle Windows Pop Up Using AutoIt

In this tutorial, we will learn How to use AutoIt in Selenium and How to upload files using AutoIt.

What is AutoIt?

AutoIt is an open source tool that is use for perform windows based operation like handle window alert. Also we can use in combination of mouse-movement, keystrokes operations. We are well known that selenium web driver only work on web based related application. So when we face window alert then we have to use this tool. It is also using in to uploading the files which we are going to discuss.

How to upload files using Autoit in Selenium

Step by Step How to use AutoIt in Selenium:

  1. Download and Install AutoIt.
  2. Open AutoIt identifier and find attribute of the elements like title, class and instance.
  3. Open AutoIt editor and write some code to perform select file, mouse click operation.
  4. At the end write code in selenium script.

 

  1. Download and Install AutoIt –

         For download AutoIt, use this link- Download AutoIt

Download Autoit

 

By clicking on Download AutoIt, It will be downloaded then clicking on “autoit-v3-setup.exe” setup, it will be installed. Now go to installed folder (In Program files)  we can see SciTE.exe file. By clicking on this we can see AutoIt editor and we can see Au3Info.exe, that is AutoIt Identifier to finding the attribute of the elements.

 

  1. Open AutoIt identifier and find attribute of the elements like title, class and instance:If I want to upload a file then first I clicked on “Upload File” button then a window popup will be opened to select file from your computer. Here we will use AutoIt identifier to find window popup element attributes.

 

By using finder tool (Click on Finder tool with holding left mouse button and drag it on file name edit box ) find the attributes(Title, Class, Instance) of File name edit box. See in below image we have found these attributes are:

Title —> Open
Class –> Edit
Instance –> 1

AutoIt Identifier

 

Same like we will drag finder tool on “Open” button in window popup and find attributes of this button:
Title –> Open
Class –> Button
Instance –> 1

 

  1. Now, Open AutoIt editor and write some code to perform select file, mouse click operation.a) Click on SciTE folder to open autoit editor.

autoit editor

b) Click on SciTE file. Now Editor will be open.

 

In this we will write 3 lines code. In this we will use “ControlFocus”, “ControlSetText” and “ControlClick” methods.

ControlFocus(“title”,”text”,”ControlID”);
ControlSetText(“title”,”text”,”ControlID”,”File Path which we want to upload”);
ControlClick(“title”,”text”,”ControlID”);

 

a) Write code for “File name” text box.

– First attribute is ‘title’ which is ‘Open’.
– Second attribute is ‘text’ which is not required.
– Third attribute is ‘ControlID’ which is Edit1 (combination of Class and Instance);

 

b) Write code for select file which we want to upload using ControlSetText method

– First attribute is ‘title’ which is ‘Open’.
– Second attribute is ‘text’ which is not required.
– Third attribute is ‘ControlID’ which is Edit1.
– Fourth is path of file which we want to upload.

 

c) Write code for click Open button.

– First attribute is ‘title’ which is ‘Open’.
– Second attribute is ‘text’ which is not required.
– Third attribute is ‘ControlID’ which is Button1. (Here controlid is Button1)

 

Now save this editor file with .au3 extension.

After save this file, Compile it by right click on it. (Compile script on the basis of your OS bit. ). After compilation a .exe file will be generated.

Compile Autoit File

 

Now open the eclipse and write the code for upload file.

1. Write the code for click on “upload file” button.

driver.findElement(By.xpath(“locator of upload file button”)).click();

2. Write code for run the .exe file which we have created above.

Runtime.getRunTime.exec(“C:\\Users\\Aj\\New folder\\FileUpload.exe”);


How To Upload File in Selenium WebDriver without using AutoIT:

There are two other methods to upload the file without using Selenium.

  1. Using sendkeys
  2. Using Robot class

#Using sendKeys method:  This is basic way to upload the file in selenium.

Syntax:

WebElement uploadfile = driver.findElement(By.xpath(“//input[@id=’file’]”));

uploadfile.sendKeys(“C:/Users/Ajeet/Desktop/upload.png”);

 

#Using Robot Class: We can perform the various activities using Robot class such as mouse functions, keywords functions and many more.

Read below link to upload files using Robot Class.

Upload files using Robot Class


How To Handle Window Authentication Popup using AutoIt:

Sometimes when we navigate any website which contains window authentication popup which ask for Username and Password so while running selenium script we cannot navigate to parent window.

Now we will discuss How to handle window authenticate popup. There are two ways to handle it.

1.We can provide credentials in URL itself. We will add username and password in URL so while running script it will bypass the same.

Example:

http://ajeetmaurya:[email protected]

2. In this approach we will use AutoIt which we have already discussed. Please read above section for AutoIt.

Summary:

In this post, we have covered ‘How To Upload Files Using AutoIt’.

We have also covered – How to handle window authentication popup using AutoIt, How to upload file in Selenium WebDriver without using AutoIT.

I am sure this content added some additional value in your skills and also helpful to preparation of your interviews.

Final word, Bookmark this post How To Upload Files Using AutoIt for future reference.

If you have other questions or feedback, the comment section is yours. Don’t forget to leave a comment below!

 

Leave a Comment