Пример #1
0
        public bool FindClipboardHelperWindowEnterTextAndCopy(string textToCopy, out IList <string> log)
        {
            log = new List <string>();
            if (!TestProcessHelper.IsProcessRunning(TestProcessHelper.ProcessType.Paleface))
            {
                log.Add("No paleface process could be found");
                return(false);
            }

            var windowsElementSearchSpec = WindowsElementSearchSpec.Create("", Properties.Resources.WindowTitle);
            var element = vWindowsElementSearcher.SearchWindowsElement(windowsElementSearchSpec, log);

            if (element == null)
            {
                return(false);
            }

            windowsElementSearchSpec = WindowsElementSearchSpec.Create("", "TextToCopyAutoId");
            element = vWindowsElementSearcher.SearchWindowsElement(element, windowsElementSearchSpec, log);
            if (element == null)
            {
                return(false);
            }

            var textBox = Create(element);

            textBox.Clear();
            textBox.SetText(textToCopy, false);
            textBox.CopyToClipboard();
            textBox.PasteFromClipboard();
            return(textBox.Text == textToCopy);
        }
Пример #2
0
        public void CanUseFileDialog()
        {
            var sut = vContainer.Resolve <IWindowsElementSearcher>();

            var windowsElementSearchSpec      = WindowsElementSearchSpec.Create("", "Document - WordPad");
            var windowsChildElementSearchSpec = WindowsElementSearchSpec.Create("", "Rich Text Window");

            windowsElementSearchSpec.WindowsChildElementSearchSpecs.Add(windowsChildElementSearchSpec);
            var log     = new List <string>();
            var element = sut.SearchWindowsElement(windowsElementSearchSpec, log);

            Assert.IsNotNull(element, $"Wordpad document not found\r\n{string.Join("\r\n", log)}");

            element.SendKeys(Keys.Control + 'o' + Keys.Control);

            windowsElementSearchSpec      = WindowsElementSearchSpec.Create("", "Document - WordPad");
            windowsChildElementSearchSpec = WindowsElementSearchSpec.Create("", "Open");
            windowsElementSearchSpec.WindowsChildElementSearchSpecs.Add(windowsChildElementSearchSpec);
            element = sut.SearchWindowsElement(windowsElementSearchSpec, log);
            Assert.IsNotNull(element, $"File dialog not found\r\n{string.Join("\r\n", log)}");

            element = sut.SearchWindowsElement(element, WindowsElementSearchSpec.Create(UiClassNames.Edit, "File name:"), log);
            Assert.IsNotNull(element, $"File name not found\r\n{string.Join("\r\n", log)}");

            var comboBox = vContainer.Resolve <ITextBoxServices>().Create(element);

            var fileName = Path.GetTempPath() + @"\TextFile.txt";

            comboBox.Text = fileName;
            Assert.AreEqual(fileName, comboBox.Text);
        }
Пример #3
0
        public async Task CannotFindShutDownCalculator()
        {
            TestProcessHelper.ShutDownRunningProcesses(TestProcessHelper.ProcessType.Calculator);
            var windowsElementSearchSpec = WindowsElementSearchSpec.Create(UiClassNames.ApplicationFrameWindow, "Calculator");
            var sut     = vContainer.Resolve <IWindowsElementSearcher>();
            var log     = new List <string>();
            var element = sut.SearchWindowsElement(windowsElementSearchSpec, log);

            Assert.IsNull(element);
            Assert.AreEqual(1, log.Count);
            element = sut.SearchWindowsElement(windowsElementSearchSpec);
            Assert.IsNull(element);
            await TestProcessHelper.LaunchProcessAsync(TestProcessHelper.ProcessType.Calculator);
        }
Пример #4
0
        public void CanUseNameContainsCriteria()
        {
            var windowsElementSearchSpec = WindowsElementSearchSpec.Create(UiClassNames.ApplicationFrameWindow, "Calculator");

            windowsElementSearchSpec.NameContains = "Calculator";
            var sut     = vContainer.Resolve <IWindowsElementSearcher>();
            var log     = new List <string>();
            var element = sut.SearchWindowsElement(windowsElementSearchSpec, log);

            Assert.IsNotNull(element);
            Assert.AreEqual("Calculator", element.GetName());
            Assert.AreEqual(UiClassNames.ApplicationFrameWindow, element.GetClassName());
            Assert.AreEqual("Calculator", element.GetName());
        }
