Sunday, 2 August 2015

Interaction with Windows Dialog box using Selenium Web-Driver C#

Agenda for this blog:To discuss about  handling Window based Dialogue/popup box in Selenium C# .

What is Window Dialogue/Pop-Up:Its a dialogue box which is not a part of  webpage but are the domain of the Operating System.

As it does not belong the web property so Selenium web-driver is unable to interact directly with this type of popup/dialogue.However its possible to perform actions on dialog windows using 
"SendKeys" class's method "SendWait()" of name space System.Windows.Forms.

Detailed reference of SendKeys Class can be found in https://msdn.microsoft.com/en-au/library/system.windows.forms.sendkeys.aspx


Sample Scenario

1.Uploading File:


In this scenario we will handle window dialogue for uploading File in Selenium.


 static void Main(string[] args)
        {

            IWebDriver driver = new FirefoxDriver();
            driver.Manage().Window.Maximize();
            driver.Navigate().GoToUrl("https://www.newocr.com/");
            driver.FindElement(By.XPath("//input[@id='userfile']")).Click();
            Thread.Sleep(3000);
            String pathToFile = "C:\\Users\\Admin\\Desktop\\434765";
            System.Windows.Forms.SendKeys.SendWait("^a");
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait("{BACKSPACE}");
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait(pathToFile);
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait("{ENTER}");
            Thread.Sleep(3000);
            driver.Quit();
         
        }


2. Saving/Downloading Files:



static void Main(string[] args)

        {

            IWebDriver driver = new FirefoxDriver();
            driver.Manage().Window.Maximize();
            driver.Navigate().GoToUrl("http://www.seleniumhq.org/projects/webdriver/");
            Thread.Sleep(3000);
            System.Windows.Forms.SendKeys.SendWait("^s");
            Thread.Sleep(2000);
            String pathToSaveFile="C:\\Users\\Admin\\Desktop\\II";
            System.Windows.Forms.SendKeys.SendWait("^a");
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait("{BACKSPACE}");
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait(pathToSaveFile);
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait("{ENTER}");
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait("{ENTER}");
            Thread.Sleep(3000);
            driver.Quit();


        }


3.Printing Page:


class Program

    {
        static void Main(string[] args)
        {

            IWebDriver driver = new FirefoxDriver();
            driver.Manage().Window.Maximize();
            driver.Navigate().GoToUrl("https://www.newocr.com/");
            Thread.Sleep(3000);
            System.Windows.Forms.SendKeys.SendWait("^p");
            Thread.Sleep(2000);
            System.Windows.Forms.SendKeys.SendWait("{ENTER}");
            Thread.Sleep(3000);
            driver.Quit();
         
        }


By the above approach interaction with windows dialogue box /pop-up is handled properly with Selenium WebDriver.





Visit for More Automation Related Discussion:


https://www.youtube.com/channel/UCKSk4gkmO3LXcW17hFUkmcQ/videos