在Android Sudio中使用Uiautomator

http://blog.csdn.net/cxq234843654/article/details/51203143

在Android Sudio中使用Uiautomator

我使用的环境要求:

1、Android Studio 2.0

2、SDK Manager需要安装Android Support Repository,没有安装的需要自己去下,如图:




【步骤1】新建一个Android工程


不需要创建Activity



【步骤2】配置gradle(app)



内容如下:

  1. dependencies {  
  2.     compile fileTree(dir: 'libs', include: ['*.jar'])  
  3.     testCompile 'junit:junit:4.12'  
  4.     compile 'com.android.support:appcompat-v7:23.3.0'  
  5.     //引入uiautomator  
  6.     androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.0'  
  7. }  

修改gradle之后,需要同步一下,才能把uiautomator包导入,如图




【步骤3】创建TestCase

  • 在src/androidTest/java目录下创建测试类


类名可以随便取,写上你的用例,可以参照下面的例子:
  1. package com.cxq.uiautomatordemo;  
  2.   
  3. import android.support.test.uiautomator.UiAutomatorTestCase;  
  4. import android.support.test.uiautomator.UiObject;  
  5. import android.support.test.uiautomator.UiObjectNotFoundException;  
  6. import android.support.test.uiautomator.UiSelector;  
  7.   
  8. /** 
  9.  * Created by CrystalChen on 2016/4/21. 
  10.  */  
  11. public class UiTest extends UiAutomatorTestCase {  
  12.     public void testDemo() throws UiObjectNotFoundException {  
  13.         getUiDevice().pressHome();  
  14.         UiObject Calculator = new UiObject(new UiSelector().description("计算器"));  
  15.   
  16.         Calculator.clickAndWaitForNewWindow();  
  17.         UiObject seven = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/digit7"));  
  18.         seven.click();  
  19.         UiObject plus = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/plus"));  
  20.         plus.click();  
  21.         UiObject one = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/digit1"));  
  22.         one.click();  
  23.         UiObject result = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/equal"));  
  24.         result.click();  
  25.         getUiDevice().pressBack();  
  26.     }  
  27. }  




【步骤4】运行,右键你的测试类,Run
 如果按照上面的那个用例写,机子会打开计算器,自动输入7+1=
例子中的控件id会有变动,需要自行修改。


【补充】如果后期还需要运行测试用例,可以通过如下的adb命令调用

adb shell am instrument -w -r   -e debug false -e class com.cxq.uiautomatordemo.UiTest com.cxq.uiautomatordemo.test/android.test.InstrumentationTestRunner


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值