I am trying to use selenium webdriver to automate web application. So say the dialog below pops up, my goal is to automatically click Save File, and then hit Ok.
Windows Save/Open File DownloadDialouge
WebDriver cannot directly interact with dialog windows this is because dialog windows are the domain of the operating system and not the webpage. However its possible to do actions on dialog windows using SendKeys class method SendWait() of name space System.Windows.Forms
using System.Windows.Forms;
In the example code below a PLUpload button is pressed, which opens a Windows Dialog to select the file to upload.
Following lines are used to send key values to the dialog window displayed.
SendKeys.SendWait(@"C:\Users\Public\Pictures\Sample Pictures\Dock.jpg");
SendKeys.SendWait(@"{Enter}");
Detailed reference of SendKeys class in C# can be found in http://msdn.microsoft.com/en-au/library/system.windows.forms.sendkeys.aspx
using System;
using System.Windows.Forms;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Interactions;
using NUnit.Framework;
namespace BusinessCreation
{
class PlUpload
{
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.plupload.com/example_queuew idget.php");
driver.FindElement(By.XPath("//object[@data='/plupload/js/pluploa d.flash.swf']")).Click();
SendKeys.SendWait(@"C:\Users\Public\Pictures\Sample Pictures\Dock .jpg");
SendKeys.SendWait(@"{Enter}");
}
}
}
Not possible with Selenium on it's own.
You'll have to combine it with AutoIT:
http://www.autoitscript.com/site/autoit/
(or something similar).
I'd also point out for majority of cases you can get away with simply 'sending' (using the 'Send Keys' method/function in your particular language bindings) the file path direct to the upload control, ensuring you do not click the actual Browse...
button. Once you do, it's game over for Selenium.
//File Save Code
FirefoxProfile profile =new FirefoxProfile();
profile.setPreferences("browser.download.folderlist",0);
profile.setpreferences("browser.helperApps.neverAsk.saveToDisk",mimeType);//applicatio/zip
WebDriver driver = new FireforDriver(profile);
driver.get("http:seleniumhq.org/download");
driver.get("http://selenium.com/files/selenium.zip");
//File Open Code
profile.setPreferences("browser.helperApps.neverAsk.open",mimeType);
Use this Method for file handling:
We need :
jacob.jar Download
It will contain one jar file and 2 .dll files
AutoItX4Java.jar Download
public static void uploadFile(String path, String browser){
if(browser.equalsIgnoreCase("chrome")){
if(x.winWaitActive("Open", "", 10)){
if(x.winExists("Open")){
x.sleep(500);
x.send(path);
x.controlClick("Open", "", "Button2");
}
}
}
if(browser.equalsIgnoreCase("firefox")){
if(x.winWaitActive("File Upload", "", 10)){
if(x.winExists("File Upload")){
x.sleep(500);
x.send(path);
x.controlClick("File Upload", "", "Button2");
}
}
}
if(browser.equalsIgnoreCase("InternetExplorer")){
if(x.winWaitActive("Choose File to Upload", "", 10)){
if(x.winExists("Choose File to Upload")){
x.sleep(500);
x.send(path);
x.controlClick("Choose File to Upload", "", "Button2");
}
}
}
}
public void test(){
//Click on the Select button of the file upload
uploadFile("Path", "chrome");
}
Thanks... Dont click Accept or Upvote until it works for you. If it is not working for you means, please comment.. Dont Downvote...