|
| 1 | +using OpenQA.Selenium.Appium; |
| 2 | +using OpenQA.Selenium; |
| 3 | +using NUnit.Framework; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace Microsoft.ML.OnnxRuntime.Tests.BrowserStack.Android |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// This class contains a single test: RunAll, which interacts with the UI from |
| 14 | + /// https://github.com/mattleibow/DeviceRunners/tree/main by clicking the "Run All" button and checking the number |
| 15 | + /// of passed and failed tests. |
| 16 | + /// |
| 17 | + /// It searches for elements on the page using Appium's WebDriver. These searches use the XPath attributes. |
| 18 | + /// |
| 19 | + /// Launching the MAUI test app in Appium Inspector will allow you to see the exact XPath attributes for each |
| 20 | + /// element. |
| 21 | + /// </summary> |
| 22 | + [TestFixture] |
| 23 | + public class RunAllTest : BrowserStackTest |
| 24 | + { |
| 25 | + public AppiumElement FindAppiumElement(String xpathQuery, String text) |
| 26 | + { |
| 27 | + IReadOnlyCollection<AppiumElement> appiumElements = driver.FindElements(By.XPath(xpathQuery)); |
| 28 | + |
| 29 | + foreach (var element in appiumElements) |
| 30 | + { |
| 31 | + if (element.Text.Contains(text)) |
| 32 | + { |
| 33 | + return element; |
| 34 | + } |
| 35 | + } |
| 36 | + // was unable to find given element |
| 37 | + throw new Exception(String.Format("Could not find {0}: {1} on the page.", xpathQuery, text)); |
| 38 | + } |
| 39 | + |
| 40 | + public AppiumElement FindAppiumElementThenClick(String xpathQuery, String text) |
| 41 | + { |
| 42 | + AppiumElement appiumElement = FindAppiumElement(xpathQuery, text); |
| 43 | + appiumElement.Click(); |
| 44 | + return appiumElement; |
| 45 | + } |
| 46 | + |
| 47 | + public (int, int) GetPassFailCount() |
| 48 | + { |
| 49 | + int numPassed = -1; |
| 50 | + int numFailed = -1; |
| 51 | + |
| 52 | + IReadOnlyCollection<AppiumElement> labelElements = |
| 53 | + driver.FindElements(By.XPath("//android.widget.TextView")); |
| 54 | + |
| 55 | + for (int i = 0; i < labelElements.Count; i++) |
| 56 | + { |
| 57 | + AppiumElement element = labelElements.ElementAt(i); |
| 58 | + |
| 59 | + if (element.Text.Equals("✔")) |
| 60 | + { |
| 61 | + i++; |
| 62 | + numPassed = int.Parse(labelElements.ElementAt(i).Text); |
| 63 | + } |
| 64 | + |
| 65 | + if (element.Text.Equals("⛔")) |
| 66 | + { |
| 67 | + i++; |
| 68 | + numFailed = int.Parse(labelElements.ElementAt(i).Text); |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + Assert.That(numPassed, Is.GreaterThanOrEqualTo(0), "Could not find number passed label."); |
| 74 | + Assert.That(numFailed, Is.GreaterThanOrEqualTo(0), "Could not find number failed label."); |
| 75 | + |
| 76 | + return (numPassed, numFailed); |
| 77 | + } |
| 78 | + |
| 79 | + [Test] |
| 80 | + public async Task ClickRunAllTest() |
| 81 | + { |
| 82 | + // XAML for the main page: |
| 83 | + // https://github.com/mattleibow/DeviceRunners/blob/cba7644e07b305ba64dc930b01c3eee55ef2b93d/src/DeviceRunners.VisualRunners.Maui/App/Pages/HomePage.xaml |
| 84 | + AppiumElement runAllButton = FindAppiumElementThenClick("//android.widget.Button", "Run All"); |
| 85 | + |
| 86 | + while (!runAllButton.Enabled) |
| 87 | + { |
| 88 | + // waiting for unit tests to execute |
| 89 | + await Task.Delay(500); |
| 90 | + } |
| 91 | + |
| 92 | + var (numPassed, numFailed) = GetPassFailCount(); |
| 93 | + |
| 94 | + if (numFailed == 0) |
| 95 | + { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + // click into test results if tests have failed |
| 100 | + FindAppiumElementThenClick("//android.widget.TextView", "⛔"); |
| 101 | + await Task.Delay(500); |
| 102 | + |
| 103 | + // Brings you to the test assembly page |
| 104 | + // XAML for test assembly page: |
| 105 | + // https://github.com/mattleibow/DeviceRunners/blob/cba7644e07b305ba64dc930b01c3eee55ef2b93d/src/DeviceRunners.VisualRunners.Maui/App/Pages/TestAssemblyPage.xaml |
| 106 | + FindAppiumElementThenClick("//android.widget.EditText", "All"); |
| 107 | + await Task.Delay(100); |
| 108 | + FindAppiumElementThenClick("//android.widget.TextView", "Failed"); |
| 109 | + await Task.Delay(500); |
| 110 | + |
| 111 | + StringBuilder sb = new StringBuilder(); |
| 112 | + sb.AppendLine("PASSED TESTS: " + numPassed + " | FAILED TESTS: " + numFailed); |
| 113 | + |
| 114 | + IReadOnlyCollection<AppiumElement> textResults = driver.FindElements(By.XPath("//android.widget.TextView")); |
| 115 | + foreach (var element in textResults) |
| 116 | + { |
| 117 | + sb.AppendLine(element.Text); |
| 118 | + } |
| 119 | + |
| 120 | + Assert.That(numFailed, Is.EqualTo(0), sb.ToString()); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments