AutoIT Example – Upload File In Selenium Webdriver

Selenium is used to make web-based application automation tests, but it can not handle native OS windows dialog for example upload file dialog. If you run the automation test in windows os, AutoIT is a good choice to resolve such an issue. This article will show you how to use AutoIT to select upload files in selenium webdriver automation test script.

1. Download And Install AutoIT.

  1. You should install two software to run AutoIT. One is AutoIT.exe which is used to extract the GUI component’s title, text, and id in the windows dialog. The other is AutoIT Editor which is used to write an automation script that simulates user action in the process of choosing an upload file.
  2. Go to the AutoIT download page to download the latest version of AutoIt.
  3. After install, you can find it in the windows Start —> AutoIt v3 —> AutoIt Windows Info (X64).
  4. When you click “AutoIt Window Info” in the above startup menu, a dialog will pop up.
  5. You can use the Finder Tool in the Window Info dialog to get windows GUI attributes.
  6. Go to the AutoIT editor download page to download the latest version for example SciTE4AutoIt3.exe.
  7. After installing AutoIT editor, you can find it in the windows Startup —> AutoIt v3 —> SciTE —> SciTE.
  8. Click the SciTE menu in the above startup menu to start the AutoIT editor window.

2. Write AutoIT Automation Script.

  1. Now you can use the above tool to write windows based application automation test scripts.
  2. Use a web browser to open URL http://www.dev2qa.com/demo/upload/uploadFileTest.html.
  3. It will display a web page with an Html file upload element (an input textbox and a Browse… button), and an Upload File button below the input textbox.
  4. Click Browse… button on the web page to open the upload file dialog. Do not close this dialog, it will be used later.
  5. Drag “Finder Tool” from the “AutoIT Window Info” dialog to the target window component which you want to get properties such as title, text, and component id. From the below picture we can get the File name input text box’s title is “Choose File to Upload“, Component id is Edit1(Basic Control Info: Class + Instance).
    drag-autoit-finder-tool-to-upload-file-input-text-box
  6. In AutoIT editor, write the below commands in it. You can refer to AutoIT built-in functions for command explanation. You can also refer to AutoIT online docs for more info.
    ; Focus on the Edit1 component.
    ControlFocus("Choose File to Upload", "", "Edit1");
    ; Click Edit1 component.
    ControlClick("Choose File to Upload", "", "Edit1");
    ; Input upload file path in Edit1 input text box.
    ControlSetText("Choose File to Upload","", "Edit1", "C:\WorkSpace\JEETT.pdf");
    ; Click Open button to choose the upload file.
    ControlClick("Choose File to Upload", "", "Button1");
    
  7. Save the above code to file uploadFile.au3, right-click the file, click the “Run Script” menu in the popup menu list.
  8. After execution, you can see that the upload file absolute path value has been entered in the upload file input text box.
  9. Right-click file uploadFile.au3 again, click “Compile Script” menu item to generate uploadFile.exe file.

3. Invoke AutoIT Executable File.

  1. Now you can invoke the above generated uploadFile.exe file in your selenium webdriver automation script with the below code.
    Runtime.getRuntime().exec("C:\\WorkSpace\\uploadFile.exe");
  2. Below is the full source code for this example.
    public class TestUploadFileWithAutoIT {
    
    	public static void main(String[] args) {
    		
    		TestUploadFileWithAutoIT example = new TestUploadFileWithAutoIT();
    		example.uploadFileUseAutoITChrome();
    	}
    
    	/* Upload in Chrome. */
    	public void uploadFileUseAutoITChrome()
    	{
    		ChromeDriver driver = null;
    		try
    		{	
    			 // Specify Chrome Driver executable path.
    			 String chromeDriverPath = "C:\\Workspace\\dev2qa.com\\Lib\\chromedriver_win32\\chromedriver.exe";
    			 
    			 //Assign chrome driver path to system property "webdriver.chrome.driver"
    			 System.setProperty("webdriver.chrome.driver", chromeDriverPath);
    			  
    			 //Initiate Chrome driver instance.
    			 driver = new ChromeDriver();
    			
    			 /* Upload page*/
    			 String uploadFileDisplayUrl = "http://www.dev2qa.com/demo/upload/uploadFileTest.html";
    							
    			 // Display upload page.
    			 driver.get(uploadFileDisplayUrl);
    				
    			 //Get the upload web element by it's name "uploadFileInputBox"
    			 WebElement element = driver.findElement(By.name("uploadFileInputBox"));
    				
    			 //Click the upload button to open select file window.
    			 element.click();
    				
    			 Thread.sleep(1000);
    
    			 Runtime.getRuntime().exec("C:\\WorkSpace\\uploadFile.exe");
    			 
    			 Thread.sleep(5000);
    			 
    			 // Click the upload form submit button. 
    			 driver.findElement(By.name("uploadFileSubmitBtn")).click();
    			 
    			 Thread.sleep(3000);
    				
    		}catch(Exception ex)
    		{
    			ex.printStackTrace();
    		}finally
    		{
    			if(driver!=null)
    			{
    				driver.close();
    				driver = null;
    			}
    		}
    	}
    }
    

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.