5

Does any body know how to call
am start -a ACTIVITY from uiautomator code.
Or is it possible to start activity right from junit code.

4

3 回答 3

6

这是我用来从 .jar 文件启动活动的示例:

private boolean startSettings() {
    try {
        Runtime.getRuntime().exec(
                "am start -n com.android.settings/.Settings");
        sleep(1000);
    } catch (IOException e) {
        e.printStackTrace();
    }
    for (int i = 0; i < 5; i++) {
        sleep(1000);
        if (getUiDevice().getCurrentPackageName().contains(
                "com.android.settings")) {
            return true;
        }
    }
    return false;
}

您可以修改代码以启动任何应用程序。您还可以通过为包/活动值添加参数来使该方法更通用。

于 2013-07-30T10:31:07.043 回答
5

它应该具有以下代码。我在测试中使用它。

UiDevice device = UiDevice.getInstance(getInstrumentation());
final String TARGET_PACKAGE =
        InstrumentationRegistry.getTargetContext().getPackageName();

Context context = InstrumentationRegistry.getContext();
final Intent intent = context.getPackageManager().getLaunchIntentForPackage(TARGET_PACKAGE);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
device.wait(Until.hasObject(By.pkg(TARGET_PACKAGE).depth(0)), 5000);
于 2015-12-11T06:59:20.560 回答
0

我所做的是让启动应用程序和运行 UIAutomator 测试成为构建的一部分。这就是我在构建 Ant build.xml 后运行 UIAutomator 测试的方式。此代码段添加到 build.xml 的末尾,并导致您的应用程序启动,然后启动您的 UI 测试。使用 eclipse 确保你右键单击 build.xml 然后 -> Run As -> Ant Build... 并确保选择了正确的目标:'build'、'install'、'start'、'mytest'。目标 'start' 和 'mytest' 通过以下代码段添加。

<!-- version-tag: VERSION_TAG -->
<!-- This line should already be at the end of build.xml -->
<import file="${sdk.dir}/tools/ant/uibuild.xml" />

<target name="start" description="Start App" depends="build, install">
    <echo>Starting Navigation Example</echo>

    <exec executable="${adb}" failonerror="true">
        <arg value="shell" />
        <arg value="am" />
        <arg value="start" />
        <arg value="-n" />
        <arg value="com.example.android.navigationdrawerexample/.MainActivity" />
    </exec>
</target>

<target name="mytest" description="Runs UI tests" depends="build, install, start">
    <echo>Running UI Tests</echo>
    <exec executable="${adb}" failonerror="true">
       <arg value="shell" />
       <arg value="uiautomator" />
       <arg value="runtest" />
       <arg value="${out.filename}" />
       <arg value="-c" />
       <arg value="com.example.android.navigationdrawerexample.MainTestCase" />
   </exec>
</target>
于 2013-08-12T15:35:41.140 回答