Automating File Downloads with Watir and AutoIT: A Step-by-Step Guide

DevOps & Testing

December 28, 2010

Handling file download prompts can be tricky in automated testing, especially when dealing with modal dialogs or dynamic filenames. AutoIT, when combined with Ruby and Watir, is an effective tool for overcoming these challenges by simulating user actions without requiring access to the source code.

In this guide, we’ll walk through automating file downloads in Firefox and Internet Explorer using Watir and AutoIT, exploring key functions like WinActivate, WinGetTitleWinGetText

Automating File Downloads in Firefox

Imagine you’re testing an application that requires downloading a file in Firefox. AutoIT can simulate user interactions with the download prompt to handle this seamlessly.

Example: Activating the Download Prompt

require 'rubygems'
require 'watir'
require 'win32ole'

ai = WIN32OLE.new("AutoItX3.Control")
ai.WinActivate("Opening")

This activates the download prompt window. Ensuring the correct window is active is crucial for sending commands or retrieving information.

Example: Capturing the Window Title

downloadtitle = ai.WinGetTitle("")

This retrieves the title of the active window, which is especially useful when dealing with dynamic filenames. For instance, if the title is Opening Lead_1286919229280.csv, the downloadtitle variable can later help access the file.

Example: Initiating the Download

ai.Send("{ENTER}")

Simulate pressing the ENTER key to initiate the file download.

Confirming the Download

downloadConfirm = ai.WinExists("Downloads")

An output of 1 confirms that a window titled “Downloads” exists, indicating the download was successfully initiated.

Extracting the File Name

To extract just the filename (e.g., Lead_1286919229280.csv), manipulate the downloadtitle variable:

Filename1 = downloadtitle[8...downloadtitle.length]

Automating File Downloads in Internet Explorer

File download prompts in Internet Explorer require slightly different handling.

Simulating the Save Option

Unlike Firefox, you need to simulate a left arrow key press before hitting ENTER to choose the “Save” option:

ai.Send("{LEFT}")
ai.Send("{ENTER}")

Capturing the File Name

The title of the download window in IE doesn’t contain the file name. Instead, use WinGetText to retrieve all text from the window:

text = ai.WinGetText("File Download", " ")
Filename2 = text[45...67]

This approach captures the filename from the window text.

Best Practices for Using AutoIT with Watir

  1. Window Focus: Always ensure the correct window is active before interacting with it.
  2. Dynamic Filenames: Use WinGetTitle or WinGetText to handle filenames that change dynamically.
  3. Cross-Browser Compatibility: Adapt scripts to address differences in browser prompts (e.g., Firefox vs. Internet Explorer).
  4. Error Handling: Incorporate checks like WinExists to confirm that expected windows or dialogs are present.

Why Use AutoIT with Watir?

AutoIT enhances your test automation by enabling interaction with elements outside the DOM, such as file download prompts and modal dialogs. It’s a lightweight yet powerful addition to your automation toolkit, making complex scenarios manageable without relying on source code.

By combining Watir’s browser automation capabilities with AutoIT’s window manipulation, you can achieve robust and efficient test automation workflows.

Are you using AutoIT in your automation projects? Share your experiences and tips in the comments below!