Skip to content

Add support for the --reinstall-driver option to AndroidDriver #2520

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class PrintHierarchyCommand : Runnable {

@CommandLine.Option(
names = ["--reinstall-driver"],
description = ["[Beta] Reinstalls xctestrunner driver before running the test. Set to false if the driver shouldn't be reinstalled"],
description = ["[Beta] Reinstalls iOS/Android driver before printing the hierarchy. Set to false if the driver shouldn't be reinstalled"],
hidden = true
)
private var reinstallDriver: Boolean = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class TestCommand : Callable<Int> {

@Option(
names = ["--reinstall-driver"],
description = ["[Beta] Reinstalls xctestrunner driver before running the test. Set to false if the driver shouldn't be reinstalled"],
description = ["[Beta] Reinstalls iOS/Android driver before running the test. Set to false if the driver shouldn't be reinstalled"],
hidden = true
)
private var reinstallDriver: Boolean = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,10 @@ object MaestroSessionManager {
selectedDevice.device != null -> MaestroSession(
maestro = when (selectedDevice.device.platform) {
Platform.ANDROID -> createAndroid(
selectedDevice.device.instanceId,
!connectToExistingSession,
driverHostPort,
instanceId = selectedDevice.device.instanceId,
reinstallDriver = reinstallDriver,
openDriver = !connectToExistingSession,
driverHostPort = driverHostPort,
)

Platform.IOS -> createIOS(
Expand Down Expand Up @@ -313,6 +314,7 @@ object MaestroSessionManager {

private fun createAndroid(
instanceId: String,
reinstallDriver: Boolean = true,
openDriver: Boolean,
driverHostPort: Int?,
): Maestro {
Expand All @@ -323,6 +325,7 @@ object MaestroSessionManager {
?: Dadb.discover()
?: error("Unable to find device with id $instanceId"),
hostPort = driverHostPort,
reinstallDriver = reinstallDriver,
emulatorName = instanceId,
)

Expand Down
53 changes: 35 additions & 18 deletions maestro-client/src/main/java/maestro/drivers/AndroidDriver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ private const val DefaultDriverHostPort = 7001
class AndroidDriver(
private val dadb: Dadb,
hostPort: Int? = null,
private val reinstallDriver: Boolean = true,
private var emulatorName: String = "",
private val metricsProvider: Metrics = MetricsProvider.getInstance(),
) : Driver {
Expand Down Expand Up @@ -95,6 +96,20 @@ class AndroidDriver(

override fun open() {
allocateForwarder()

if (!reinstallDriver) {
// Try to connect to already running instrumentation session
try {
LOGGER.info("Attempting to connect to existing instrumentation session")
awaitLaunch(timeout = 1)
LOGGER.info("Connected to existing instrumentation session")
Thread.sleep(100)
return
} catch (timeout: AndroidDriverTimeoutException) {
LOGGER.info("Failed to connect to existing instrumentation session")
}
}

installMaestroApks()
startInstrumentationSession(hostPort)

Expand Down Expand Up @@ -155,10 +170,10 @@ class AndroidDriver(
PORT_TO_ALLOCATION_POINT[hostPort] = Exception().stackTraceToString()
}

private fun awaitLaunch() {
private fun awaitLaunch(timeout: Long = getStartupTimeout()) {
val startTime = System.currentTimeMillis()

while (System.currentTimeMillis() - startTime < getStartupTimeout()) {
while (System.currentTimeMillis() - startTime < timeout) {
runCatching {
dadb.open("tcp:$hostPort").close()
return
Expand All @@ -179,26 +194,28 @@ class AndroidDriver(
isLocationMocked = false
}

LOGGER.info("[Start] close port forwarder")
PORT_TO_FORWARDER[hostPort]?.close()
LOGGER.info("[Done] close port forwarder")
if (reinstallDriver) {
LOGGER.info("[Start] close port forwarder")
PORT_TO_FORWARDER[hostPort]?.close()
LOGGER.info("[Done] close port forwarder")

LOGGER.info("[Start] Remove host port from port forwarder map")
PORT_TO_FORWARDER.remove(hostPort)
LOGGER.info("[Done] Remove host port from port forwarder map")
LOGGER.info("[Start] Remove host port from port forwarder map")
PORT_TO_FORWARDER.remove(hostPort)
LOGGER.info("[Done] Remove host port from port forwarder map")

LOGGER.info("[Start] Remove host port from port to allocation map")
PORT_TO_ALLOCATION_POINT.remove(hostPort)
LOGGER.info("[Done] Remove host port from port to allocation map")
LOGGER.info("[Start] Remove host port from port to allocation map")
PORT_TO_ALLOCATION_POINT.remove(hostPort)
LOGGER.info("[Done] Remove host port from port to allocation map")

LOGGER.info("[Start] Uninstall driver from device")
uninstallMaestroApks()
LOGGER.info("[Done] Uninstall driver from device")
LOGGER.info("[Start] Uninstall driver from device")
uninstallMaestroApks()
LOGGER.info("[Done] Uninstall driver from device")

LOGGER.info("[Start] Close instrumentation session")
instrumentationSession?.close()
instrumentationSession = null
LOGGER.info("[Done] Close instrumentation session")
LOGGER.info("[Start] Close instrumentation session")
instrumentationSession?.close()
instrumentationSession = null
LOGGER.info("[Done] Close instrumentation session")
}

LOGGER.info("[Start] Shutdown GRPC channel")
channel.shutdown()
Expand Down
Loading