Open In App

Use of AutoIt in Selenium WebDriver

Last Updated : 29 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

AutoIt is a scripting language used to automate the Windows GUI and general scripting tasks on the Windows platform. AutoIt can be used with Selenium WebDriver to handle scenarios where automation involves interactions with Windows-based GUI elements. This article focuses on discussing the use of AutoIt in Selenium WebDriver.

Need of Third-party Tools in Selenium

Here are some reasons why third-party tools are needed in Selenium:

1. Handling Native Windows Dialogs

Selenium WebDriver is designed for automating web applications. When dealing with native window dialogs third-party tools like AutoIt can be integrated to automate interactions that Selenium cannot handle natively.

  1. File Uploads: Selenium does not work with the application file uploading option dialog box. Such GUI dialogs are handled using tools such as AutoIt or Robot Framework.
  2. File Downloads: Likewise, closing file download dialogs is only possible with the help of additional software or browser profiles.

2. Interacting with Non-Web Elements

Selenium WebDriver is designed for automating web applications. When dealing with non-web elements, such as Windows-based pop-ups, file uploads, etc, third-party tools like AutoIt, or Sikuli can be integrated to automate interactions that Selenium cannot handle natively.

  1. Desktop Applications: As for its types, Selenium is restricted to web elements only. In testing situations that require application testing specifically desktop applications; AutoIt Or Sikuli is essential.
  2. System Pop-ups: Those pop-ups that appear outside browser, categorized under the system-level pop-ups require the use of other third parties’ automation tools.

3. Advanced User Interactions

  1. Complex Keyboard and Mouse Actions: AutoIt or PyAutoGUI will be required for complex sequences if Selenium cannot handle them well, though it handles basic interactions well.
  2. Clipboard Operations: Applications such as system clipboard (pyperclip) are better off in charge of copy-paste operations more efficiently than when run by the code.

4. Cross-Platform Automation

While Selenium WebDriver supports multiple platforms, managing cross-platform testing across different versions and configurations can be complex.

OS-Specific Tasks: For certain aspects of the operating system particular only to Windows or macOS, for instance, Windows specific settings or applications, AutoIt (Windows) or AppleScript (macOS) are to be employed.

5. Data-Driven Testing

Implementing data-driven testing can be efficiently managed using tools like Apache POI, ExcelDataReader, or libraries provided by test frameworks such as TestNG or JUnit. These tools enable reading data from external sources (e.g., Excel files, databases) and iterating tests with multiple datasets.

What is AutoIt?

AutoIt is a third party automation tool that is meant strictly for Windows GUI and general scripting. AutoIT, a powerful scripting language that able to automating GUI of Windows removes these Selenium’s limitations.

Features

  1. GUI Automation: AutoIT can simulate keystrokes, mouse movements, and window commands to automate interactions with the Windows GUI. This includes clicking buttons, entering text, and selecting options in native Windows dialogs.
  2. Window Management: It provides functions to manipulate window states, such as minimizing, maximizing, hiding, and showing windows. AutoIT can also wait for specific windows to appear or become active.
  3. File Management: AutoIT scripts can automate file and directory operations, such as copying, moving, renaming, and deleting files. This is useful for tasks like uploading or downloading files during automated tests.
  4. Complex Control Interactions: It can handle complex interactions with controls that are not easily accessible via standard Selenium commands, such as tree views, list views, and menu items in Windows applications.
  5. Integration with Other Tools: AutoIT can be easily integrated with other tools and languages, including Selenium WebDriver, through script execution. This makes it versatile and powerful for hybrid automation frameworks.
  6. Extensive Help and Community Support: AutoIT has comprehensive documentation, including tutorials and examples, along with a supportive community. This makes it easier for new users to get started and for experienced users to find solutions to advanced problems.

Steps to Download AutoIt v3 and AutoIt Script Editor

Download AutoIT

1. Visit the official AutoIT website.

2. Download the AutoIT installer.

Download AutoIt
Download AutoIt

Install AutoIT

1. Run the downloaded installer.

2. Follow the on-screen instructions to complete the installation.

Install AutoIT Script Editor

1. Visit the official AutoIT website.

2. Download the Editor.

Download Editor
Download Editor

3. Follow the steps below to install the Editor.


4. Go To Folder of AutoIT:

  1. Folder Path: `C:\Program Files (x86)\AutoIt3`
  2. Open `AutoIt3Help.exe` for tutorials on script writing in AutoIT
  3. Open Au3Info.exe or element detection, which will be useful for creating scripts.

For more information about AutoIT Window tool Visit.

Au3Info
Au3Info

