Uiautomator “am start”

后端 未结 3 674
一个人的身影
一个人的身影 2021-01-03 02:47

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

相关标签:
3条回答
  • 2021-01-03 03:24

    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);
    
    0 讨论(0)
  • 2021-01-03 03:29

    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>
    
    0 讨论(0)
  • 2021-01-03 03:40

    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.

    0 讨论(0)
提交回复
热议问题