OptiTest / Tests

Automated Tests for Optify
2 stars 0 forks source link

Handling dialog box #120

Open oras opened 11 years ago

oras commented 11 years ago

Selenium can't handle OS dialog box, so we can't handle the Save as dialog. one way to do it, by using AutoIt scripting tool for windows.

oras commented 11 years ago

Example:

;--------------------------------------------------------- ;~ Save_Dialog_FF.au3 ;~ Purpose: To handle the Dowload/save Dialogbox in Firefox ;~ Usage: Save_Dialog_FF.exe "Dialog Title" "Opetaion" "Path" ;~ Create By: Gaurang Shah ;~ Email: gaurangnshah@gmail.com ;----------------------------------------------------------

; set the select mode to select using substring AutoItSetOption("WinTitleMatchMode","2")

if $CmdLine[0] < 2 then ; Arguments are not enough msgbox(0,"Error","Supply all the Arguments, Dialog title,Save/Cancel and Path to save(optional)") Exit EndIf

; wait until dialog box appears WinWait($CmdLine[1]) ; match the window with substring $title = WinGetTitle($CmdLine[1]) ; retrives whole window title WinActive($title);

; if user choose to save file If (StringCompare($CmdLine[2],"Save",0) = 0) Then

WinActivate($title) WinWaitActive($title) Sleep(1)

; If firefox is set the save the file on some specific location without asking user. ;Save the File send("{TAB}") Send("{ENTER}") if ( StringCompare(WinGetTitle("[active]"),$title,0) = 0 ) Then WinActivate($title) send("{TAB}") Send("{ENTER}") EndIf

;if firefox is set to prompt user for save path. if WinExists("Enter name") Then $title = WinGetTitle("Enter name") if($CmdLine[0] = 2) Then ; If user hasn't provided path to save ;save to default path. WinActivate($title) ControlClick($title,"","Button2")

Else ; If user has provided path ;Set path and save file WinActivate($title) WinWaitActive($title) ControlSetText($title,"","Edit1",$CmdLine[3]) ControlClick($title,"","Button2") EndIf

Else ;Firefox is configured to save file at specific location Exit EndIf

EndIf ; do not save the file If (StringCompare($CmdLine[2],"Cancel",0) = 0) Then WinWaitActive($title) Send("{ESCAPE}") EndIf

oras commented 11 years ago

Example calling for the AutoIt script:

/**

import org.testng.annotations.*; import com.thoughtworks.selenium.DefaultSelenium; import com.thoughtworks.selenium.Selenium;

public class Handle_Dialogs { private Selenium selenium;

@BeforeClass public void startSelenium() { selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.autoitscript.com/"); selenium.start(); }

@AfterClass(alwaysRun=true) public void stopSelenium() { this.selenium.stop(); }

@Test public void handleSaveDialog() throws Exception { String[] dialog;

selenium.open("/site/autoit/downloads/"); selenium.waitForPageToLoad("3000"); selenium.click("//img[@alt='Download AutoIt']");

Thread.sleep(2000); String browser = selenium.getEval("navigator.userAgent"); if(browser.contains("IE")){ System.out.print("Browser= IE "+browser); dialog = new String[]{ "Save_Dialog_IE.exe","Download","Save" }; Runtime.getRuntime().exec(dialog); } if(browser.contains("Firefox")){ System.out.print("Browser= Firefox "+browser); dialog = new String[] { "Save_Dialog_FF.exe","Opening","save" }; Runtime.getRuntime().exec(dialog); } }

}