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