Cogitek RIATest 3 Documentation Copyright © Cogitek Inc.

Handling Windows dialogs for Download/Upload process

RIATest is not capable of directly interacting with Windows Open/Save dialogs which appear during download/upload operations. However it is possible to use free third-party tools to automate this part of your tests.

This sample shows how to use the free AutoIt automation tool.

The technique is as follows. We will use RIATest script to locate the "Start Download" button that we want to be clicked to initiate the download process. The RIATest script will calculate the X,Y position of this button and will execute the AutoIt tool passing it the position of the button to click and the file name to save to. The AutoIt script will first find the browser window, then Flash Player inside the window, then will emulate a mouse click at the position supplied by RIATest and will interact with the Save dialog.

First, here is the RIATest script that finds the button position and calls AutoIt:

// Calculate position of click (center of the button)
var btn = FlexButton("Start Download");
var x = btn=>x + btn=>width/2;
var y = btn=>y + btn=>height/2;

// Generate temporary file name to download to
var tempDir = getEnv("TEMP");
var tempFileName = tempDir+"\\downloadfile.html";
trace("Downloading to "+tempFileName);

// Call AutoIt to automate download click initiation and Windows save dialog handling.
// Command line parameters for Download.au3 script:
// 1 - X offset to click
// 2 - Y offset to click
// 3 - File name to save to
// 4 - Browser to use: ie or firefox
var p = new Process('"C:\\Program Files\\AutoIt3\\AutoIt3.exe" Download.au3 '+x+' '+y+' "'+tempFileName+'" ie');

// Wait for AutoIt to execute
waitfor(p.isTerminated());

// Check exit code
verifyEqual(p.getExitCode(),0);

You will need to have AutoIt installed at the default location or edit the above code to point it to the correct location of AutoIt3.exe.

Next, AutoIt script that will do its part of the job (save this to Download.au3 file that will be located in your RIATest project directory):

#include <WinAPI.au3>

if ($CmdLine[0]<4) then
	Exit 1;
EndIf

; Get command line parameters
$ofsX = $CmdLine[1]
$ofsY = $CmdLine[2]
$saveFileName = $CmdLine[3]
$browser = $CmdLine[4]

if $browser="ie" Then
	; Parameters for IE
	$browserTitle = "Windows Internet Explorer"
	$playerClass = "MacromediaFlashPlayerActiveX"
else
	; Parameters for Firefox
	$browserTitle = "Mozilla Firefox"
	$playerClass = "MozillaWindowClass"
EndIf
	
; Match mode - substring
AutoItSetOption("WinTitleMatchMode",2);

; Activate browser window
WinActivate($browserTitle)

; Find Player control
$playerHwnd = ControlGetHandle($browserTitle, "",  "[CLASS:"+$playerClass+"]")
if @error=1 Then
	Exit 1;
EndIf

; Get Player control position
Local $tpoint = DllStructCreate("int X;int Y")
DllStructSetData($tpoint, "X", 0)
DllStructSetData($tpoint, "Y", 0)
_WinAPI_ClientToScreen($playerHwnd, $tpoint)

$playerLeft = DllStructGetData($tpoint, "X")
$playerTop = DllStructGetData($tpoint, "Y")

; Click on component that will start download. Note that offsets are fragile and may break if you change component location
MouseClick("left",$playerLeft+$ofsX,$playerTop+$ofsY);

; Wait 10 seconds for dialog to appear
if (WinWait("Select location for download","",10)=0) then
	Exit 2;
EndIf	

; Enter file name
if (ControlSetText("Select location for download", "", "[CLASS:Edit]", $saveFileName)=0) Then
	Exit 3;
Endif

; Click Save
if (ControlClick("Select location for download", "", "[CLASS:Button; TEXT:&Save]")=0) Then
	Exit 3;
Endif

; Wait for overwrite dialog to appear
Sleep(1000)

; If overwrite message appears click Yes
ControlClick("Select location for download", "", "[CLASS:Button; TEXT:&Yes]");

This AutoIt script should work correctly for Internet Explorer and Mozilla Firefox browsers. You can use a similar technique to automate the file Upload process.


Found a typo? Have a suggestion? Please submit your request here.