Пример #5
0
        public void CanUseNameDoesNotContainCriteria()
        {
            var windowsElementSearchSpec = WindowsElementSearchSpec.Create(UiClassNames.Button, "");

            windowsElementSearchSpec.NameMustNotBeEmpty = true;
            windowsElementSearchSpec.NameDoesNotContain = new List <string> {
                "User Notifications Indicator", "Open Navigation", "Keep on top", "Minimize", "Restore", "Close"
            };
            var sut     = vContainer.Resolve <IWindowsElementSearcher>();
            var log     = new List <string>();
            var element = sut.SearchWindowsElement(windowsElementSearchSpec, log);

            Assert.IsNotNull(element);
            Assert.IsFalse(string.IsNullOrWhiteSpace(element.GetName()));
            Assert.IsFalse(windowsElementSearchSpec.NameDoesNotContain.Any(n => element.GetName().Contains(n)));
        }
Пример #6
0
        public void CanUseOptionalSearchCriteria()
        {
            var windowsElementSearchSpec = WindowsElementSearchSpec.Create("", "Desktop 1");

            windowsElementSearchSpec.NameMustNotBeEmpty = true;
            var sut     = vContainer.Resolve <IWindowsElementSearcher>();
            var log     = new List <string>();
            var element = sut.SearchWindowsElement(windowsElementSearchSpec, log);

            Assert.IsNotNull(element);
            Assert.AreEqual("Desktop 1", element.GetName());
            Assert.AreEqual("#32769", element.GetClassName());
            var elementName = element.GetName();

            Assert.IsFalse(string.IsNullOrWhiteSpace(elementName));
            Assert.IsTrue(elementName.Contains("Desktop"));
        }
Пример #7
0
        public bool EnterFileNameAndPressEnter(string fileName, string windowName, List <string> log)
        {
            if (!TestProcessHelper.IsProcessRunning(TestProcessHelper.ProcessType.Paleface))
            {
                log.Add("Paleface clipboard helper window has not been started");
                return(false);
            }

            var windowsChildElementSearchSpec      = WindowsElementSearchSpec.Create("#32770", string.IsNullOrWhiteSpace(windowName) ? "Open" : "");
            var windowsGrandChildElementSearchSpec = WindowsElementSearchSpec.Create(UiClassNames.Edit, "File name:");

            windowsChildElementSearchSpec.WindowsChildElementSearchSpecs.Add(windowsGrandChildElementSearchSpec);
            var windowsElementSearchSpec = string.IsNullOrWhiteSpace(windowName)
                ? WindowsElementSearchSpec.Create("", "Desktop 1")
                : WindowsElementSearchSpec.Create(UiClassNames.Window, windowName);

            windowsElementSearchSpec.WindowsChildElementSearchSpecs.Add(windowsChildElementSearchSpec);

            var element = WindowsElementSearcher.SearchWindowsElement(windowsElementSearchSpec, log);

            if (element == null)
            {
                return(false);
            }

            element.SendKeys("");
            element = WindowsElementSearcher.SearchWindowsElement(element, windowsGrandChildElementSearchSpec, log);
            if (element == null)
            {
                return(false);
            }

            element.SendKeys("");
            var textBox = TextBoxServices.Create(element);

            textBox.Clear();
            textBox.Text = fileName;
            if (textBox.Text != fileName)
            {
                return(false);
            }

            element.SendKeys(Keys.Enter);
            return(true);
        }
Пример #8
0
        public AppiumWebElement SearchWindowsElement(WindowsElementSearchSpec windowsElementSearchSpec, IList <string> log)
        {
            AppiumHelper.LaunchWinAppDriverIfNecessary();

            var options = new AppiumOptions();

            options.AddAdditionalCapability("app", "Root");

            WindowsDriver <AppiumWindowsElement> desktopSession;

            try {
                desktopSession = new WindowsDriver <AppiumWindowsElement>(new Uri("http://127.0.0.1:4723"), options);
            } catch (WebDriverException) {
                throw new Exception("WinAppDriver.exe process could not be contacted");
            }

            return(SearchWindowsElement(desktopSession, null, windowsElementSearchSpec, log));
        }
