Wednesday, March 2, 2016

Windows Dialog Box handling using .Net framework in Selenium

As we know that through selenium we can not automate Windows dialog boxes . There are different way to handle this.

1. Use AutoIT
2. Use System.Windows.Automation library

Will focus on second approach. To identify the control and dialog boxes id we can use UISpy tool.

Here by default , windows desktop is a root element and all the open application will be considered as child element.


// First method is for windows authentication .

public bool Authenticate(string windowTitle, string userName, string password)
        {
            string checkProperty = "AutomationId";
            string userNameId = "1003";
            string passwordId = "1005";
            string okButoonId = "1";
            try
            {
                AutomationElement desktopRoot = AutomationElement.RootElement;
                AutomationElement app = desktopRoot.FindFirst(TreeScope.Children,
                    new PropertyCondition(AutomationElement.NameProperty, windowTitle));
                if (app == null)
                    throw new AutomationException("Window '" + windowTitle + "' is not found.");

                this.WaitForCondition(app, checkProperty, userNameId, 5000);
                AutomationElement userNameTextBoxElement = GetRequiredElement(app, checkProperty, userNameId);
                var userNameTextBox = (ValuePattern)userNameTextBoxElement.GetCurrentPattern(ValuePattern.Pattern);
                userNameTextBox.SetValue(userName);

                AutomationElement passwordTextBoxElement = GetRequiredElement(app, checkProperty, passwordId);
                var passwordTextBox = (ValuePattern)passwordTextBoxElement.GetCurrentPattern(ValuePattern.Pattern);

                AutomationElement okButton = GetRequiredElement(app, checkProperty, okButoonId);
                var okButtonelement = (InvokePattern)okButton.GetCurrentPattern(InvokePattern.Pattern);
                okButtonelement.Invoke();

                return true;
            }
            catch (Exception e)
            {
                throw new AutomationException("Authenticate Method Failed due to exception '" + e.Message);
            }
        }

// Save Dialog box.


        public void SaveDialog(string windowTitle, string dialogName)
        {
            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 pdfWindow = app[0].FindAll(TreeScope.Children,
                new PropertyCondition(AutomationElement.NameProperty, dialogName));


            AutomationElementCollection childrenTemp = pdfWindow[0].FindAll(TreeScope.Children, System.Windows.Automation.Condition.TrueCondition);

            foreach (AutomationElement element in childrenTemp)
            {
                if (element.Current.Name.Equals("OK"))
                {
                    InvokePattern ok = (InvokePattern)element.GetCurrentPattern(InvokePattern.Pattern);
                    ok.Invoke();
                }
            }
        }

  private AutomationElement GetRequiredElement(AutomationElement parentElement, string criteria, string text)
        {
            foreach (AutomationElement element in parentElement.FindAll(TreeScope.Descendants, Condition.TrueCondition))
            {
                if (IsRequiredElement(element, criteria, text))
                    return element;
            }
            return null;

        }

        private Boolean IsRequiredElement(AutomationElement element, string criteria, string text)
        {
            //Check property exists and has value.
            if (!(element.Current.GetType().GetProperty(criteria) == null) && !(element.Current.GetType().GetProperty(criteria).GetValue(element.Current, null) == null))
            {
                //Check property is 'System.String' as it has a different logic to extract the value.
                if (element.Current.GetType().GetProperty(criteria).GetValue(element.Current, null).GetType().ToString().Equals("System.String")
                    || element.Current.GetType().GetProperty(criteria).GetValue(element.Current, null).GetType().ToString().Equals("System.Int32"))
                {
                    if (element.Current.GetType().GetProperty(criteria).GetValue(element.Current, null).ToString() == text)
                        return true;
                }
                else
                {
                    if ((((AutomationIdentifier)(element.Current.GetType().GetProperty(criteria).GetValue(element.Current, null))).ProgrammaticName) == text)
                        return true;
                }
            }
            return false;
        }

        private Boolean WaitForCondition(AutomationElement parentLocator, string criteria, string text, int timeout)
        {
            DateTime actualtimeouttime = DateTime.Now.AddMilliseconds(timeout);
            DateTime currTime = DateTime.Now;
            while (currTime < actualtimeouttime)
            {
                AutomationElement element = GetRequiredElement(parentLocator, criteria, text);
                if (element != null)
                {
                    return true;
                }
                Thread.Sleep(timeout / 100);
                currTime = DateTime.Now;
            }
            throw new AutomationException("Element with search criteria '" + criteria + "=" + text + "' was not found even after '" + timeout + "'");
        }

No comments:

Post a Comment