无法使用 appium 启动 activity

Not able to start activity using appium

我正在尝试使用 appium android 驱动程序启动透明 activity 并传递一些可选的意图参数。无论我设置多少 abdExecTimeout 功能值,它总是以 500 状态失败并且命令超时。

示例代码:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("automationName", "UiAutomator2");
capabilities.setCapability("app", "/path/to/app.apk");
capabilities.setCapability("appActivity", "com.myapp.home.splash.SplashActivity");
capabilities.setCapability("appPackage", "com.myapp.app");
capabilities.setCapability("udid","e76dcaaqs");
capabilities.setCapability("avdLaunchTimeout", 300000);
capabilities.setCapability("useKeystore", false);
capabilities.setCapability("autoGrantPermissions",true);
capabilities.setCapability("automationName", "UiAutomator2");
capabilities.setCapability("skipUnlock", true);
capabilities.setCapability("androidInstallTimeout", 180000);
capabilities.setCapability("adbExecTimeout", 60000);
capabilities.setCapability("ignoreHiddenApiPolicyError", true);
capabilities.setCapability("deviceName", "Oppo Reno");
capabilities.setCapability("appWaitForLaunch", false);
AndroidDriver<MobileElement> driver = new AndroidDriver<>(new URL("http://localhost:4723/wd/hub"), capabilities);
Activity activity = new Activity("com.myapp.app", "com.myapp.instrumentation.persistence.preferences.PreferencesOverrideActivity");
String intentParam = "--es \"overrideData\" '[{\"pref_name\": \"MyPref\",\"overrides\": " +
        "[{\"key\": \"mykey\",\"value\": \"" + obj.getValue() + "\",\"type\": \"string\"}],\"clear\": false}]'";
activity.setOptionalIntentArguments(intentParam);
//            activity.setStopApp(true);
try {
    Thread.sleep(5000);
} catch (InterruptedException e) {
    e.printStackTrace();
}
driver.startActivity(activity);
driver.launchApp();

} catch (OperationFailedException | MalformedURLException e) {
e.printStackTrace();
}

输出日志

[W3C (146a80b7)] Calling AppiumDriver.startActivity() with args: ["com.myapp.app","com.myapp.instrumentation.persistence.preferences.PreferencesOverrideActivity","com.myapp.app","com.myapp.home.splash.SplashActivity","","","","--es "overrideData" '[{"pref_name": "MyPreferences","overrides": [{"key": "myKey","value": "myValue"","type": "string"}],"clear": false}]' [AndroidDriver] Starting package 'com.myapp.app' and activity 'com.myapp.instrumentation.persistence.preferences.PreferencesOverrideActivity' [ADB] Running '/Users/user1/Library/Android/sdk/platform-tools/adb -P 5037 -s e15adapa shell am start -W -n com.myapp.app/com.myapp.instrumentation.persistence.preferences.PreferencesOverrideActivity -S --es '"overrideData"' "'[{"pref_name": "MyPreferences","overrides": [{"key": "myKey","value": "myValue","type": "string"}],"clear": false}]'"' [Instrumentation] INSTRUMENTATION_RESULT: shortMsg=Process crashed. [Instrumentation] INSTRUMENTATION_CODE: 0 [Instrumentation] The process has exited with code 0

奇怪的是,即使进程崩溃,偏好数据也会更新。

找到原因了。默认情况下,Appium 在调用 startActivity 时将 -W 添加到 adb 命令,等待 activity 启动。在我的例子中,它是透明的 activity 并且不会启动。所以最终 appium 命令超时并失败。

您需要将“appWaitForLaunch”设置为 false。

查看 here - 来自 Appium 文档:

There might be also situations where an activity does not return the control to the calling process at all, so am start call blocks forever independently of the value of appWaitDuration, thus causing the timeout. In such case setting appWaitForLaunch to false might help to resolve the issue. Although, by choosing this option, the driver cannot make sure the activity has fully started, so then it is up to the client code to verify the initial UI state is the one that is expected.