Пример #9
0
        public void CanFindCalculator()
        {
            var windowsElementSearchSpec      = WindowsElementSearchSpec.Create(UiClassNames.ApplicationFrameWindow, "Calculator");
            var windowsChildElementSearchSpec = WindowsElementSearchSpec.Create(UiClassNames.NamedContainerAutomationPeer, "Standard functions");

            windowsElementSearchSpec.WindowsChildElementSearchSpecs.Add(windowsChildElementSearchSpec);
            windowsChildElementSearchSpec.WindowsChildElementSearchSpecs.Add(WindowsElementSearchSpec.Create(UiClassNames.Button, "Square root"));
            windowsChildElementSearchSpec.WindowsChildElementSearchSpecs.Add(WindowsElementSearchSpec.Create(UiClassNames.Button, "Reciprocal"));
            var sut     = vContainer.Resolve <IWindowsElementSearcher>();
            var log     = new List <string>();
            var element = sut.SearchWindowsElement(windowsElementSearchSpec, log);

            Assert.IsNotNull(element);
            Assert.AreEqual("Calculator", element.GetName());
            Assert.AreEqual(UiClassNames.ApplicationFrameWindow, element.GetClassName());
            Assert.AreEqual(16, log.Count);
            element = sut.SearchWindowsElement(windowsElementSearchSpec, log);
            Assert.IsNotNull(element);
        }
Пример #10
0
        public void CanPressEnter()
        {
            var windowsElementSearchSpec      = WindowsElementSearchSpec.Create("", "Document - WordPad");
            var windowsChildElementSearchSpec = WindowsElementSearchSpec.Create("", "Rich Text Window");

            windowsElementSearchSpec.WindowsChildElementSearchSpecs.Add(windowsChildElementSearchSpec);
            var searcher = vContainer.Resolve <IWindowsElementSearcher>();
            var element  = searcher.SearchWindowsElement(windowsElementSearchSpec);

            Assert.IsNotNull(element, "Wordpad not found");
            var log = new List <string>();

            element = searcher.SearchWindowsElement(element, windowsChildElementSearchSpec, log);
            Assert.IsNotNull(element, "Wordpad document not found");
            element.Click();
            element.SendKeys(Keys.Control + 'o' + Keys.Control);
            var          sut      = vContainer.Resolve <IPressEnterAgent>();
            var          fileName = new Folder(Path.GetTempPath()).FullName + @"\PressEnter.txt";
            const string testText = "It worked!";

            File.WriteAllText(fileName, testText);
            Assert.IsTrue(sut.EnterFileNameAndPressEnter(fileName, "", log));
            Assert.IsTrue(element.Text.Contains(testText));
        }
Пример #11
0
        private static bool DoesElementMatchSearchSpec(AppiumWebElement element, WindowsElementSearchSpec windowsElementSearchSpec, int depth, ICollection <string> log, string parentElementIds, out AppiumWebElement elementOrMatchingChildElement)
        {
            if (parentElementIds.Contains(element.Id + ';'))
            {
                throw new Exception($"Element {element.Id} already is a parent element");
            }

            parentElementIds += element.Id + ';';

            elementOrMatchingChildElement = element;
            if (depth > 20)
            {
                throw new Exception($"Search depth exceeds {depth}");
            }

            var reverseSearchSpec = new WindowsElementSearchSpec(element);

            log.Add($"Checking {windowsElementSearchSpec} against {reverseSearchSpec} at depth {depth}");
            if (windowsElementSearchSpec.ClassNames.Any() && !windowsElementSearchSpec.ClassNames.Contains(element.GetClassName()))
            {
                log.Add($"Mismatch, class name is {element.GetClassName() ?? "empty"}");
                return(false);
            }

            if (windowsElementSearchSpec.NameMustNotBeEmpty)
            {
                if (string.IsNullOrWhiteSpace(element.GetName()))
                {
                    log.Add("Mismatch, name must not be empty");
                    return(false);
                }
            }

            if (!string.IsNullOrWhiteSpace(windowsElementSearchSpec.Name) && element.GetName() != windowsElementSearchSpec.Name)
            {
                log.Add($"Mismatch, name is {element.GetName() ?? "empty"}");
                return(false);
            }

            if (!string.IsNullOrWhiteSpace(windowsElementSearchSpec.NameContains))
            {
                var elementName = element.GetName();
                if (elementName?.Contains(windowsElementSearchSpec.NameContains) != true)
                {
                    log.Add($"Mismatch, name {element.GetName() ?? "empty"} does not contain {windowsElementSearchSpec.NameContains}");
                    return(false);
                }
            }

            foreach (var nameDoesNotContain in windowsElementSearchSpec.NameDoesNotContain.Where(n => element.GetName()?.Contains(n) == true))
            {
                log.Add($"Mismatch, name {element.GetName()} contains {nameDoesNotContain}");
                return(false);
            }

            log.Add($"Element is indeed {windowsElementSearchSpec}, checking child search specifications");
            if (!windowsElementSearchSpec.WindowsChildElementSearchSpecs.All(
                    spec => {
                var elements = new List <AppiumWebElement>();
                if (!string.IsNullOrWhiteSpace(spec.Name))
                {
                    elements = element.FindElementsByName(spec.Name).ToList();
                }
                else if (spec.ClassNames.Any())
                {
                    spec.ClassNames.ForEach(n => elements.AddRange(element.FindElementsByClassName(n)));
                }
                else
                {
                    throw new ArgumentException($"Invalid {nameof(WindowsElementSearchSpec)}, name or class name is required");
                }
                var reverseSearchSpecs = elements.Select(e => new WindowsElementSearchSpec(e)).ToList();
                log.Add(elements.Any()
                            ? $"[{spec}] applied to parent element {new WindowsElementSearchSpec(element)} resulted in {elements.Count} elements: {string.Join(", ", reverseSearchSpecs)}"
                            : $"[{spec}] applied to parent element {new WindowsElementSearchSpec(element)} did not yield any results");
                return(elements.Any(e => DoesElementMatchSearchSpec(e, spec, depth + 1, log, parentElementIds, out _)));
            }))
            {
                log.Add($"Child specifications not met for '{elementOrMatchingChildElement.GetName()}' of class '{elementOrMatchingChildElement.GetClassName()}' at depth {depth}");
                return(false);
            }

            if (elementOrMatchingChildElement == null)
            {
                throw new Exception("Child element matches search specification, but no element is returned");
            }
            log.Add($"Found '{elementOrMatchingChildElement.GetName()}' of class '{elementOrMatchingChildElement.GetClassName()}' at depth {depth}");
            return(true);
        }
