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
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
You can send parameters to the autoitScript.exe.
The Upload Window changes based on the browser:
However these remain the same:
Sleep(500);
If $CmdLine[0] > 0 Then
ControlFocus($CmdLine[1],"","Edit1")
ControlSetText($CmdLine[1],"","Edit1",$CmdLine[2])
ControlClick($CmdLine[1],"","Button1")
EndIf
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
Firefox: Runtime.getRuntime().exec("E:\\compiledScript.exe \"File Upload\" E:\\file.txt");
Chrome: Runtime.getRuntime().exec("E:\\compiledScript.exe Open E:\\file.txt");