The SciTE folder contains the AutoIT Script Editor. Open SciTE.exe to write and edit your scripts

Upload File in Selenium with AutoIt

1. Go to the folder where you would like to save your AutoIT script and right click and select New --> Autoit v3 Script

Way for AutoIT v3 Script Creation
Way for AutoIT v3 Script Creation

2. When you have completed your script, you can build it by running the script CTRL+F7 or click ‘tools > compile’ in SciTE. This will generate an executable . exe file in the same directory and clicking it runs the program.

File Upload Script

Save the following script as file-upload.au3:

#cs ----------------------------------------------------------------------------

AutoIt Version: 3.3.16.1
Author: Sanjeev

Script Function:
File upload AutoIt script.

#ce ----------------------------------------------------------------------------


; Wait for the "Open" dialog to become active
WinWaitActive("Open", "", 10)

; Set the file path to upload
Local $filePath = "C:\path\to\your\file.txt"

; Sleep for 2 seconds
Sleep(2000)

; Type the file path into the "File name:" field
Send($filePath)

; Press Enter to confirm the file selection
Send("{ENTER}")

Explanation:

  1. WinWaitActive("Open", "", 10): Waits up to 10 seconds for the "Open" dialog to become active.
  2. Local $filePath = "C:\path\to\your\file. txt”: This assigns the variable $filePath to the path of the file which you want to upload.
  3. Send($filePath): Sends the file path to “File name:” field in the dialog.
  4. Send("{ENTER}"): Hits the main ENTER key to affirm the selection of the file and the shutting down of the dialog.

Compiling the Script

  1. Open the SciTE editor.
  2. Write your script and save it with the name file-upload. au3.
  3. Stop the program and then compile the script by pressing CTRL+F7 or from the menu tools then compile.
  4. This will create an executable file-upload.exe file in the same directory.
Python
from selenium import webdriver
import os
import time

try:
    # Create a new instance of the Chrome driver
    driver = webdriver.Chrome(executable_path="chromedriver.exe")

    # Go to the URL
    driver.get("https://uploadnow.io/en")

    # Find the file input element by ID and click to open the file dialog
    time.sleep(5)
    driver.find_element_by_xpath('''//*[@id="content-scrollable-page"]/div/div[1]/div/div/div[2]/div[2]/div/button''').click()

    # Upload the file using the AutoIT script
    os.system("file-upload.exe")
    time.sleep(5)

    # Click on the upload button to start the upload process
    driver.find_element_by_xpath('''//*[@id="content-scrollable-page"]/div/div[1]/div/div/div[2]/div[3]/div/button[3]''').click()

    # Check if the file is uploaded
    while True:
        try:
            driver.find_element_by_tag_name("input").get_attribute("value")
            break
        except:
            pass

    # Get the link of the uploaded file
    link = driver.find_elements_by_tag_name("input")[1].get_attribute("value")
    print(f"File uploaded successfully, link: {link}")

except Exception as e:
    print(f"An error occurred: {e}")

finally:
    # Close the browser
    driver.quit()

Explanation:

1. Setup the WebDriver:

  • Initialize the Selenium WebDriver instance for Chrome.
  • Navigate to the target URL using driver.get("https://uploadnow.io/en").

2. Trigger File Upload Dialog:

  • Use Selenium to find and click the button or link that triggers the file upload dialog.
  • Example: driver.find_element_by_xpath(...).click().

3. Use AutoIT for File Selection:

  • After triggering the file upload dialog, use os.system("file-upload.exe") command runs the compiled AutoIT script to handle the file selection in the native dialog.
  • This script handles the native file dialog that Selenium cannot interact with.

4. Initiate File Upload:

  • After the file is selected, use Selenium to find and click the button that starts the upload process.
  • Example: driver.find_element_by_xpath(...).click().

5. Verify Upload Completion:

  • Use a loop to check if the file upload is complete by inspecting the presence of certain elements.
  • Example: driver.find_element_by_tag_name("input").get_attribute("value").

6. Extract the Uploaded File Link:

  • Once the upload is complete, retrieve the link or confirmation message.
  • Example: link = driver.find_elements_by_tag_name("input")[1].get_attribute("value").

7. Clean Up:

  • Print the link to confirm the upload and close the browser using driver.quit().

Conclusion

AutoIT is genuinely a powerful instrument for enriching the Selenium and the goal of having full and end-to-end coverage of the application. Therefore, incorporating AutoIT into your automation process enables one to overcome most of the limitations of tools that solely operate on the web interfaces, enhance complex operations, and enhance the efficiency of the test automation overall.


Next Article
Article Tags :

Similar Reads