Android单元测试02--Espresso测试

Espresso测试用真机测试,测试ui等一些信息。

支持的API

CodenameAPI
Froyo8
Gingerbread10
Ice Cream Sandwich15
Jelly Bean16, 17 ,18
KitKat19
Lollipop21

测试之前的准备

  1. 关闭窗口动画缩放
  2. 关闭过度动画缩放
  3. 关闭动画程序时长缩放

添加依赖

androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test:runner:0.5'

defaultCongfig

android.defaultCongfig{
    testInstrumentationRunner      "android.support.test.runner.AndroidJUnitRunner"
}

创建的一个测试

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

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

    @Test
    public void testHelloWordOnScreen() {
        onView(withText("Hello world!")).check(matches(isDisplayed()));
        onView(withId(R.id.hello_world)).check(matches(isDisplayed()));
    }
}

运行:
1. 点击右键 runTest即可
2. gradle connectedAndroidTest

基本的操作符

4个重要的概念

  • Espresso:获取想要操作的view,其中onView()/onData()
  • ViewMatchers:里面有大量的实现Matcher

onView(withId(R.id.hello_world))      // withId(R.id.hello_world) :ViewMatcher
  .perform(click())               // click() :ViewAction
  .check(matches(isDisplayed()));  //  matches(isDisplayed()) : ViewAssertion

获取到操作的view

onView();

//单独id
onView(withId(R.id.view))

//相同id
onView(allOf(withId(R.id.view),withText("text")));

onData()

//adapterView
onData(allOf(is(instanceOf(String.class)), is("value")));
onData(hasEntry(equalTo(KEY),is(value))))

操作view

preform()

//ViewAction
click();
doubleClick();
typeText();
...

验证操作

check();

// ViewAssertion
// ViewAssertion mather.mathces()
doesNotExist
matches(Matcher<? super View> viewMatcher)

intended/Intenting

额外添加依赖

androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'

用IntentsTestRule 代替ActivityTestRule
intended:vertify 操作,

    onView(R.id.next_activity).preform(click());

    intended(allOf(
    hasAction(equalTo(Intent.ACTION_VIEW)),
    hasCategories(hasItem(equalTo(Intent.CATEGORY_BROWSABLE))),
    hasData(hasHost(equalTo("www.google.com"))),
    hasExtras(allOf(
        hasEntry(equalTo("key1"), equalTo("value1")),
        hasEntry(equalTo("key2"), equalTo("value2")))),
        toPackage("com.android.browser")));

intending:when的操作,当什么时候,一般startActivityResult;

intending().respondWith(ActivityResult);

这里写图片描述
这里写图片描述

hasSibling

//文本为7 并且在文本为item:0的旁边
onView(allOf(withText("7"), hasSibling(withText("item: 0"))))
  .perform(click());

customer matcher

//adapter ondata()
onData(allOf(is(instanceOf(Map.class)),hasEntry(equalTo(key),is(value))));

//customeAdapterMatcher
public static Matcher<Object,Map> withItemCount(String value){
    checkNotNull(value);
    return new BoundedMatcher<Object,Map>(Map.class){
        @Override
        public boolean matchesSafely(Map map){
            return hasEntry(equalTo(key),value).matchs(map);
        }

        @Override
         public void describeTo(Description description) {
            description.appendText("with item content: ");
                itemTextMatcher.describeTo(description);
        }
    }
}

//VisibleMatcher
public static Matcher<View,View> visible(){
    return new BoundedMatcher<View,View>(View.class){
        public boolean matcherSafely(View view){
            return view.isShow();
        }

        public void describleTo(Description description){
            description.appendText("visible");
        }
    }
}

onChildView

onData(ViewMatcher)
.onChildView(ViewMatcher)
.preform()

openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());

打开菜单页

doesNotExist

现在不存在

web 页面

androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.2'

@Rule

 @Rule
    public ActivityTestRule<WebViewActivity> mActivityRule = new ActivityTestRule<WebViewActivity>(
            WebViewActivity.class, false, false) {
        @Override
        protected void afterActivityLaunched() {
            // Technically we do not need to do this - WebViewActivity has javascript turned on.
            // Other WebViews in your app may have javascript turned off, however since the only way
            // to automate WebViews is through javascript, it must be enabled.
            onWebView().forceJavascriptEnabled();
        }
    };
            onWebView()
                //找元素
                .withElement(findElement(Locator.ID, "text_input"))
                // 清理元素的内容
                .perform(clearElement())
                // 输入元素内容
                .perform(DriverAtoms.webKeys(MACCHIATO))
                // 找元素
                .withElement(findElement(Locator.ID, "submitBtn"))
                // 点击
                .perform(webClick())
                // 找元素
                .withElement(findElement(Locator.ID, "response"))
                // 检查
                .check(webMatches(getText(), containsString(MACCHIATO)));
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值