How do I change the appID based on a parameter passed on the command line when running Maestro tests?
I'm aware that it's possible to set the app ID for a maestro test suite based on an env variable (Is it possible to run a single Maestro flow against two different app ids?), but how can I programmatically set the appID based on a more simple flag as a parameter?
I'd like to be able to run tests with either maestro test . -e android or maestro test . -e iOS, and for this I'd need to take that parameter and use it to work out the appID string programatically in javascript (I think?).
I have a rough idea of the javascript code to do this (see below), but not sure how to pull the parameter for this:
output.result = function getAppId() {
// Assuming parameters passed to Maestro can be accessed from command line arguments
const args = process.argv;
const isAndroid = args.includes("android");
// Default app ID
let appId = "com.ios.appid";
// If 'android' parameter is present, change the app ID accordingly
if (isAndroid) {
appId = "com.android.appid";
}
return appId;
};
Whilst you set an appId in the meta at the top of the test, and it's a required field, you don't need to keep it. As andrew said in their answer, you can't write appId or output prior to the test being evaluated. But you probably don't need it.
If you've already got a script which sets output.appIdUnderTest, then instead of relying on the test's appId to launch the app, you could do this to launch the app:
- launchApp ${output.appIdUnderTest}
Fuller example:
whatAppAreWeTesting.js:
// expects to receive MAESTRO_TESTENV from the environment
const androidApp = "com.android.chrome"
const iosApp = "com.apple.mobilesafari"
if (MAESTRO_TESTENV === "ios") {
output.appIdUnderTest = iosApp
} else {
output.appIdUnderTest = androidApp
}
flow.yml:
appId: com.example.notused
---
- runScript: 'whatAppAreWeTesting.js'
- launchApp: ${output.appIdUnderTest}
Launch with:
#!/usr/bin/env bash
export MAESTRO_TESTENV="android"
maestro test flow.yml
I've used MAESTRO_ style env vars here to be automatically collected from the environment, but you could also run this as:
maestro test -e MAESTRO_TESTENV=android flow.yml
I've put the above in Github, here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With