Upload File In Selenium Webdriver Using Autoit With Selenium

Posted onby
  1. How To Download File In Selenium Webdriver
  2. Upload File In Selenium Webdriver Using Autoit With Selenium C#

In our testing, many times we need to upload or download files and verify that upload or download functionality are working fine or not. In this post, we will see how can we manage the file download using selenium WebDriver.

What is AutoIt?

AutoIt v3 is also freeware. It uses a combination of mouse movement, keystrokes and window control manipulation to automate a task which is not possible by selenium webdriver.

In this tutorial, you will learn-

Why Use AutoIt?

Selenium is an open source tool that is designed to automate web-based applications on different browsers but to handle window GUI and non HTML popups in application. AutoIT is required as these window based activity are not handled by Selenium.

Moving ahead we will learn how to upload a file in selenium web driver using autoIT. Here we need three tools in order to this.

  • Selenium Webdriver
  • AutoIT editor and element identifier
  • The window that you want to automate

How to download and install AutoIT

Step 1): Go to this link.

Step 2): Hover on 'Autoit' and 'Autoit Editor' dropdown.

Step 3) Click on 'AutoIT' Downloads option.

Step 4): Download 'Autoit' by clicking on 'Download Autoit' button .

Step 5): Now download 'Autoit editor' by clicking on 'Downloads' button .

Step 6): Click on the link as shown below.

After download you will get two setup file as shown in below screen, first is AutoIt version 3 setup and second is Scitautoit3 .

Step 6): For Installing AutoIT-Click on both AutoIT setup one by one .

Step 7): After successfully installation - open up AutoIT Editor.

Go to 'C:Program Files (x86)AutoIt3SciTE'

and click on 'SciTE.exe' file, the AutoIT editor opens as shown in below screen.

Step 8) : Now opens element Identifier .

Go to 'C:Program Files (x86)AutoIt3 '

And click on 'Au3Info.exe' file, the element identifier opens as shown in below screen.

Note: Once you done with this element identifier you need to close manually, it will not close automatically.

Finding element through element Identifier and writing script on AutoIT editor.

Under this, we will see how to find element on file uploader window through AutoIT Element Identifier (Element identifier is a tool like selenium IDE, identifier find the element of window GUI or non HTML popups and provide the attribute of element like title, class, instance ) and how to write script on AutoIT editor using 3 methods.

For Example: We will use 'Write to us' page of guru99 to upload resume ( Doc file).

After clicking on 'Choose File' button from the 'Write to us' page, we need to call AutoIT script. The control immediately transferred to autoit after clicking 'Choose File' by the below statement which takes care of uploading part.

Finally, when we run selenium script-it will fill the form-> upload resume-> Submit form.

Step 1): Now open element Identifier- Go to 'C:Program Files (x86)AutoIt3' and click on 'Au3Info.exe' file, the element identifier window opens as shown in below screen.

Step 2): Now open file uploader window by clicking on 'Choose File' which is windows activity.

Step 3): Drag the finder tool on the ' File Name' box element of file uploader window to find the basic attributes info as shown in the below screen with the arrow.

We can get the value of attributes i.e. title='Open', class='Edit' and instance='1' as shown below. These values are used in writing AutoIT script as explained in below step 4.

Step 4): Now open AutoIT script editor, goto 'C:Program Files (x86)AutoIt3SciTE' and click on 'SciTE.exe' as shown in step 7 from the 1st topic.

Start writing a script for selecting a file to upload.

There are lots of method available which we can use in a script according to the requirement, but right now we will focus on the below methods as these methods are required for writing file upload script:

  1. ControlFocus(' title ',' text ',controlID ) //Sets input focus to a given control on a window.
  2. ControlSetText(' title ',' text ',controlID ,' File path which need to upload ' ) // Sets text of a control.
  3. ControlClick(' title ',' text ',controlID ) //Sends a mouse click command to a given control.

You can see a number of methods are displayed as shown in below screen. The good feature of AutoIT is that it is somewhat like Eclipse that suggests you some of the methods.

Here in the AutoIT editor, we have selected 'control focus' method. Element identifier is already opened and minimized as the element is already identified in above step 3. We can open it by maximizing it.

Now, we will take the values from element identifier for 'ControlFocus' and 'ControlSetText' methods as these methods works on same element i.e. 'File name' text box but for 'ControlClick' method need to capture values of different element i.e. 'Open' button.

Parameter values for ControlFocus method:

This method sets focus to the 'file name' text box of the file uploader window.

  • 1st parameter title is ' Open '.
  • We ignore 2nd parameter, the text is not required.
  • 3rd parameter controlID is the combination of and Instance='1' i.e., . 'Edit1.'

