Video:
Document to Install and Write Script for Auto IT:
Code to Call Auto It Script from Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
public class AutoITUseToHandleWindowsPopUp { public static void main(String[] args) throws IOException { System.setProperty("webdriver.chrome.driver", "D:\\VisionITWorkspace\\dependencies\\chromedriver_win32\\chromedriver.exe"); //1 Create Driver object WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); //Navigate and click on the link driver.get("https://www.w3schools.com/jsref/dom_obj_fileupload.asp"); WebElement lnk_try_it_urself = driver.findElement(By.partialLinkText("Try it Yourself")); //this link will open new tab because of its implementation of <a target= "_blank"> lnk_try_it_urself.click(); //Switch to New tab //Get windows handles ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles()); //Two handle will be returned System.out.println(tabs.size()); //Switch to 2nd tab driver.switchTo().window(tabs.get(1)); //Switch to Frame because Choose from File is present in the frame WebElement frame = driver.findElement(By.id("iframeResult")); driver.switchTo().frame(frame); //Find Element Choose from file WebElement btn_choose_from_file = driver.findElement(By.id("myFile")); btn_choose_from_file.click(); //A Window pop Up will be invoked which could not be handled using Selenium //Auto It script need to be invoked in such cases String fileUploadPath = "D:\\VisionITWorkspace\\dependencies\\FileUpload.txt"; //Exe file has been paramaterized to accept file name at run time //Note the space after exe and file name Runtime.getRuntime().exec("D:\\VisionITWorkspace\\dependencies\\AutoITW3SchoolFileUploadPopUp.exe " + fileUploadPath); /* * Tips on How to pass command line parameters to auto it script * https://www.autoitscript.com/autoit3/docs/intro/running.htm#CommandLine * * Auto IT Code: * ControlFocus("Open","","Edit1") ControlSetText("Open","","Edit1",$CmdLine[1]) ControlClick("Open","","Button1") */ } } |
Auto IT code:
1 2 3 |
ControlFocus("Open","","Edit1") ControlSetText("Open","","Edit1",$CmdLine[1]) ControlClick("Open","","Button1") |