Hi, all today I am going to share small tricks for Autoit, Thanks to my friend Lajish Lakshmanan who gave me this tip. In this post, we will discuss how to pass parameters in Autoit script for Selenium code.
Background
While uploading file in Selenium Webdriver using AutoIT we have hard coded the complete path so we cannot reuse that script for another test, which is actually bad practice.
We have to create a script in such way that we can reuse the scripts. In this case, we need to send parameters so that we can pass file in run time.
Please check below article before moving to passing parameter.
Upload file in Selenium using AutoIT
Solution for pass parameters to AutoIT script
We can accept command line parameter in runtime using below command.
$CmdLine[1]
In my previous post, I have already discussed that part using parameters we can upload multiple files without creating multiple scripts.
AutoIT Script
We can use & symbol to add values in AutoIT.
Example
“Mukesh”&”Otwani”
Program for pass parameters to AutoIT script
//Wait 15 seconds for the Upload window to appear(Comments)
WinWait(“[CLASS:#32770]”,””,15)
//Set input focus to the edit control of Upload window using the handle returned by WinWait(Comments)
ControlFocus(“File Upload”,””,”Edit1″)
//Set the File name text on the Edit field. Last parameter will take file name on runtime(Comments)
ControlSetText(“File Upload”, “”, “Edit1”, “C:\FileUploads\Resume\”&$CmdLine[1])
Once you are done with script save the file > Compile the script > You will get the .exe file.
Program to call the file from Java program.
Runtime.getRuntime().exec(“Your-autoit-script-compiled.exe”+” “+”file-name-which-you-want-to-upload”);
Conclusion.
We have multiple ways to upload file in Selenium webdriver but you have to select which one will be best for you.
This is one of my short post but informative one, I hope you will implement in your project or in your future projects.
Please feel free to share your thoughts in comment section. Will see you in next post.
Kalaiselvi says
Hi,
How I need to give the upload file name through the excel file? I have used dataprovider to get the excel data. Will it support in autoit?
Mukesh Otwani says
Hi Kalaselvi,
Yes, it will. You will be reading data from an intended cell from excel file in String format and saving it in a variable. Pass the same variable to the AutoIT script via cmd line.
Kalaiselvi says
Thank you for clarifying the doubts. Suppose I want to give two parameters through excel means like first name and last name how it will map first parameter and second parameter?
Mukesh Otwani says
Hi Kalaselvi,
You pass as many parameters as you want, just put space between those params. I would also recommend you to go through their official documentation also https://www.autoitscript.com/autoit3/docs/intro/running.htm
Yauwana Ravindra says
Hi Mukesh,
How can i run the AUTOit script using Jenkins or Bamboo in the cloud.will it run as usual as in the local machine or do I need to do few tweaks ?
Thank you,
Yauwana
Mukesh Otwani says
Hi Yauwana,
Hopefully, it should work as it is. Be assure that path of file being used in AutoIt should be correct as location available in Jenkins Slave/Bamboo Agent machine. If these machines are linux machines then AutoIt executable will not work as it works only on Windows OS
Yauwana Ravindra says
Thank you for the reply Mukesh,
What if I use Sikuli instead of AutoIt ?
Mukesh Otwani says
Hi Yuawana,
Sikuli highly depends on screen resolution as Sikuli goes with image comparison. If you are sure about screen resolution stability then go ahead with it.
Laxman says
Hi Mukesh,
Please let me know how to call project location (user.dir) in AutoIT.
Thanks in advance
Laxman
Mukesh Otwani says
Hi Laxman,
There are Macros in AutoIT. Please check this link https://www.autoitscript.com/autoit3/docs/macros.htm
Omkar says
Hi Mukesh,
If we Parameterize
1.it is not updating path of 2nd document, still uploads the first one only
Mukesh Otwani says
Hi Omkar,
AutoIT simply paste files path into file input box at bottom when you click on browse button so if you want to upload multiple file in one shot then you must paste file name like this “C:\Folder_1\File_1.txt” “D:\Folder_2\File_2.txt” …so on
Also check whether your file upload control allows multiple file upload at same time.
harish says
Hi sir, your tutorials are very useful and easy for beginners to learn….. sir let me know what is the difference between Implicit wait , Explicit wait, Fluent wait
Mukesh Otwani says
Hi Harish,
I have article on this and will upload soon.
Raghavendra says
Hi Mukesh,
I was stuck on passing dynamic parameter with auto It. This post has resolved my issue clearly. Thank you very much for such a proper solution.
Mukesh Otwani says
Cheers Raghavendra 🙂
RaviKiran Reddy says
HI Mukesh,
Iam Ravi ,Working as Automation Engineer using selenium, I have found one problem with selenium,If Element is not exists in the Html page or DOM its take alot of time to find that element more than 5 min after 5 mins its executes next line of code I want if that element not exists in the page it immediately go to the next line of code but it takes more time.In Some cases element exists in page so I did if element exist then come this code otherwise go to else code I have a lot of cases like these so it takes a lot of take to execute complete code,I tried with all possible ways like list ,try and Catch but unable to reduce time,Can you please give any solution for this in selenium
Mukesh Otwani says
Hi Ravi,
Are you using implicit wait of 5 min. Kindly use 5-20 seconds and try to use custom library for avoiding sync issue.
Kamal Verma says
Hi Ravikiran,
Can you please help me on this.I am also facing the similar issue
Mukesh Otwani says
Hi Kamal,
As already suggested, reduce time for implicit wait if defined considerably more. Then use a custom method using which check element with fluent wait.
Ritesh kumar says
Hello sir,
i am facing problem while uploading multiple files.
here is my autoIT code
WinWait(“[CLASS:#32770]”,””,15)
ControlFocus(“File Upload”,””,”Edit1″)
ControlSetText(“File Upload”,””,”Edit1″,”C:\Users\Ritesh_PC\Desktop”&$CmdLine[1])
ControlClick(“File Upload”,””,”Button1″)
Here is my script
WebDriver d1 = new FirefoxDriver(fp);
d1.manage().window().maximize();
d1.get(“file:///C:/Users/Ritesh_PC/Desktop/fileupload.html”);
//Demo
WebElement ele= d1.findElement(By.xpath(“//input[@id=’file_u1′]”));
ele.click();
Runtime.getRuntime().exec(“C:/Users/Ritesh_PC/Desktop/m_j.exe”+” “+”Jellyfish.jpg”);
Thread.sleep(3000);
Runtime.getRuntime().exec(“C:/Users/Ritesh_PC/Desktop/m_j.exe”+” “+”Koala.jpg”);
error is i am getting a pop up.
Desktopjellyfish.jpg
File not found
Check the file name and try again
Mukesh Otwani says
Hi Ritesh,
You forgot to add \
old
C:\Users\Ritesh_PC\Desktop
new
C:\Users\Ritesh_PC\Desktop\
try and let me know.
Lajish Lakshmanan says
Good Post…….
Beneficial for all selenium testers
Mukesh Otwani says
Thanks Lajish for nice post.