Parameter values for ControlSetText method :

This method is used to define the path of a file which we need to upload in 'file name' text box. In another way, we can say that this method is used to set the text to the input element.

  • 1st parameter title is ' Open '.
  • We ignore 2nd parameter, the text is not required.
  • 3rd parameter controlID is the combination of and Instance='1' i.e., ' Edit1 '.
  • 4th parameter new text, we pass the path of the file which we need to upload.
Upload File In Selenium Webdriver Using Autoit With Selenium

After following the above step, don't close the windows (editor and element identifier), keep it remain open. You again need to open file uploader window as to find attributes of 'Open' Button as shown in below step 5.

How To Download File In Selenium Webdriver

Step 5): Now drag the finder tool on the 'Open' button element of file uploader window to find the basic attribute information.

Previous values ( i.e. attributes of 'File name' text box) overwrite with new values of 'Open' button. You can see the class attribute is now changed to 'button' which was previously 'edit' in AutoIT element identifier window.

We can get the value of attributes i.e. title='Open', class='Button' and instance='1' as shown below. These values are used in writing Autoit script as explained in below.

Parameter values for ControlClick method:

This method clicks on 'Open' button of the file uploader window.

  • 1st parameter title is ' Open '.
  • We ignore 2nd parameter; the text is not required.
  • 3rd parameter controlID is the combination of class and Instance, i.e., ' Button1 '.

Step 6): You can see in below screen that AutoIT script is completed to handle file uploader.Now you can close the element identifier and save the script as ' FileUpload ' at the given location ( E:AutoIT ).

Now you can't execute this script directly, you need to compile this script.

For compiling this script, you have two options ' compile script x64 ' and ' compile script x86 ', if you have windows 32-bit machine then u go with ' compile script x86 ' and for windows 64-bit machine then u go with ' compile script x64 .'

Step 7):'FileUpload exe' file generated after compilation, you can see in the below screen. Now we can use this file in Selenium webdriver script.

Upload File In Selenium Webdriver Using Autoit With Selenium C#

Now we will use this AutoIT script in Selenium web driver. Check below for output.

AutoIT Upload file in Selenium Webdriver

In Selenium script, we find the elements of the form and fill the data in each element as required and upload 'resume.doc' file by executing AutoIT exe file generated from AutoIT script and then allow to submit the form in selenium script.

  • Open Eclipse and start writing code.
  • When selenium clicks on Choose File button, file uploader box opens.
  • Then we need to call AutoIT script, the control immediately transferred to AutoIT in order to upload a file and then control send back to selenium as shown below.
Upload File In Selenium Webdriver Using Autoit With Selenium

Step 1):Develop selenium script in eclipse.

  • Runtime class allows the script to interface with the environment in which the script is running.
  • getRuntime() get the current runtime associated with this process.
  • exec() methods execute the AutoIT script ( FileUpload.exe ) .

above line will call AutoIT script in selenium and upload file .

Step 2) : Execute the Selenium script in Eclipse.

Step 3): Verify the output, resume.doc file uploaded successfully and thank you message will be displayed.

Conclusion:

  • Downloaded and installed Element Identifier and AutoIT editor.
  • Opened the site on which to do the operation.
  • Element Identifier identifies the elements of file uploader window.
  • Prepared AutoIT script in the editor with the help of Element identifier.
  • Autoit script is used in selenium webdriver script.
  • Executed the selenium script.
  • Output: Successfully the file uploaded.

This article is contributed by Gaurav Nigam

Pop-ups are those irritating windows that come up while we work on something important and interrupt our concentration. It is necessary to get rid of them while testing.

In this article, we shall discuss how to handle window pop-ups in automation testing using AutoIt.

This tutorial covers:

  • How to handle window pop-ups in Selenium WebDriver,
  • Uploading or downloading the files or images by transferring our control from Selenium WebDriver to AutoIt
  • How to call the AutoIt script from our program

But first, let’s understand what is AutoIt, how to install and use it.

What You Will Learn:

What is AutoIt?

AutoIt V3 is a freeware tool which is used for automating anything in Windows environment. AutoIt script is written in a BASIC language. It can simulate any combination of keystrokes, mouse movement and window/control manipulation.

Through AutoIt, we can prepare scripts for our routine actions like file input/output operations, application handling, resources monitoring, and administrative tasks and so on. However, in this article, we shall limit our discussion to handling window pop-ups in Selenium WebDriver.

While doing automation through Selenium or through any other tool for that matter, we all encounter a common problem, windows pop-ups. As Selenium is confined to automating browsers, desktop window is out of scope. Web applications sometimes need to interact with the desktops to perform things like file downloads and uploads. There are tools available for automating these sorts of workflow such as AutoIt, Robot Framework, Silk Test etc.

