How to upload file in selenium using AutoIT
In earlier articles, we saw that, we can upload the file using sendKeys() method or by using robot class.
Refer below articles for this.
Upload file in selenium using sendkeys() method.
Upload file in selenium using robot class
Now, in case both of the above methods won’t work, then we have to use AutoIT tool (which a third-party tool used to handle window popups). Let’s talk how this can be done.
AutoIt v3 is a freeware BASIC-like scripting language designed for automating Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks.
This method is for handling the Windows File Upload dialog, which cannot be handled using Selenium. Please follow below steps:
1. Download AutoIT setup file from here (https://www.autoitscript.com/site/autoit/downloads/)
Click here to download AutoIT.exe (https://www.autoitscript.com/cgi-bin/getfile.pl?autoit3/autoit-v3-setup.exe)
2. Open Au3Info.exe for which helps to find window information details.
3. Open SciTE Script Editor.
Script used to upload the file is as below. Write the script below in script editor.
ControlFocus(“File Upload”,””,”Edit1″)
ControlSetText(“File Upload”,””,”Edit1″,”C:UsersPrakashDesktopTOSCA tutorial.pptx”)
ControlClick(“File Upload”,””,”Button1″)
We can get Title and control id from application Au3Info.exe. Just Drag and drop the finder tool at required position.
In this script Control Focus is used to focus on the field where we enter the file to be uploaded path details. Note that Control id is the combination of Class and Instance.
Syntax – ControlFocus(“title”,”text”,”Control id”)
ControlSetText is used to write the text (file path of the file to be uploaded)
Syntax – ControlSetText(“title”,”text”,”Control id”,”file path”)
ControlClick is used to click on the button.
Syntax – ControlClick(“title”,”text”,”Control id”)
2. Save the Script with some name with .au3 extension.
3. Then right click on it, click on Compile.
4. Then Double click on the same file.
5. Now you will see the .exe file with same name is generated.
We are going to use this .exe file in our java – selenium program (to upload the file using AutoIT).
6. Use below piece of code for executing the .exe file created above
Runtime.getRuntime().exec(“C:UsersPrakashDesktopTestScript.exe”);
Refer below example –
package basicsOfSelenium;
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 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.tinyupload.com/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
// Click on Browse Button
driver.findElement(By.xpath("html/body/table/tbody/tr[4]/td/table/tbody/tr/td[2]/form/table/tbody/tr[2]/td[1]/input[2]")).click();
//After pop-up open, below script is used to upload the file
Runtime.getRuntime().exec("C:UsersPrakashDesktopTestScript.exe");
}
Hope this helps !!!!
How to upload file in Selenium WebDriver using sendKeys() method
Let us talk about how to upload file in Selenium WebDriver. We can upload the file in selenium WebDriver using various methods. By using sendKeys() method is one of the simplest one.
This is the simplest way to upload the file in selenium Webdriver. Here we will use sendKeys() method to send the absolute file path to be uploaded. But this can be achieved only when the type attribute for filepath textbox / browse button is “file”. Observe below screenshot for this.
This is the simplest way to upload the file in selenium Webdriver. Here we will use sendKeys() method to send the absolute file path to be uploaded. But this can be achieved only when the type attribute for filepath textbox / browse button is “file”. Observe below screenshot for this.
As in above screenshot, since type = file, we can use sendKeys() method to send the file path & then click on upload / submit file button to complete upload process.
First make sure that the input element is visible
Don’t click on the browse button, it will trigger an OS level dialogue box and effectively stop your test dead.
Instead you can use:
driver.findElement(By.xpath(“//*[@id=’uploadfile_0′]”)).sendKeys(“C:UsersPrakashDesktopDesktopTOSCA tutorial.pptx”);
//*[@id=’uploadfile_0′] is the xpath of that element (button in this case) and in sendKeys you have to specify the absolute path of the content you want to upload (Image,video etc). Selenium will do the rest for you.
Keep in mind that the upload file will work only If the element you send a file should be in the form <input type=”file”>
Below is the sample example of how to upload file –
package basicsOfSelenium; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class FileUploadInSelenium1 { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.navigate().to("http://demo.guru99.com/selenium/upload/"); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.manage().window().maximize(); //send the file path to be uploaded to the element, note - this will work only if attribute type = file driver.findElement(By.xpath("//*[@id='uploadfile_0']")).sendKeys("C:UsersPrakashDesktopDesktopTOSCA tutorial.pptx"); //click on accept term checkbox driver.findElement(By.xpath("//*[@id='terms']")).click(); //click on submit button - to complete file upload driver.findElement(By.xpath("//*[@id='submitbutton']")).click(); } }
Well, this was all about upload file in Selenium webdriver by using sendKeys() method. There is another way to do it is by using AutoIT. Refer below link to know more about AutoIT
AutoIT Tutorials – Upload and download files in Selenium WebDriver
Hope this helps !!!