Пример #12
0
        protected AppiumWebElement SearchWindowsElement(WindowsDriver <AppiumWindowsElement> desktopSession, AppiumWebElement parentElement, WindowsElementSearchSpec windowsElementSearchSpec, IList <string> log)
        {
            log.Clear();
            var elements = new List <AppiumWebElement>();

            if (desktopSession != null && !string.IsNullOrWhiteSpace(windowsElementSearchSpec.Name))
            {
                elements = desktopSession.FindElementsByName(windowsElementSearchSpec.Name).Cast <AppiumWebElement>().ToList();
            }
            else if (desktopSession != null && windowsElementSearchSpec.ClassNames.Any())
            {
                windowsElementSearchSpec.ClassNames.ForEach(n => elements.AddRange(desktopSession.FindElementsByClassName(n)));
            }
            else if (!string.IsNullOrWhiteSpace(windowsElementSearchSpec.Name))
            {
                elements = parentElement.FindElementsByName(windowsElementSearchSpec.Name).ToList();
            }
            else if (windowsElementSearchSpec.ClassNames.Any())
            {
                windowsElementSearchSpec.ClassNames.ForEach(n => elements.AddRange(parentElement.FindElementsByClassName(n)));
            }
            else
            {
                throw new ArgumentException($"Invalid {nameof(WindowsElementSearchSpec)}, name or class name is required");
            }
            var reverseSearchSpecs = elements.Select(e => new WindowsElementSearchSpec(e));

            log.Add(elements.Any()
                ? $"[{windowsElementSearchSpec}] applied to root element resulted in {elements.Count} elements: {string.Join(", ", reverseSearchSpecs)}"
                : $"[{windowsElementSearchSpec}] applied to root element did not yield any results");

            foreach (var element in elements)
            {
                if (!DoesElementMatchSearchSpec(element, windowsElementSearchSpec, 0, log, "", out var elementOrMatchingChildElement))
                {
                    continue;
                }

                if (elementOrMatchingChildElement == null)
                {
                    throw new Exception("Element matches search specification, but no element is returned");
                }
                return(elementOrMatchingChildElement);
            }
            return(null);
        }
Пример #13
0
 public AppiumWebElement SearchWindowsElement(AppiumWebElement parentElement, WindowsElementSearchSpec windowsElementSearchSpec, IList <string> log)
 {
     return(SearchWindowsElement(null, parentElement, windowsElementSearchSpec, log));
 }
Пример #14
0
 public AppiumWebElement SearchWindowsElement(WindowsElementSearchSpec windowsElementSearchSpec)
 {
     return(SearchWindowsElement(windowsElementSearchSpec, new List <string>()));
 }