【Android测试】AndroidStudio通过UIAutomator进行测试

目录(?)[+]

在eclipse中使用UIAutomator时步骤较为繁琐,而在Studio环境下只需要添加引用,编写测试类,运行测试类就可以实现。步骤较简单。

1.配置环境

在包依赖中添加对UIAutomator的引用,这里需要注意,引用包版本不能高于最低编译版本,也就是 minSdkVersion 的值,期初我的 minSdkVersion 为15,与添加的v18产生冲突。

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

2.测试类的实现

在添加引用后直接在测试包下创建测试类即可。以对官方应用计算器为例进行测试。测试类需要继承自UiAutomatorTestCase。在setUp()中进行测试初始化,在以test开头的测试方法中进行操作。 
通过UiDevice可以进行手机按键的一些操作,比如休眠与唤醒,回退键等一些系统按键。 
在setUp中先通过UiDevice对象跳转到页面主页,通过点击按钮进入所有apps的界面,对界面中各种按钮等对象的获取通过UiSelector进行定位,在UiAutomator中,所有的控件都是同样的UiObject。通过对按钮的description可以获取到该按钮。对页面的控件描述都可以通过sdk/tools文件夹下的uiautomatorviewer进行页面的显示获取。 
操作到Calculator界面后通过文本内容获取控件模拟点击,最后将显示内容与预期内容进行对比。 
这里写图片描述
实现测试代码如下:

public class CalculatorAndroidTest extends UiAutomatorTestCase {

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        UiDevice device = getUiDevice();
        device.pressHome();
        UiObject allAppsButton = new UiObject(new UiSelector().description("Apps"));
        allAppsButton.clickAndWaitForNewWindow();
        UiObject cal = new UiObject(new UiSelector().text("Calculator"));
        cal.click();
    }

    public void testSum() throws UiObjectNotFoundException {
        new UiObject(new UiSelector().text("9")).click();
        new UiObject(new UiSelector().text("+")).click();
        new UiObject(new UiSelector().text("1")).click();
        new UiObject(new UiSelector().text("=")).click();

        UiObject res = new UiObject(new UiSelector().className("android.widget.EditText"));
        assertEquals("","10",res.getText());
    }

    public void testSub() throws UiObjectNotFoundException {
        new UiObject(new UiSelector().text("9")).click();
        new UiObject(new UiSelector().text("−")).click();
        new UiObject(new UiSelector().text("1")).click();
        new UiObject(new UiSelector().text("=")).click();

        UiObject res = new UiObject(new UiSelector().className("android.widget.EditText"));
        assertEquals("","8",res.getText());
    }

    public void testMul() throws UiObjectNotFoundException {
        new UiObject(new UiSelector().text("3")).click();
        new UiObject(new UiSelector().text("×")).click();
        new UiObject(new UiSelector().text("4")).click();
        new UiObject(new UiSelector().text("=")).click();

        UiObject res = new UiObject(new UiSelector().className("android.widget.EditText"));
        assertEquals("","12",res.getText());
    }

    public void testDiv() throws UiObjectNotFoundException {
        new UiObject(new UiSelector().text("9")).click();
        new UiObject(new UiSelector().text("÷")).click();
        new UiObject(new UiSelector().text("3")).click();
        new UiObject(new UiSelector().text("=")).click();

        UiObject res = new UiObject(new UiSelector().className("android.widget.EditText"));
        assertEquals("","3",res.getText());
    }
}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

最后自动测试的过程: 
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值