-
Notifications
You must be signed in to change notification settings - Fork 318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add start session command to increase speed of tests #1902
Draft
Almouro
wants to merge
1
commit into
mobile-dev-inc:main
Choose a base branch
from
bamlab:feat/start-session
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+125
−4
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/usr/bin/env sh | ||
|
||
set -eu | ||
|
||
# The point of this test is to ensure that the 'maestro start-session' works correctly | ||
# - Running it should reinstall Maestro APKs on device | ||
# - Running 'maestro test' while a 'maestro start-session' is open should not reinstall Maestro APKs | ||
|
||
command -v maestro >/dev/null 2>&1 || { echo "maestro is required" && exit 1; } | ||
|
||
[ "$(basename "$PWD")" = "e2e" ] || { echo "must be run from e2e directory" && exit 1; } | ||
|
||
get_maestro_apk_install_time() { | ||
adb shell dumpsys package dev.mobile.maestro | grep "lastUpdateTime" | awk -F= '{print $2}' | ||
} | ||
|
||
start_maestro_session() { | ||
LOG_FILE=$(mktemp) | ||
MAX_TIMEOUT=60 | ||
START_TIME=$(date +%s) | ||
|
||
# Start the Maestro session in the background and redirect output to logs.txt | ||
echo "Starting Maestro session..." | ||
maestro --device emulator-5554 start-session > "$LOG_FILE" 2>&1 & | ||
MAESTRO_START_SESSION_PID=$! | ||
trap "kill -9 $MAESTRO_START_SESSION_PID" EXIT | ||
|
||
# Loop to check for the "Maestro session started." message | ||
while true; do | ||
if grep -q "Maestro session started." "$LOG_FILE"; then | ||
echo "Maestro session started successfully." | ||
break | ||
fi | ||
CURRENT_TIME=$(date +%s) | ||
ELAPSED_TIME=$((CURRENT_TIME - START_TIME)) | ||
|
||
if [ "$ELAPSED_TIME" -ge "$MAX_TIMEOUT" ]; then | ||
echo "Timed out waiting for Maestro session to start." | ||
break | ||
fi | ||
sleep 1 | ||
done | ||
} | ||
|
||
# Test Start session doesn't reinstall APKs | ||
PREVIOUS_INSTALL_TIME=$(get_maestro_apk_install_time) | ||
start_maestro_session | ||
|
||
# Ensure maestro start-session did reinstall APKs | ||
AFTER_START_SESSION_INSTALL_TIME=$(get_maestro_apk_install_time) | ||
|
||
if [ "$PREVIOUS_INSTALL_TIME" = "$AFTER_START_SESSION_INSTALL_TIME" ]; then | ||
echo "'maestro start-session' didn't reinstall APKs while it should have" | ||
exit 1 | ||
fi | ||
|
||
# Ensure subsequent maestro test do not reinstall APKs | ||
# We don't really care if the test fails or not since it should be taken care of in run_tests | ||
maestro --device emulator-5554 test ./flows/nowinandroid/bookmarks.yaml || FAILED=true | ||
AFTER_TEST_INSTALL_TIME=$(get_maestro_apk_install_time) | ||
|
||
if [ "$AFTER_START_SESSION_INSTALL_TIME" != "$AFTER_TEST_INSTALL_TIME" ]; then | ||
echo "'maestro test' reinstalled APKs while it shouldn't have" | ||
exit 1 | ||
fi | ||
|
||
echo $PREVIOUS_INSTALL_TIME | ||
echo $AFTER_START_SESSION_INSTALL_TIME | ||
echo $AFTER_TEST_INSTALL_TIME |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
maestro-cli/src/main/java/maestro/cli/command/StartSessionCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package maestro.cli.command | ||
|
||
import maestro.cli.App | ||
import maestro.cli.CliError | ||
import maestro.cli.DisableAnsiMixin | ||
import maestro.cli.ShowHelpMixin | ||
import maestro.cli.device.Platform | ||
import maestro.cli.session.MaestroSessionManager | ||
import maestro.cli.util.DeviceConfigIos.device | ||
import picocli.CommandLine | ||
import picocli.CommandLine.Command | ||
|
||
@Command( | ||
name = "start-session", | ||
description = [ | ||
"Setup Maestro session once, so you can run 'maestro test' faster since the setup is already done.", | ||
], | ||
hidden = true | ||
) | ||
class StartSessionCommand : Runnable { | ||
@CommandLine.Mixin | ||
var disableANSIMixin: DisableAnsiMixin? = null | ||
|
||
@CommandLine.Mixin | ||
var showHelpMixin: ShowHelpMixin? = null | ||
|
||
@CommandLine.ParentCommand | ||
private val parent: App? = null | ||
|
||
override fun run() { | ||
MaestroSessionManager.newSession( | ||
host = parent?.host, | ||
port = parent?.port, | ||
driverHostPort = parent?.port, | ||
deviceId = parent?.deviceId | ||
) { session -> | ||
/** | ||
* As of now on iOS, the session gets started but this has no impact on subsequent tests | ||
* running maestro test after the session would still install the XCTest runner | ||
*/ | ||
if (session.device?.platform != Platform.ANDROID) { | ||
throw CliError ("This command is only supported for Android devices.") | ||
} | ||
|
||
println("Maestro session started. Keep this running in the background.") | ||
println("Run your tests with `maestro test <test.yaml>` in another terminal.") | ||
|
||
while (!Thread.interrupted()) { | ||
Thread.sleep(100) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned, I had to remove 2 lines of code: I have to be honest, I didn't manage to understand their purpose. 😓
So I'd love some help from @sdfgsdfgd or @igorsmotto to know why they're needed. 🙏
I've tested
maestro test -s 2 tests
locally (including somelaunchApp
) and didn't run into any issues, but likely I've missed something.Here the issue is that since
maestro test
has found an active session, it didn't "open" the Android driver since it doesn't need to.But this condition would force it to reopen when using
launchApp
which defeats the purpose ofmaestro start-session