We can upload or download the files or images by transferring our control from Selenium WebDriver to AutoIt. We need to explicitly call the AutoIt script from our program.

Also, read => How to Handle Alerts/Popups in Selenium WebDriver

AutoIt Download and Installation – Step by step Guide

AutoIt Download: Click here to download AutoItV3 current version and AutoIt Script Editor.

Once downloaded, install the AutoItV3 setup on your system. Follow below steps to install AutoIt.

Step #1 Click on the setup file

Step #2 – Accept the license agreement

Step #3 – Choose 64 bit or 32-bit options based on your operating system: I choose 64 bit

Step #4 – Click on next and select what operation you want to perform by double-clicking: I choose run the script

Step #5 – Choose all the components that are required and click next. You can also choose default where all are checked

Step #6 – Choose file installation location and click on Install. It will take a few seconds to install. Once done, install the script editor

AutoIt Script Editor – Installation

Download the SciTE.exe and install; it is an editor which helps in finding the commands.

How to write AutoIt script:

  • Identify the Windows control, through AutoItV3 Windows Info tool for 64 bit or 32 bit depending on your operating system
  • Click on Finder Tool and mouse hover over the object for which you want the properties
  • It will capture the properties of pop-up like Title, Class, Position, Size, Style, Handle and so on

AutoIt V3 Window Info – finder tool

  • Then with the help of above-captured properties, write a script in SciTE script editor or in notepad and save the script with .au3 extension
  • Now compile the .au3 script using AutoIt Script to EXE converter, which converts .au3 file to .exe file
  • In that editor provide source and destination folder location and click on convert button, it will create a .exe file

AutoIt Script to Exe converter:

  • Else right click you’re saved .au3 script, it will show compile options. Select compile to 64bit or compile to32 bit option and will create a .exe file in the same folder.
  • Wherever you encounter download/ upload pop-up window in your Selenium test case, execute the .exe file
  • Syntax to call .exe file in your script is: Runtime.getRuntime().exec(“path of exe file”);

Download pop-up example:

How to download the file?

You can use the below AutoIt script to handle the download popups:

We have already captured the file download popup properties like Tile, Class, Position, and Size and so on in our previous steps. Now build an AutoIt script using identified windows control:

Upload pop up example:

How to upload the file?

You can use the below AutoIt script to handle the Upload window popup:

Build an AutoIt script using identified windows control:

Save and compile this script and execute that .exe file in your selenium script where we need to upload the file.

Login Pop-Up Window Example

How to enter username and password to the above login pop-up alert window?

You can use the below AutoIt script to handle the login popup:

How to test if your AutoIt script is showing expected results or not without integrating it in your selenium script?

Follow below steps:

  • Before compiling your script double click .au3 file – it will show the errors in your script
  • If no errors are found then convert your script into a .exe file
  • Manually generate the file download pop scenario
  • Now the pop is available and click on the .exe file, it should be able to accept the pop-up and download the file

Disadvantages of AutoIT:

  • It works only in Windows operating system
  • Knowledge of fundamental coding principles is a must
  • It is great tool for professional but bit complicated for beginners

What else can we use AutoIt for?

Apart from handing windows pop-ups, we can use AutoIt to automate your each and every windows operation like file search, copy file from one location to other, installation of software and so on: Refer below examples –

The script for file search in your computer:

Save and compile this script and execute that .exe file in your current working directory. It will search all the files which start with “How” and display a message box.

Further resources: Learning to Script with AutoItV3 here and here – basic getting started guides to start using AutoIt scripting.

Important aspects you should keep in mind while working with AutoIt:

  • Do not forget to save your script with .au3 extension
  • Compile your script and create a .exe executable file
  • The pop-up or anything that you want to handle should be present beforehand so that the .exe file can work on it

Conclusion:

We can use AutoIt to automate anything in a Windows Operating System environment. It is a script that is written in a BASIC language and can handle any type of window pop-ups which we encounter while doing automation testing. It can kindle various things such as combinations of keystrokes, mouse movement and window/control manipulation.

Using AutoIt, we can also handle any interaction issues between Selenium WebDriver and Windows.

Here, we learned how to upload/download file pop-up, but there are also other issues such as browser authentication popup, file search in a particular directory, etc. We can handle all these issues by using AutoIt tool.

You can also write scripts through AutoIt to start a task at a particular time, to schedule a task, to copy a file from one server to another and so on.

Do you have any tips/experience/questions to share on AutoIt tool? Let us know in comments below.

Recommended Reading