- Upload File In Selenium Webdriver Using Autoit With Selenium Key
- Selenium Webdriver Tutorial
- Upload File In Selenium Webdriver Using Autoit With Selenium Download
- How To Read Excel File In Selenium Webdriver
Upload File using AutoIT & SendKeys:
In this post we are going to see on how to upload file using AutoIT and sendKeys method in Selenium WebDriver. There are two cases which are majorly used to upload file in Selenium WebDriver such as using SendKeys Method and using AutoIT Script.
Also Read: How To Download File Using AutoIT in Selenium WebDriver
1. Upload file using SendKeys method in Selenium WebDriver:
Upload a file using AutoIT in Selenium-Webdriver July 12, 2015 July 12, 2015 Hari Charan Generally we face a problem while uploading a file in selenium.Because we know that selenium only supports web based applications.In order to suppress this we need to use AutoIT.For example you wanna upload your a file in Gmail-compose. What you gonna do???
Its very straightforward. Using sendkeys method, we could easily achieve this. Locate the text box and set the file path using sendkeys and click on submit button.
2 4 6 8 10 12 14 16 18 20 | import java.io.IOException; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; publicclassUpload{ publicstaticvoidmain(String[]args)throwsIOException{ //Instantiation of driver object. To launch Firefox browser //To open URL 'http://softwaretestingmaterial.com' driver.get('http://softwaretestingplace.blogspot.com/2015/10/sample-web-page-to-test.html'); WebElement browse=driver.findElement(By.id('uploadfile')); //pass the path of the file to be uploaded using Sendkeys method browse.sendKeys('D:SoftwareTestingMaterialUploadFile.txt'); //'close' method is used to close the browser window } |
2. Upload file AutoIt Script in Selenium WebDriver:
If there is no text box to set the file path and only able to click on Browse button to upload the file in the windows popup box then we do upload file using AutoIt tool.
AutoIt Introduction:
AutoIt Tool is an open source tool. It is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying “runtimes” required!
Now the question is how we do upload file using AutoIT Tool in Selenium WebDriver.
Follow the below steps:
- Download Autoittool from here and install it
- Open Programs – Autoit tool – SciTE Script Editor and add the below mentioned AutoIt script in Autoit editor and save it as ‘UploadFile.au3’ in your system
- Convert it as ‘UploadFile.exe’
- In Eclipse, add the below mentioned Selenium Script and run
Step 1: Download AutoIt tool and install
Step 2: Open SciTE Script editor and add the below mentioned AutoIt script and save it as ‘UploadFile.au3’ in your system.
AutoIt Script:
2 | Send('D:SoftwareTestingMaterialUploadFile.txt') |
AutoIt Script Explanation:
Line 1 : WinWaitActive(“File Upload”)
Above line of code changes the focus of cursor on the Window popup box to upload file.
‘File Upload‘ is the name of the window popup when using Mozilla Firefox. If you want to use other browsers such as Chrome you need to pass the value as ‘Open‘ (‘Open’ is the name of the window popup) and for IE you need to pass the value as ‘File To Upload’ (‘File To Upload’ is the name of the window popup)
Line 2 : Send(“Path of the document”)
Once the window popup is active, it sets the path of the document which needs to be uploaded
Send(“D:SoftwareTestingMaterialUploadFile.txt”)
Line 3 : Send(“{ENTER}”)
After that it clicks on Open button which will upload the document
Step 3: Once the file is saved, we need to convert the ‘UploadFile.au3’ to ‘UploadFile.exe’. To do this we need to compile the ‘UploadFile.au3’
Right click on the file ‘UploadFile.au3’ and click on ‘Compile Script’ to generate an executable file ‘UploadFile.exe’
Step 4: In Eclipse, add the below mentioned Selenium Script and run
Given clear explanation in the comments section with in the program itself. Please go through it to understand the flow.
Upload File In Selenium Webdriver Using Autoit With Selenium Key
2 4 6 8 10 12 14 16 18 20 22 | importjava.io.IOException; importorg.openqa.selenium.WebDriver; importorg.openqa.selenium.firefox.FirefoxDriver; publicstaticvoidmain(String[]args)throwsIOException{ //Instantiation of driver object. To launch Firefox browser //To open URL 'http://softwaretestingmaterial.com' driver.get('http://softwaretestingplace.blogspot.com/2015/10/sample-web-page-to-test.html'); WebElement browse=driver.findElement(By.id('uploadfile')); browse.click(); Runtime.getRuntime().exec('D:SoftwareTestingMaterialAutoItUploadfile.exe'); //'close' method is used to close the browser window } |
In the above Selenium Script, we did call the AutoIt Script after clicking on the browser button which transfers windows popup box and upload the required file.
Syntax:
Selenium Webdriver Tutorial
Runtime.getRuntime().exec('File Path of AutoIt.exe'); |
Upload File In Selenium Webdriver Using Autoit With Selenium Download
Runtime.getRuntime().exec(“D:SoftwareTestingMaterialAutoItUploadfile.exe”);
This way we could upload a file using AutoIT
If you are not regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.