Hi,
I have a .NET MAUI App which should actually be launched after I provided a a TestDatabase File via PushFile(…) so that it can be used in the iOS-Simulator and App instance that is created for the test.
When I use the following method, the App is launched instantly when the new IOSDriver(…) instance is created:
var options = new AppiumOptions
{
PlatformName = "iOS",
PlatformVersion = "18.2", // Specify the iOS version of the simulator or device
DeviceName = "iPhone 16 Pro Max", // Specify the device name
AutomationName = "XCUITest",
App = "/Users/mauioxo/Library/Caches/Xamarin/mtbs/builds/MyApp/81ca9a5b7d14b275e14ee3337e9324be32a9316344637117fb3208acc66ff091/bin/Debug/net9.0-ios/iossimulator-arm64/MyApp.app"
};
// options.AddAdditionalAppiumOption("autoLaunch", false); // Don't start the App
var processArgs = new Dictionary<string, object>
{
{ "args", new[] { "-isTestExecution", "true" } }
};
options.AddAdditionalAppiumOption("processArguments", processArgs);
driver = new IOSDriver(_appiumService.ServiceUrl, options);
In my AppDelegate.cs
(used for iOS platform code), I can see the provided Argument -isTestExecution
and also its value true
:
private void SetTestExecutionState()
{
// Retrieve the command-line arguments
var args = NSProcessInfo.ProcessInfo.Arguments;
bool isTestExecution = false;
// Check if the argument "-isTestExecution" exists and if
// the argument is found check if a subsequent value exists
int index = Array.IndexOf(args, "-isTestExecution");
if (index >= 0 && index + 1 < args.Length)
{
bool.TryParse(args[index + 1], out isTestExecution);
}
// Obtain the AppExecutionState service and update its IsTestExecution property
var appExecutionState = ServiceHelper.GetService<AppExecutionState>();
if (appExecutionState != null)
{
appExecutionState.IsTestExecution = isTestExecution;
}
}
Problem with this code is, that I am not able to use PushFile(...)
and access the IsTestExecution
flag when my App is launched.
Therfore, I tried another approach and used the following method which sets autoLaunch
to false
:
var options = new AppiumOptions
{
PlatformName = "iOS",
PlatformVersion = "18.2", // Specify the iOS version of the simulator or device
DeviceName = "iPhone 16 Pro Max", // Specify the device name
AutomationName = "XCUITest",
App = "/Users/mauioxo/Library/Caches/Xamarin/mtbs/builds/MyApp/81ca9a5b7d14b275e14ee3337e9324be32a9316344637117fb3208acc66ff091/bin/Debug/net9.0-ios/iossimulator-arm64/MyApp.app"
};
options.AddAdditionalAppiumOption("autoLaunch", false); // Don't start the App
driver = new IOSDriver(_appiumService.ServiceUrl, options);
DeployTestDatabaseToEmulator();
var launchAppArgs = new Dictionary<string, object>
{
{ "bundleId", "com.myapp.app" },
{ "args", new[] { "-isTestExecution", "true" } }
};
driver.ExecuteScript("mobile: launchApp", launchAppArgs);
The App seems to be launched after I executed my method DeployTestDatabaseToEmulator()
.
But obviously, in my AppDelegate.cs
method, I can’t find the argument -isTestExecution
and true
anymore.
I also tried using arguments
instead of args
, but then the driver.ExecuteScript(...)
call crashes.
Is there a way to get my database file pushed to the iOS Emulator and still being able to access the launchApp
arguments?