diff --git a/packages/patrol/CHANGELOG.md b/packages/patrol/CHANGELOG.md index e61976e94..cc1baf673 100644 --- a/packages/patrol/CHANGELOG.md +++ b/packages/patrol/CHANGELOG.md @@ -1,6 +1,7 @@ ## 3.6.0 -- Added the NativeAutomator2, which allows to interact with native using selectors designated for a specific platform (AndroidSelector and IOSSelector). +- Added the NativeAutomator2, which allows to interact with native using selectors designated for a specific platform (AndroidSelector and IOSSelector) (#2132). +- Add support for --app-server-port and --test-server-port on Android (#2154). ## 3.5.2 diff --git a/packages/patrol/android/build.gradle b/packages/patrol/android/build.gradle index 8a38598e2..b966e6d97 100644 --- a/packages/patrol/android/build.gradle +++ b/packages/patrol/android/build.gradle @@ -52,6 +52,12 @@ android { defaultConfig { minSdk 21 + buildConfigField("String", "PATROL_APP_PORT", project.hasProperty('app-server-port') ? '"' + project.getProperty('app-server-port') + '"' : '""') + buildConfigField("String", "PATROL_TEST_PORT", project.hasProperty('test-server-port') ? '"' + project.getProperty('test-server-port') + '"' : '""') + } + + buildFeatures { + buildConfig true } dependencies { diff --git a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt index 7bdfa7bad..be71e0be7 100644 --- a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt +++ b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolAppServiceClient.kt @@ -16,13 +16,27 @@ class PatrolAppServiceClient { private val timeout = 2L private val timeUnit = TimeUnit.HOURS + private val defaultPort = 8082 + val port: Int + get() { + val portStr = BuildConfig.PATROL_APP_PORT + if (portStr == null) { + Logger.i("PATROL_APP_PORT is null, falling back to default ($defaultPort)") + return defaultPort + } + return portStr.toIntOrNull() ?: run { + Logger.i("PATROL_APP_PORT is not a valid integer, falling back to default ($defaultPort)") + defaultPort + } + } + constructor() { - client = Client(address = "localhost", port = 8082, timeout = timeout, timeUnit = timeUnit) + client = Client(address = "localhost", port = port, timeout = timeout, timeUnit = timeUnit) Logger.i("Created PatrolAppServiceClient: ${client.serverUrl}") } constructor(address: String) { - client = Client(address = address, port = 8082, timeout = timeout, timeUnit = timeUnit) + client = Client(address = address, port = port, timeout = timeout, timeUnit = timeUnit) Logger.i("Created PatrolAppServiceClient: ${client.serverUrl}") } diff --git a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolServer.kt b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolServer.kt index c97ed851f..73288bcf5 100644 --- a/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolServer.kt +++ b/packages/patrol/android/src/main/kotlin/pl/leancode/patrol/PatrolServer.kt @@ -1,7 +1,6 @@ package pl.leancode.patrol import android.os.ConditionVariable -import androidx.test.platform.app.InstrumentationRegistry import org.http4k.core.ContentType import org.http4k.filter.ServerFilters import org.http4k.server.Http4kServer @@ -11,25 +10,22 @@ import org.http4k.server.asServer class PatrolServer { private val defaultPort = 8081 - private val envPortKey = "PATROL_PORT" private var server: Http4kServer? = null private var automatorServer: AutomatorServer? = null val port: Int get() { - val portStr = arguments.getString(envPortKey) + val portStr = BuildConfig.PATROL_TEST_PORT if (portStr == null) { - Logger.i("$envPortKey is null, falling back to default ($defaultPort)") + Logger.i("PATROL_TEST_PORT is null, falling back to default ($defaultPort)") return defaultPort } return portStr.toIntOrNull() ?: run { - Logger.i("$envPortKey is not a valid integer, falling back to default ($defaultPort)") + Logger.i("PATROL_TEST_PORT is not a valid integer, falling back to default ($defaultPort)") defaultPort } } - private val arguments get() = InstrumentationRegistry.getArguments() - fun start() { Logger.i("Starting server...") diff --git a/packages/patrol_cli/CHANGELOG.md b/packages/patrol_cli/CHANGELOG.md index 9bac1f17e..50629613d 100644 --- a/packages/patrol_cli/CHANGELOG.md +++ b/packages/patrol_cli/CHANGELOG.md @@ -1,3 +1,6 @@ +## 2.6.5 +- Add support for --app-server-port and --test-server-port on Android (#2154). + ## 2.6.4 - Fix compatibility_checker getting stuck (#2091). diff --git a/packages/patrol_cli/lib/src/commands/build_android.dart b/packages/patrol_cli/lib/src/commands/build_android.dart index 3f8ce1a2f..7f48d97cf 100644 --- a/packages/patrol_cli/lib/src/commands/build_android.dart +++ b/packages/patrol_cli/lib/src/commands/build_android.dart @@ -33,6 +33,7 @@ class BuildAndroidCommand extends PatrolCommand { usesDartDefineOption(); usesLabelOption(); usesWaitOption(); + usesPortOptions(); usesAndroidOptions(); } @@ -124,6 +125,8 @@ class BuildAndroidCommand extends PatrolCommand { final androidOpts = AndroidAppOptions( flutter: flutterOpts, packageName: packageName, + appServerPort: super.appServerPort, + testServerPort: super.testServerPort, ); try { diff --git a/packages/patrol_cli/lib/src/commands/build_ios.dart b/packages/patrol_cli/lib/src/commands/build_ios.dart index f984dbf72..7b896438a 100644 --- a/packages/patrol_cli/lib/src/commands/build_ios.dart +++ b/packages/patrol_cli/lib/src/commands/build_ios.dart @@ -34,6 +34,7 @@ class BuildIOSCommand extends PatrolCommand { usesDartDefineOption(); usesLabelOption(); usesWaitOption(); + usesPortOptions(); usesIOSOptions(); argParser.addFlag( diff --git a/packages/patrol_cli/lib/src/commands/build_macos.dart b/packages/patrol_cli/lib/src/commands/build_macos.dart index 892e01d20..2ff8b7a5b 100644 --- a/packages/patrol_cli/lib/src/commands/build_macos.dart +++ b/packages/patrol_cli/lib/src/commands/build_macos.dart @@ -34,6 +34,7 @@ class BuildMacOSCommand extends PatrolCommand { usesDartDefineOption(); usesLabelOption(); usesWaitOption(); + usesPortOptions(); usesMacOSOptions(); } diff --git a/packages/patrol_cli/lib/src/commands/develop.dart b/packages/patrol_cli/lib/src/commands/develop.dart index f96d29756..9b9f862ee 100644 --- a/packages/patrol_cli/lib/src/commands/develop.dart +++ b/packages/patrol_cli/lib/src/commands/develop.dart @@ -50,6 +50,7 @@ class DevelopCommand extends PatrolCommand { usesDartDefineOption(); usesLabelOption(); usesWaitOption(); + usesPortOptions(); usesUninstallOption(); @@ -187,6 +188,8 @@ class DevelopCommand extends PatrolCommand { final androidOpts = AndroidAppOptions( flutter: flutterOpts, packageName: packageName, + appServerPort: super.appServerPort, + testServerPort: super.testServerPort, ); final iosOpts = IOSAppOptions( diff --git a/packages/patrol_cli/lib/src/commands/test.dart b/packages/patrol_cli/lib/src/commands/test.dart index 0c18dfd4c..4eae92dc8 100644 --- a/packages/patrol_cli/lib/src/commands/test.dart +++ b/packages/patrol_cli/lib/src/commands/test.dart @@ -46,6 +46,7 @@ class TestCommand extends PatrolCommand { usesDartDefineOption(); usesLabelOption(); usesWaitOption(); + usesPortOptions(); usesUninstallOption(); @@ -178,6 +179,8 @@ See https://github.com/leancodepl/patrol/issues/1316 to learn more. final androidOpts = AndroidAppOptions( flutter: flutterOpts, packageName: packageName, + appServerPort: super.appServerPort, + testServerPort: super.testServerPort, ); final iosOpts = IOSAppOptions( diff --git a/packages/patrol_cli/lib/src/crossplatform/app_options.dart b/packages/patrol_cli/lib/src/crossplatform/app_options.dart index 998f73002..a24376ea8 100644 --- a/packages/patrol_cli/lib/src/crossplatform/app_options.dart +++ b/packages/patrol_cli/lib/src/crossplatform/app_options.dart @@ -41,10 +41,14 @@ class AndroidAppOptions { const AndroidAppOptions({ required this.flutter, this.packageName, + required this.appServerPort, + required this.testServerPort, }); final FlutterAppOptions flutter; final String? packageName; + final int appServerPort; + final int testServerPort; String get description => 'apk with entrypoint ${basename(flutter.target)}'; @@ -116,6 +120,11 @@ class AndroidAppOptions { cmd.add('-Pdart-defines=$dartDefinesString'); } + // Add app and test server ports + cmd + ..add('-Papp-server-port=$appServerPort') + ..add('-Ptest-server-port=$testServerPort'); + return cmd; } } diff --git a/packages/patrol_cli/lib/src/runner/patrol_command.dart b/packages/patrol_cli/lib/src/runner/patrol_command.dart index 6bbcce237..688687567 100644 --- a/packages/patrol_cli/lib/src/runner/patrol_command.dart +++ b/packages/patrol_cli/lib/src/runner/patrol_command.dart @@ -104,6 +104,20 @@ abstract class PatrolCommand extends Command { ); } + void usesPortOptions() { + argParser + ..addOption( + 'test-server-port', + help: 'Port to use for server running in the test instrumentation app.', + defaultsTo: _defaultTestServerPort.toString(), + ) + ..addOption( + 'app-server-port', + help: 'Port to use for server running in the app under test.', + defaultsTo: _defaultAppServerPort.toString(), + ); + } + void usesAndroidOptions() { argParser.addOption( 'package-name', @@ -118,7 +132,6 @@ abstract class PatrolCommand extends Command { help: 'Bundle identifier of the iOS app under test.', valueHelp: 'pl.leancode.AwesomeApp', ); - _usesIOSPortOptions(); } void usesMacOSOptions() { @@ -127,21 +140,6 @@ abstract class PatrolCommand extends Command { help: 'Bundle identifier of the MacOS app under test.', valueHelp: 'pl.leancode.macos.AwesomeApp', ); - _usesIOSPortOptions(); - } - - void _usesIOSPortOptions() { - argParser - ..addOption( - 'test-server-port', - help: 'Port to use for server running in the test instrumentation app.', - defaultsTo: _defaultTestServerPort.toString(), - ) - ..addOption( - 'app-server-port', - help: 'Port to use for server running in the app under test.', - defaultsTo: _defaultAppServerPort.toString(), - ); } // Runtime-only options diff --git a/packages/patrol_cli/test/crossplatform/app_options_test.dart b/packages/patrol_cli/test/crossplatform/app_options_test.dart index 8c886f05c..5a09cbd35 100644 --- a/packages/patrol_cli/test/crossplatform/app_options_test.dart +++ b/packages/patrol_cli/test/crossplatform/app_options_test.dart @@ -16,7 +16,11 @@ void main() { flavor: null, dartDefines: {}, ); - options = const AndroidAppOptions(flutter: flutterOptions); + options = const AndroidAppOptions( + flutter: flutterOptions, + appServerPort: 1, + testServerPort: 2, + ); final invocation = options.toGradleAssembleTestInvocation(isWindows: true); @@ -26,6 +30,8 @@ void main() { r'.\gradlew.bat', ':app:assembleDebugAndroidTest', r'-Ptarget=C:\Users\john\app\integration_test\app_test.dart', + '-Papp-server-port=1', + '-Ptest-server-port=2', ]), ); }); @@ -37,7 +43,11 @@ void main() { flavor: null, dartDefines: {}, ); - options = const AndroidAppOptions(flutter: flutterOpts); + options = const AndroidAppOptions( + flutter: flutterOpts, + appServerPort: 1, + testServerPort: 2, + ); final invocation = options.toGradleAssembleTestInvocation(isWindows: false); @@ -47,6 +57,8 @@ void main() { './gradlew', ':app:assembleReleaseAndroidTest', '-Ptarget=/Users/john/app/integration_test/app_test.dart', + '-Papp-server-port=1', + '-Ptest-server-port=2', ]), ); }); @@ -66,7 +78,11 @@ void main() { flavor: 'dev', dartDefines: dartDefines, ); - options = const AndroidAppOptions(flutter: flutterOpts); + options = const AndroidAppOptions( + flutter: flutterOpts, + appServerPort: 1, + testServerPort: 2, + ); final invocation = options.toGradleAssembleTestInvocation(isWindows: true); @@ -77,6 +93,8 @@ void main() { ':app:assembleDevReleaseAndroidTest', r'-Ptarget=C:\Users\john\app\integration_test\app_test.dart', '-Pdart-defines=RU1BSUw9dXNlckBleGFtcGxlLmNvbQ==,UEFTU1dPUkQ9bnk0bmNhdA==,Zm9vPWJhcg==', + '-Papp-server-port=1', + '-Ptest-server-port=2', ]), ); }); @@ -88,7 +106,11 @@ void main() { flavor: 'dev', dartDefines: dartDefines, ); - options = const AndroidAppOptions(flutter: flutterOpts); + options = const AndroidAppOptions( + flutter: flutterOpts, + appServerPort: 1, + testServerPort: 2, + ); final invocation = options.toGradleAssembleTestInvocation(isWindows: false); @@ -99,6 +121,8 @@ void main() { ':app:assembleDevDebugAndroidTest', '-Ptarget=/Users/john/app/integration_test/app_test.dart', '-Pdart-defines=RU1BSUw9dXNlckBleGFtcGxlLmNvbQ==,UEFTU1dPUkQ9bnk0bmNhdA==,Zm9vPWJhcg==', + '-Papp-server-port=1', + '-Ptest-server-port=2', ]), ); });