Thursday, 3 September 2015

File upload – Selenium WebDriver

There are different ways to upload file, below you can see code for same.

File upload using sendKeys

If   input type="file"  is used the you can upload file using  sendKeys
Suppose “FileUpload.txt” is file you want to upload, and say file is under your project only

 File file = new File("FileUpload.txt ");  
 WebElement upload = driver.findElement(By.locator(LocatorValue));  

File upload using Robot

You can upload file using robot framework as below

 File file = new File("FileUpload.txt");  
 WebElement upload = driver.findElement(By.locator(LocatorValue));;  
 StringSelection filepath = new StringSelection(file.getAbsolutePath());  
 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(filepath, null);  
 //imitate mouse events like ENTER, CTRL+C, CTRL+V  
 Robot robot = new Robot();  

You can also upload file using AutoIT tool


No comments:

Post a Comment