AutoIt is a
AutoIt, combined with selenium, allows testers to automate web apps with enhanced functionality. Following are the steps to upload a file in Selenium web driver using AutoIt.
Note: This Answer assumes you have AutoIt and Selenium set up on your device.
Write a
ControlFocus
method used to set the keyboard focus to a specific control within a window method. ControlFocus
method is used to set the keyboard focus to a specific control within a window
Here "File Upload"
is the window title containing the control, and "Edit1"
is the control class to set focus to.
You need to save it as test.au3
to test it. Then hold the F5 key. This will execute the statement and display the result in the script editor window. Next, complete the script by using the commands ControlSetText
(command for setting the text in the edit field) and ControlClick
(command used for click action).
Save the script in .au3 format and convert it into .exe by right-clicking on the .au3 file and selecting “Compile Script.”
Now call the .exe file created in the directory to your test script in Selenium by using this command:
Runtime.getRuntime().exec("C:\AutoIt\Test.exe")
The complete test will be as follows:
package practiceTestCases;import java.io.IOException;import java.util.concurrent.TimeUnit;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;public class AutoIt {private static WebDriver driver = null;public static void main(String[] args) throws IOException, InterruptedException {// New instance for the Firefox driverdriver = new FirefoxDriver();// Set element visibility wait timedriver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);// Open your websitedriver.get("https://websitename.com");// Click on element to begin file uploaddriver.findElement(By.id("elementname")).click();// File upload dialog handled by executing scriptRuntime.getRuntime().exec("C:\\AutoIt\\Test.exe");// Allow time for file to uploadThread.sleep(6000);// Close the browserdriver.close();}}
Verify the output on your website and check if the file is uploaded.
Free Resources