onnxruntime/csharp/test/Microsoft.ML.OnnxRuntime.Tests.BrowserStack.Android/BrowserStackTest.cs
Caroline Zhu 2fc75a45a2
[mobile] Add Android BrowserStack test project back (#23551)
## Description
Follow-up for #23383 and #23474

* Adds android BrowserStack test back in
* Modifies MAUI csproj file to build into an APK


### Motivation and Context
There were 2 issues with the previous PRs:
1. The updated MAUI .csproj file configuration failed when building to
iOS and MacCatalyst. This caused problems in the packaging pipeline
because we build all C# projects in the .soln file in the packaging
pipeline. Removed the Mac & iOS build targets for now

3. The previous MAUI .csproj file configuration did not build into an
APK. It was missing the `<OutputType>` XAML tag and the Android package
type XAML tag.
2025-02-04 14:39:50 -08:00

68 lines
2.4 KiB
C#

using Newtonsoft.Json;
using NUnit.Framework.Interfaces;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Microsoft.ML.OnnxRuntime.Tests.BrowserStack.Android
{
public class BrowserStackTest
{
public AndroidDriver driver;
public BrowserStackTest()
{}
[SetUp]
public void Init()
{
var androidOptions = new AppiumOptions {
AutomationName = "UIAutomator2",
PlatformName = "Android",
};
driver = new AndroidDriver(new Uri("http://127.0.0.1:4723/wd/hub"), androidOptions);
}
/// <summary>
/// Passes the correct test status to BrowserStack and ensures the driver quits.
/// </summary>
[TearDown]
public void Dispose()
{
try
{
// According to
// https://www.browserstack.com/docs/app-automate/appium/set-up-tests/mark-tests-as-pass-fail
// BrowserStack doesn't know whether test assertions have passed or failed. Below handles
// passing the test status to BrowserStack along with any relevant information.
if (TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed)
{
String failureMessage = TestContext.CurrentContext.Result.Message;
String jsonToSendFailure =
String.Format("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": " +
"{\"status\":\"failed\", \"reason\": {0}}}",
JsonConvert.ToString(failureMessage));
((IJavaScriptExecutor)driver).ExecuteScript(jsonToSendFailure);
}
else
{
((IJavaScriptExecutor)driver)
.ExecuteScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": " +
"{\"status\":\"passed\", \"reason\": \"\"}}");
}
}
finally
{
// will run even if exception is thrown by previous block
((AndroidDriver)driver).Quit();
}
}
}
}