四、Android UI测试

为你的APP进行UI测试是为了确保不出现意料以外的结果,提高用户的体验。若是你须要验证你的APP UI的正确性,你须要养成建立UI测试的习惯。
Espresso测试框架是由Android Testing Support Library提供,包含了编写UI测试的API用于模拟用户在指定的APP界面上进行交互。Espresso测试能够运行在Android 2.2(API level 8)以上的设备。当主线程空闲时,Espresso能够侦测到,因此它能够在合适的时候运行你的测试指令,提高测试的可信度。
Espresso基于仪表测试。
配置Espresso
先看第一篇。
在build.gradle文件中添加依赖。css

dependencies { androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1' }

关闭测试机器的动画。
建立一个Espresso 测试类
为了建立一个Espresso测试,按照如下的方式建立一个Java类:
一、 经过调用onView()方法或者onData()在Activity中找到须要测试的UI组件。
二、 经过调用ViewInteraction.perform()或DataInteraction.perform()在UI组件上模拟特定的用户动做。
三、 须要的话重复如上动做。
四、 用ViewAssertions来检测UI。
代码以下:android

nView(withId(R.id.my_view))            
        .perform(click())              
        .check(matches(isDisplayed()));

使用带ActivityTestRule的Espresso
下面将接受如何建立Junit 4风格的Espresso 测试,经过使用ActivityTestRule来减小没必要要的代码。markdown

package com.example.android.testing.espresso.BasicSample;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
...

@RunWith(AndroidJUnit4.class)
@LargeTest
public class ChangeTextBehaviorTest {

    private String mStringToBetyped;

    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
            MainActivity.class);

    @Before
    public void initValidString() {
        // 声明一个String
        mStringToBetyped = "Espresso";
    }

    @Test
    public void changeText_sameActivity() {
        // 输入文字,点击按钮
        onView(withId(R.id.editTextUserInput))
                .perform(typeText(mStringToBetyped), closeSoftKeyboard());
        onView(withId(R.id.changeTextBt)).perform(click());

        // 检测文字改变
        onView(withId(R.id.textToBeChanged))
                .check(matches(withText(mStringToBetyped)));
    }
}

使用带ActivityInstrumentationTestCase2的Espressoapp

代码以下:框架

import android.support.test.InstrumentationRegistry;

public class MyEspressoTest extends ActivityInstrumentationTestCase2<MyActivity> {

    private MyActivity mActivity;

    public MyEspressoTest() {
        super(MyActivity.class);
    }

    @Before
    public void setUp() throws Exception {
        super.setUp();
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        mActivity = getActivity();
    }
}

访问UI组件
在测试下Espresso与你的app进行交互以前,你首先须要声明UI 组件或者view。
代码以下:测试

public void testChangeText_sameActivity() {
    // 输入文字,点击按钮
    onView(withId(R.id.editTextUserInput))
            .perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());
    onView(withId(R.id.changeTextButton)).perform(click());
}

声明一个ViewMatchergradle

能够经过以下方法声明一个view matcher:
一、 调用ViewMatchers类中的方法。以下:动画

onView(withText("登录"));
onView(withId(R.id.button_signin));

使用id的时候须要注意,Android中资源id并非惟一的,使用不当,Espresso可能会抛出AmbiguousViewMatcherException一场。
二、 使用Hamcrest Matchers类。你可使用allof()方法来组合多个matchers。好比containsString()和instanceof()
onView(allOf(withId(R.id.button_signin), withText(“登录”)));
onView(allOf(withId(R.id.button_signin), not(withText(“登出”))));ui

在AdapterView中定位View

代码以下:

onData(allOf(is(instanceOf(Map.class)),
hasEntry(equalTo(LongListActivity.ROW_TEXT), is(str))));spa

模拟动做

ViewActions.click():单击view
ViewActions.typeText():单机view并输入一个特定的string
ViewActions.scrollTo():滚动操做
ViewActions.pressKey();按键单机
ViewActions.clearText():清除text

验证结果

经过调用ViewInteraction.check()或者DataInteraction.check()方法来检测。
代码以下:

public void testChangeText_sameActivity() {
// 检测text更改
onView(withId(R.id.textToBeChanged))
.check(matches(withText(STRING_TO_BE_TYPED)));
}
“`

本文做者:宋志辉
我的微博:点击进入

相关文章
相关标签/搜索