Using Web-Driver
Inorder to handle upload dialog selenium web driver is able to handle only when the browse button is of type HTML, if it is windows native dialog then we have to use AutoIT as explained below.In web driver, locate the element with tag perhaps like,
<input type="file" id="troubleshooting-restore-file" style="display: none">then we can simply pass the location of the file to be upload before clicking the Restore / Browser button
Correct Way:
driver.findElement(By.cssSelector("input#troubleshooting-restore-file")).sendKeys("C:\\Users\\Fayaz\\Desktop\\<file name>"); // sending keys directly
Wrong way/ Mistake:
WebElement uploadFile = driver.findElement(By.cssSelector("input#troubleshooting-restore-file")).click(); //clicking the element
uploadFile..sendKeys("C:\\Users\\Fayaz\\Desktop\\<file name>") //sending keysThe above case will work irrespective of the HTML input field state (enabled / disabled ). Also note that, in this way upload dialog won't display. We need not to be panic for not seeing dialog, because our work is to upload file, but not handling dialog.
However, in any case if it doesn't work please also have a glance the below explanation, how to integrate AutoIt file with selenium web driver
Using AutoIT
After so much of quest and reading forums, I came to a conclusion that selenium web driver is not handling windows native dialog's, so there fore if we need to upload any file where browse button is not editable then then we have to depend on another automation tool called 'AutoIT' . It's simple to use and very impressive to automate windows GUI's applications.For a quick solution, here is the ready-made code available but perquisites is you need to download and install AutoIT so that you can copy this piece of code and compile to get .exe file so that you can run this executable file by java ProcessBuilder class (If you run out of time to do all these, see below)
#HandleDialog.au3 // File can be save with any name with extension .au3
Opt("MustDeclareVars", 1);0=no, 1=require pre-declare
Main()
Func Main()
Local Const $dialogTitle = $CmdLine[2]
Local Const $timeout = 5
Local $windowFound = WinWait($dialogTitle, "", $timeout)
$windowFound = WinWait($dialogTitle, "", $timeout)
Local $windowHandle
If $windowFound Then
$windowHandle = WinGetHandle("[LAST]")
WinActivate($windowHandle)
ControlSetText($windowHandle, "", "[CLASS:Edit; INSTANCE:1]", $CmdLine[1])
ControlClick($windowHandle, "", "[CLASS:Button; TEXT:&Open]")
Else
MsgBox(0, "", "Could not find window.")
Exit(1)
EndIf
EndFunc
Copy the above code in a notepad and save it (with any name), then compile this using autoit tool to get executable file, now use this exe file with the following code you can test, and the highlighted code is used in your actual test script
# testUpload.java
import java.io.IOException;I am not sure, whether this executable file worked for me also works to you,
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class testUpload {
public static void main(String[] args) throws IOException {
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.imageshack.us/");
driver.findElement(By.xpath("//*[@id='SWFUpload_0']")).click();
Process process = new ProcessBuilder("C:\\Users\\selenium\\Desktop\\HandleDialog.exe", "C:\\Users\\selenium\\Desktop\\file_to_upload.rar", "Open").start();
}
}
If this doesn't works to you, then no option to save your time, you have to follow as mentioned above
No comments:
Post a Comment