Search code examples
javaseleniumautoit

How to parametrize an AutoIt script to handle cross-browser window popup titles and files?


My AutoIt script should upload files using a browser:

ControlFocus("Open","","Edit1")
ControlSetText("Open","","Edit1","C:\MWUploads\myFile.pdf")
ControlClick("Open","","Button1")

I'm using above script's compiled executable from Selenium WebDriver for Java:

Runtime.getRuntime().exec(autoItScExecutableDir+"autoitScript.exe");

Depending on browser, title of upload popup window differs (for Google Chrome and Microsoft Edge it's "Open" whereas for Firefox it is "File Upload"). How can I make it work for any file path on any browser, like below?

Runtime.getRuntime().exec(myWinTitle, myLocator, myFileToUploadPath);

//attaching an img for more clarification, when tried as per Answer

enter image description here

Here is the o/p of sysOut from else block-

E:\AOS\src\test\resources\AutoItScripts\dynamicAutoItScript.exe File Upload 
E:\AOS\src\test\resources\FilesToUpload\Greetings.png

here is what happening in actual- enter image description here


Solution

  • You can send parameters to the autoitScript.exe.

    Cross browser differences

    The Upload Window changes based on the browser:

    1. Firefox -> the upload window title is: File Upload
    2. Chrome and Edge -> the upload window title is: Open
    3. Internet Explorer -> the upload window title is: Choose File to Upload

    However these remain the same:

    • The location bar (ToolbarWindow32 on windows, the one on top)
    • File Name bar
    • Open Button

    The AutoIt script is:

    Sleep(500);
    If $CmdLine[0] > 0 Then
       ControlFocus($CmdLine[1],"","Edit1")
       ControlSetText($CmdLine[1],"","Edit1",$CmdLine[2])
       ControlClick($CmdLine[1],"","Button1")
    EndIf
    

    Parameters explained:

    Sleep(500); is a half of a second wait. I encounter some issues on an old pc's with a slow HDD. As a result the upload window didn't open in time for the script to be executed. Better safe then sorry.

    $CmdLine[0] contains the number of parameters sent. It will contain 0 if no parameter was sent.

    $CmdLine[1] - represents the upload window title

    $CmdLine[2] - represents the text sent in the File Name bar

    Usage in Java:

    Firefox: Runtime.getRuntime().exec("E:\\compiledScript.exe \"File Upload\" E:\\file.txt");

    Chrome: Runtime.getRuntime().exec("E:\\compiledScript.exe Open E:\\file.txt");