0

I wrote 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.

0

2 Answers 2

3

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 https://www.autoitscript.com/autoit3/docs/intro/running.htm#CommandLine for more information.

Sign up to request clarification or add additional context in comments.

Comments

3

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

1 Comment

Parameterizing file path is one thing, but how can I make same script work for cross browser i.e. the upload window's title is "Open" for Chrome or Edge instead of "File upload" in case of firefox..?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.