Saturday, March 23, 2013

How to interact with Dialog windows using Selenium WebDriver C#

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_queuewidget.php");
               driver.FindElement(By.XPath("//object[@data='/plupload/js/plupload.flash.swf']")).Click();
               SendKeys.SendWait(@"C:\Users\Public\Pictures\Sample Pictures\Dock.jpg");
               SendKeys.SendWait(@"{Enter}");
         }
    }
}

2 comments: