Skip to content
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
6 changes: 6 additions & 0 deletions maestro-cli/src/main/java/maestro/cli/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ class App {
@Option(names = ["--port"], hidden = true)
var port: Int? = null

@Option(
names = ["--driver-host-port"],
description = ["AndroidDriver host port for instrumentation communication (default: 7001)"]
)
var driverHostPort: Int? = null

@Option(
names = ["--device", "--udid"],
description = ["(Optional) Device ID to run on explicitly, can be a comma separated list of IDs: --device \"Emulator_1,Emulator_2\" "],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class PrintHierarchyCommand : Runnable {
MaestroSessionManager.newSession(
host = parent?.host,
port = parent?.port,
driverHostPort = null,
driverHostPort = parent?.driverHostPort,
teamId = appleTeamId,
deviceId = parent?.deviceId,
platform = parent?.platform,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class QueryCommand : Runnable {
MaestroSessionManager.newSession(
host = parent?.host,
port = parent?.port,
driverHostPort = null,
driverHostPort = parent?.driverHostPort,
deviceId = parent?.deviceId,
platform = parent?.platform,
teamId = appleTeamId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class RecordCommand : Callable<Int> {
return MaestroSessionManager.newSession(
host = parent?.host,
port = parent?.port,
driverHostPort = null,
driverHostPort = parent?.driverHostPort,
deviceId = deviceId,
teamId = appleTeamId,
platform = parent?.platform,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class StartDeviceCommand : Callable<Int> {
PrintUtils.message(if (p == Platform.IOS) "Launching simulator..." else "Launching emulator...")
DeviceService.startDevice(
device = device,
driverHostPort = parent?.port
driverHostPort = parent?.driverHostPort
)
}
} catch (e: LocaleValidationIosException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class StudioCommand : Callable<Int> {
MaestroSessionManager.newSession(
host = parent?.host,
port = parent?.port,
driverHostPort = null,
driverHostPort = parent?.driverHostPort,
teamId = appleTeamId,
deviceId = parent?.deviceId,
platform = parent?.platform,
Expand Down
25 changes: 21 additions & 4 deletions maestro-cli/src/main/java/maestro/cli/command/TestCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import maestro.cli.util.CiUtils
import maestro.cli.util.EnvUtils
import maestro.cli.util.FileUtils.isWebFlow
import maestro.cli.util.PrintUtils
import maestro.cli.util.isPortAvailable
import maestro.cli.insights.TestAnalysisManager
import maestro.cli.view.greenBox
import maestro.cli.view.box
Expand Down Expand Up @@ -503,11 +504,27 @@ class TestCommand : Callable<Int> {
}
}

private fun selectPort(effectiveShards: Int): Int =
if (effectiveShards == 1) 7001
else (7001..7128).shuffled().find { port ->
usedPorts.putIfAbsent(port, true) == null
private fun selectPort(effectiveShards: Int): Int {
// If user specified driver host port via CLI, use it
parent?.driverHostPort?.let { port ->
if (!isPortAvailable(port)) {
throw CliError("Port $port is already in use. Please specify a different port with --driver-host-port")
}
return port
}

// Otherwise use default behavior
if (effectiveShards == 1) {
if (!isPortAvailable(7001)) {
throw CliError("Default port 7001 is already in use. Use --driver-host-port to specify a different port")
}
return 7001
}

return (7001..7128).shuffled().find { port ->
usedPorts.putIfAbsent(port, true) == null && isPortAvailable(port)
Comment thread
Fishbowler marked this conversation as resolved.
} ?: error("No available ports found")
Comment thread
Fishbowler marked this conversation as resolved.
}

private fun runSingleFlow(
maestro: Maestro,
Expand Down
8 changes: 8 additions & 0 deletions maestro-cli/src/main/java/maestro/cli/util/SocketUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ fun getFreePort(): Int {
} catch (ignore: Exception) {}
}
ServerSocket(0).use { return it.localPort }
}

fun isPortAvailable(port: Int): Boolean {
return try {
ServerSocket(port).use { true }
} catch (e: Exception) {
false
}
}
Loading