Uiautomator “am start”

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-30 04:58:09

问题


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.


回答1:


Here's an example I use to start an activity from the .jar file:

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;
}

You can modify the code to start any app. You could also make the method more generic by adding a parameter for the package/ activity value.




回答2:


It shoule be of with the following code. I use this in my test.

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);



回答3:


What I have done, is make the starting the app and running your UIAutomator tests part of the build. This is how I get my UIAutomator test to run after I build the Ant build.xml. This snippet is added to the end of the build.xml and causes your application to start, then starts your UI tests. Using eclipse make sure you go to right-click build.xml then -> Run As -> Ant Build... and make sure the correct targets are selected: 'build', 'install', 'start', 'mytest'. Targets 'start' and 'mytest' are added by following snippet.

<!-- 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>


来源:https://stackoverflow.com/questions/17941372/uiautomator-am-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!