Thursday, March 24, 2016

Automating Windows Open Dialog Box using C#

We can automate Dialog boxes using C# library without using AutoIT ,if we are using Selenium Web Driver.

Step 1: Take reference of below dll
1. UIAutomationClient
2. UIAutomationProviders
3. UIAutomationTypes

Step 2: Use UISpy tool to identify the object .

Step 3:  Code


using System;
using System.Threading;
using System.Windows.Automation;
using Microsoft.VisualStudio.TestTools.UnitTesting;

 public class WindowPopUpHandler
    {

        public void OpenDialog(string windowTitle, string dialogName, string filePath)
        {
            try
            {
                AutomationElement desktopRoot = AutomationElement.RootElement;
                if (dialogName == null) throw new ArgumentNullException("dialogName");
              
                 var app = desktopRoot.FindAll(TreeScope.Children,
                    new PropertyCondition(AutomationElement.NameProperty, windowTitle));
              
              if (app == null)
                    return;

                var openDialog= app[0].FindAll(TreeScope.Children,
                    new PropertyCondition(AutomationElement.NameProperty, dialogName));

                AutomationElementCollection childrenTemp =
                openDialog[0].FindAll       (TreeScope.Children,System.Windows.Automation.Condition.TrueCondition);          
              
              ValuePattern fileName = (ValuePattern)childrenTemp[2].GetCurrentPattern (ValuePattern.Pattern);

                fileName.SetValue(filePath);
                Thread.Sleep(4000);
               
               var Open = pdfWindow[0].FindFirst(TreeScope.Children,
               new PropertyCondition       (AutomationElement.NameProperty, "Open"));
             
                InvokePattern open = (InvokePattern)Open.GetCurrentPattern(InvokePattern.Pattern);
                open.Invoke();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
        }
     }

No comments:

Post a Comment