AutoIT Example – Upload File In Selenium Webdriver

Selenium is used to make web based application automation test, 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 issue. This article will show you how to use AutoIT to select upload file in selenium webdriver automation test script.

Donwload And Install AutoIT

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 windows dialog. The other is AutoIT Editor which is used to write automation script that simulate user action in choose upload file process.

  1. Go to AutoIT download page to get the latest version as below picture.
    autoit executable file download page
  2. After install, you can find it in windows startup menu.
    autoit in windows startup menu
  3. When you click “AutoIt Window Info” in above startup menu, a dialog will popup.
    autoit window
  4. Go to AutoIT editor download page to get the latest version.
    autoit editor download page
  5. After install AutoIT editor, you can find it in windows startup menu.
    autoit editor in windows startup menu
  6. Click SciTE menu in above startup menu to start AutoIT editor window.
    autoit editor window

Write AutoIT Automation Script

Now you can use above tool to write windows based application automation test scripts.

  1. Use a web browser to open url http://www.dev2qa.com/demo/upload/uploadFileTest.html. And click Browse… button to open the upload file dialog. Do not close this dialog, it will be used later.
    click browse button to open upload file dialog
  2. Drag “Finder Tool” from “AutoIT Window Info” dialog to the target window component which you want to get properties such as title, text and component id.From 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
  3. In AutoIT editor, write below commands in it. You can refer AutoIT built-in functions for command explanation. You can also refer 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");
    

    autoit editor window

  4. Save above code to file uploadFile.au3, right click the file, click “Run Script” menu in popup menu list.
    right click uploadFile.au3 to run the script
  5. After execute, you can see that upload file absolute path value has been entered in the upload file input text box.
    upload file path value entered in the text box
  6. Right click file uploadFile.au3 again, click “Compile Script” menu item to generate uploadFile.exe file.
    compile uploadfile.au3 to get exe file

Invoke AutoIT Executable File

Now you can invoke above generated uploadFile.exe file in your selenium webdriver automation script with below code.

Runtime.getRuntime().exec("C:\\WorkSpace\\uploadFile.exe");

Full Source Code

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;
			}
		}
	}
}

Source Code

  1. Download “TestUploadFileWithAutoIT.zip” TestUploadFileWithAutoIT.zip – Downloaded 89 times – 911 B

(Visited 1,228 times, 1 visits today)
READ :   Selenium JQuery Datepicker Example

Leave a Reply

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.