
Question:
I made a Selenium script to download an Excel sheet, open, edit and upload it back. I use AutoIt for the file upload.
I have different files to upload for each run, so I can't hard code the file path, hence I want to pass it (to the AutoIt script) as an argument. The AutoIt script:
ControlFocus("File Upload", "", "Edit1")
Sleep(1000)
ControlSetText("File Upload", "", "Edit1", "C:\Users\nilasing\Downloads\somefilename")
Sleep(1000)
ControlClick("File Upload", "", "Button1")
"C:\Users\nilasing\Downloads\somefilename"
is the hard coded path.
You can pass in command line parameters to the script, then read them using the $CmdLine
array.
ControlFocus("File Upload", "", "Edit1")
Sleep(1000)
ControlSetText("File Upload", "", "Edit1", $CmdLine[1])
Sleep(1000)
ControlClick("File Upload", "" , "Button1")
See <a href="https://www.autoitscript.com/autoit3/docs/intro/running.htm#CommandLine" rel="nofollow">https://www.autoitscript.com/autoit3/docs/intro/running.htm#CommandLine</a> for more information.
Answer2:I've used the same method: In my AutoIT au3 file -
ControlFocus("File Upload","","Edit1")
ControlSetText("File Upload","","Edit1",$CmdLine[1])
ControlClick("File Upload","","Button1")
Then call the AutoIT executable from Selenium WebDriver with -
String fileName = "C:\\Calendar.xls"; // passed as a command line parameter to AutoIT executable below
String autoITExecutable = ".\\AutoITFileUploadWithParam.exe " + fileName;
.......
try {
Runtime.getRuntime().exec(autoITExecutable);
Thread.sleep(5000);
test.log(LogStatus.INFO, "Ran AutoIT script to upload : " + fileName);
} catch (Exception e) {
test.log(LogStatus.ERROR, "Failed to run AutoIT script : " + e.getMessage());
}
driver.findElement(By.xpath("//div[text()='Send']")).click();
test.log(LogStatus.INFO, "Email sent");
Note the space between the AutoIT executable and the parameter in String autoITExecutable