I have been reading many mails from various selenium groups where people asking for solution on uploading files through desktop in case sendkeys is not working where the input element is not associated with the browse button since a window's pop up is opened which only accepts a stand-alone file.
Some lines from Auto-it website:
Here you need to integrate Auto-it to make it work. Auto-it comes with various components and a complete workflow:
Steps to follow:
Step 1: Download Auto-it from here
Step 2: You will get an installer, just double click and run the executable file.
Step 3: Following components will be installed on your machine
SciTe Script Editor - Use for writing scripts & compiling programs.
AutoIT window info - Locate class & property of the window's based element etc.
Step 4: Write the script using the SciTe Script Editor. Copy and paste the below script in the editor and save it with .au3 format and then compile it by clicking on Tools menu --> Compile
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
Step 5: After compile the script you will get a .exe file that is created on the same location where the script is placed
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class testImageUpload {
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\\auto.exe",
"C:\\Users\\selenium\\test.png", "Open").start();
}
}
Some lines from Auto-it website:
AutoIt v3 is a 3rd
party freeware BASIC-like scripting language tool 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).
Here you need to integrate Auto-it to make it work. Auto-it comes with various components and a complete workflow:
Steps to follow:
Step 1: Download Auto-it from here
Step 2: You will get an installer, just double click and run the executable file.
Step 3: Following components will be installed on your machine
SciTe Script Editor - Use for writing scripts & compiling programs.
AutoIT window info - Locate class & property of the window's based element etc.
Step 4: Write the script using the SciTe Script Editor. Copy and paste the below script in the editor and save it with .au3 format and then compile it by clicking on Tools menu --> Compile
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
Step 5: After compile the script you will get a .exe file that is created on the same location where the script is placed
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class testImageUpload {
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\\auto.exe",
"C:\\Users\\selenium\\test.png", "Open").start();
}
}
Image upload example used for demonstration in this article is at:
http://imageshack.us/