android开发笔记之Android Studio运行UiAutomator

本文详细介绍如何使用UIAutomator进行Android应用自动化测试,包括设置依赖、编写测试案例及执行测试的方法。通过具体实例展示了计算器操作和应用内交互测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简历

UiAutomator是android的一种自动化测试工具, 其优点是可以对所有应用进行跨应用的操作自动化测试.

uiautomator的实现是在Android Studio应用下的androidTest目录下:

这里写图片描述

Demo

第一步:在build.gradle文件中添加uiautomator:

// Set this dependency to build and run UI Automator tests
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.0'

第二步:实现具体的测试

package android.com.uiautomatortestdemo;

import android.support.test.uiautomator.UiAutomatorTestCase;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import java.io.IOException;

public class ExampleInstrumentedTest extends UiAutomatorTestCase{

    private static final int longPressCameraCount = 20;

    public void testDemo() throws UiObjectNotFoundException, IOException {
        //testCalculator();
        //testCamera();
        testMyDemoApp();
    }


    private void testCalculator() throws UiObjectNotFoundException{
        getUiDevice().pressHome();
        //UiObject Calculator = new UiObject(new UiSelector().description("计算器"));
        UiObject Calculator = new UiObject(new UiSelector().description("Calculator"));

        Calculator.clickAndWaitForNewWindow();
        //UiObject seven = new UiObject(new UiSelector().resourceId(
        // "com.android.calculator2:id/digit7"));
        UiObject seven = new UiObject(new UiSelector().resourceId(
                "com.ape.calculator2:id/digit7"));
        seven.click();

        //UiObject plus = new UiObject(new UiSelector().resourceId(
        // "com.android.calculator2:id/plus"));
        UiObject plus = new UiObject(new UiSelector().resourceId(
                "com.ape.calculator2:id/plus"));
        plus.click();
        //UiObject one = new UiObject(new UiSelector().resourceId(
        // "com.android.calculator2:id/digit1"));
        UiObject one = new UiObject(new UiSelector().resourceId(
                "com.ape.calculator2:id/digit1"));
        one.click();
        //UiObject result = new UiObject(new UiSelector().resourceId(
        // "com.android.calculator2:id/equal"));
        UiObject result = new UiObject(new UiSelector().resourceId(
                "com.ape.calculator2:id/equal"));
        result.click();

        getUiDevice().pressBack();
    }

    private void testCamera() throws IOException, UiObjectNotFoundException {
        for(int i =0;i<longPressCameraCount;i++){
            getUiDevice().pressHome();
            String command = "am start -n com.myos.camera/.activity.CameraActivity";
            getUiDevice().executeShellCommand(command);
            sleep(1000);
            UiObject photo_shutter_button_photo = new UiObject(new UiSelector().resourceId(
                    "com.myos.camera:id/photo_shutter_button_photo"));
            //photo_shutter_button_photo.longClick();
            getUiDevice().swipe(
                    photo_shutter_button_photo.getBounds().centerX(),
                    photo_shutter_button_photo.getBounds().centerY(),
                    photo_shutter_button_photo.getBounds().centerX(),
                    photo_shutter_button_photo.getBounds().centerY(),
                    50*8);//最后一个参数单位是5ms
            getUiDevice().pressBack();
        }
    }

    private void testMyDemoApp() throws IOException, UiObjectNotFoundException {
        getUiDevice().pressHome();
        String command = "am start -n android.com.debugdemo/.MainActivity";
        getUiDevice().executeShellCommand(command);
        sleep(2000);
        UiObject testRecyclerView = new UiObject(new UiSelector().resourceId(
                "android.com.debugdemo:id/testRecyclerView"));
        testRecyclerView.click();
        sleep(2000);
        UiObject switchButton = new UiObject(new UiSelector().resourceId(
                "android.com.debugdemo:id/switchButton"));
        UiObject id_recyclerview = new UiObject(new UiSelector().resourceId(
                "android.com.debugdemo:id/id_recyclerview"));
        id_recyclerview.swipeUp(2);
        sleep(2000);
        switchButton.click();
        id_recyclerview.swipeUp(2);
        sleep(2000);
        switchButton.click();
        sleep(2000);
        id_recyclerview.swipeUp(2);
        sleep(2000);
        getUiDevice().pressBack();
    }

}

第三步:运行,右键你的测试类,Run

第四步:如果后期还需要运行测试用例,可以通过如下的adb命令调用:

android.com.uiautomatortestdemo.ExampleInstrumentedTest android.com.uiautomatortestdemo.test/android.support.test.runner.AndroidJUnitRunner

备注

此自动化测试经常需要uiautomatorviewer工具来配合,uiautomatorviewer的使用如下:

/SDK/tools$ ./uiautomatorviewer

参考资料

1.在Android Sudio中使用Uiautomator
http://blog.youkuaiyun.com/cxq234843654/article/details/51203143

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hfreeman2008

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值