GetDialogHandles() публичный статический Метод

public static GetDialogHandles ( string title ) : IEnumerable
title string
Результат IEnumerable
Пример #1
0
        /// <summary>
        /// Interacts with file dialog windows.
        /// </summary>
        /// <param name="webDriverType">The Type of web driver.</param>
        /// <param name="directory">The directory.</param>
        /// <param name="files">The files.</param>
        /// <returns>
        /// The current Dialog Manager instance.
        /// </returns>
        /// <exception cref="System.Exception">Could not find dialog window.</exception>
        public DialogManager SelectFiles(WebDriverType webDriverType, string directory, params string[] files)
        {
            //_log.Append("Verbose", string.Format("Select {0} file(s) in directory: {1}", files.Length, directory.AbsolutePath()));

            string lookForTitle = null;

            switch (webDriverType)
            {
            case WebDriverType.ChromeDriver:
                lookForTitle = "Open";
                break;

            case WebDriverType.FirefoxDriver:
                lookForTitle = "File Upload";
                break;

            case WebDriverType.InternetExplorerDriver:
                lookForTitle = "Choose File to Upload";
                break;

            case WebDriverType.EmulatorDriver:
                lookForTitle = "Open File";
                break;
            }

            bool success;
            int  tries = 0;

            do
            {
                success = WindowHelper.GetDialogHandles(lookForTitle).Any();
                tries++;
                if (!success && tries <= 20)
                {
                    Thread.Sleep(250);
                }
            } while (!success && tries < 20);

            if (!success)
            {
                throw new Exception("Could not find dialog window.");
            }

            IEnumerable <IntPtr> dialogs = WindowHelper.GetDialogHandles(lookForTitle);
            IntPtr dialog = dialogs.FirstOrDefault();

            if (dialog != default(IntPtr))
            {
                // Set the window title to something unique so other WebManager instances don't try to use it
                string title = lookForTitle + " - " + RandomData.RandomAlphanumeric(3);
                WindowHelper.SetWindowTitle(dialog, title);

                // Select files and submit
                DialogHelper.SelectFiles(dialog, title, directory, files);
            }

            return(this);
        }
Пример #2
0
        /// <summary>
        ///     Interacts with the dialog to select the specified files and return.
        /// </summary>
        /// <param name="dialogHandle">
        ///     The dialog handle.
        /// </param>
        /// <param name="comparison">Indicates how to match the <c>dialogTitle</c>.</param>
        /// <param name="dialogTitle">The dialog window title.</param>
        /// <param name="directory">
        ///     The directory.
        /// </param>
        /// <param name="files">
        ///     The files.
        /// </param>
        public static void SelectFiles(IntPtr dialogHandle, string dialogTitle, string directory, params string[] files)
        {
            // Navigate to the directory and select files - includes fallback methods if the path is longer than 259 characters
            AutomationElement appElement = AutomationElement.FromHandle(dialogHandle);
            const int         keyReturn  = 0x0D;

            directory = directory.AbsolutePath();

            if (files.Any())
            {
                AutomationElement fileTextBox;

                // Locate textbox
                int count = 0;
                do
                {
                    count++;
                    if (count == 4)
                    {
                        throw new ElementNotAvailableException();
                    }
                    fileTextBox = appElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "Edit"));
                    if (fileTextBox == null)
                    {
                        Thread.Sleep(100);
                    }
                } while (fileTextBox == null);

                var fileName = fileTextBox.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;

                if (fileName != null)
                {
                    // Textbox accepts a maximum of 259 characters
                    if (directory.Length > 259)
                    {
                        string[] split = directory.Split('\\');
                        foreach (string value in split)
                        {
                            fileName.SetValue(value);
                            WindowHelper.SendKey(dialogHandle, keyReturn);

                            do
                            {
                                Thread.Sleep(100);
                            } while (!string.IsNullOrWhiteSpace(fileName.Current.Value));
                        }
                    }
                    else
                    {
                        fileName.SetValue(directory);
                        WindowHelper.SendKey(dialogHandle, keyReturn);
                        var fileSelected = fileTextBox.GetCurrentPattern(TextPattern.Pattern) as TextPattern;

                        while (true)
                        {
                            Thread.Sleep(100);
                            int selectedLength = 0;

                            try
                            {
                                if (fileSelected != null)
                                {
                                    selectedLength = fileSelected.GetSelection()[0].GetText(-1).Length;
                                }
                            }
// ReSharper disable EmptyGeneralCatchClause
                            catch
// ReSharper restore EmptyGeneralCatchClause
                            {
                            }

                            if (string.IsNullOrWhiteSpace(fileName.Current.Value) || selectedLength > 0)
                            {
                                break;
                            }
                        }
                    }
                }

                // Select items
                if (files.Count() == 1)
                {
                    if (fileName != null)
                    {
                        fileName.SetValue(files.First());
                    }
                }
                else
                {
                    int tries   = 0;
                    int counter = 1;
                    do
                    {
                        AutomationElementCollection children = appElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.IsSelectionItemPatternAvailableProperty, true));

                        foreach (AutomationElement child in children)
                        {
                            var itemText = (string)child.GetCurrentPropertyValue(AutomationElement.NameProperty);
                            if (files.Any(file => itemText == file))
                            {
                                ListItemSelect(child, counter != 1);
                                counter++;
                            }
                        }

                        tries++;
                        if (tries > 3)
                        {
                            throw new ElementNotAvailableException("Could not find list items.");
                        }
                        if (counter < 2)
                        {
                            Thread.Sleep(100);
                        }
                    } while (counter < 2);
                }
            }

            AutomationElementCollection buttons = appElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "Button"));

            InvokePattern submitButton = null;

            foreach (AutomationElement button in buttons)
            {
                string itemText = ((string)button.GetCurrentPropertyValue(AutomationElement.NameProperty)).ToLower();
                if (itemText.Contains("open") || itemText.Contains("save"))
                {
                    try
                    {
                        submitButton = button.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
                    }
                    catch
                    {
                        Point pt = button.GetClickablePoint();
                        SendInputClass.ClickLeftMouseButton((int)pt.X, (int)pt.Y);
                    }

                    break;
                }
            }

            // Do until dialog disappears
            while (WindowHelper.GetDialogHandles(dialogTitle).Any())
            {
                Thread.Sleep(100);
                if (submitButton != null)
                {
                    try
                    {
                        submitButton.Invoke();
                    }
// ReSharper disable EmptyGeneralCatchClause
                    catch
// ReSharper restore EmptyGeneralCatchClause
                    {
                    }
                }
                else
                {
                    WindowHelper.SetActiveWindow(WindowHelper.GetDesktop());
                    WindowHelper.SendKey(dialogHandle, keyReturn);
                }
            }
        }