UIAutomator测试工具的配置以及使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010716109/article/details/51152912

运行系统条件:
1)Android Studio
2)系统版本:sdk 要求api18以及以上

配置:
1)配置build.gradle(app目录下的):加入引用库

androidTestCompile 'com.android.support.test:runner:0.2'
androidTestCompile 'com.android.support.test:rules:0.2'
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.0'
androidTestCompile 'com.android.support:support-annotations:23.2.0'

2)build.gradle(app目录下的)packagingOptions是与compileOptions、compileSdkVersion平级关系的。
android { packagingOptions { exclude 'LICENSE.txt' } }

创建测试工程:
1)在 androidTest 目录下创建一个新的测试的java类,名字随意,我这里起名字为MyFirstTest,需要注意的是必须继承自InstrumentationTestCase。
2)Alt+Insert添加必要的方法,比如下图的1,、2。其中2为必须要实现的方法,这里可以获取设备实例。

3、查看Launcher UI
连接你的Android设备到电脑上,点击home按键,进入主界面。
打开你安装Android SDK的目录,进入到 tools目录下,点击 uiautomatorviewer.bat 。启动 UI Automater Viewer后看到如下界面:

鼠标移动到不同位置,会有不同选中的框,然后右侧显示这个控件的详细信息。一般关注class、text、content-desc、这些属性,后面需要根据这些找到具体的控件。
下面是测试代码,具体语法参考:http://www.tuicool.com/articles/RVzIrm
http://wenku.baidu.com/link?url=AjD16IN1PHztrcLcEa5MoR8YYwS-mYefZW7KGRzxkLz4nYs3vSMliH3QR2dOnR3d9pPmpC6weVTF4CkjvEMauOsVBbOXJQlHzBlKpqm-PaG
编写完后,直接在java文件右键,然后run这个test文件即可。

package com.tomtop.shop;

import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;
import android.test.InstrumentationTestCase;

/**
 * author:   tc
 * date:     2016/4/12 & 16:40
 * version    1.0
 * description
 * modify by
 */
public class MyFirstTest extends InstrumentationTestCase {
    private UiDevice device;
    private static final String TAG = MyFirstTest.class.getSimpleName();

    @Override
    public void setUp() throws Exception {
        super.setUp();
        device = UiDevice.getInstance(getInstrumentation());
    }

    public void testClick() throws Exception {
        device.wait(Until.hasObject(By.clazz("android.widget.TextView").text("TOMTOP")), 3000);
        UiObject2 appsButton = device.findObject(By.clazz("android.widget.TextView").text("TOMTOP"));
        appsButton.click();
        Thread.sleep(20000);
        clickNextByText("Tablet PC & Cellphone");
        Thread.sleep(20000);
        clickNextByText("Sort");
        clickNextByText("Review");
        clickNextByText("Apply");
        Thread.sleep(20000);
        clickNextByDesc("imageview1");
        Thread.sleep(20000);
        clickNextByText("Buy Now");
        Thread.sleep(10000);
        clickNextByText("Continue");
        Thread.sleep(15000);
        clickNextByText("1");
        device.wait(Until.hasObject(By.clazz("android.widget.EditText")), 3000);
        UiObject2 btn = device.findObject(By.clazz("android.widget.EditText")
                );
        btn.setText("10");
        Thread.sleep(2000);
        clickNextByText("Confirm");
        Thread.sleep(2000);
        clickNextByText("Check Out");
        Thread.sleep(15000);


    }

    private void clickNextByText(String text) throws InterruptedException {
        device.wait(Until.hasObject(By.text(text)), 3000);
        UiObject2 btn = device.findObject(By.text
                (text));
        btn.click();
        Thread.sleep(1000);
    }
    private void clickNextByDesc(String text) throws InterruptedException {
        device.wait(Until.hasObject(By.desc(text)), 3000);
        UiObject2 btn = device.findObject(By.desc
                (text));
        btn.click();
        Thread.sleep(1000);
    }
}
展开阅读全文

没有更多推荐了,返回首页