zoukankan      html  css  js  c++  java
  • 4、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文件中添加依赖。

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

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

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

    使用带ActivityTestRule的Espresso
    下面将接受如何创建Junit 4风格的Espresso 测试,通过使用ActivityTestRule来减少不必要的代码。

    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的Espresso

    代码如下:

    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());
    }

    声明一个ViewMatcher

    可以通过如下方法声明一个view matcher:
    1、 调用ViewMatchers类中的方法。如下:

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

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

    
    在AdapterView中定位View
    
    代码如下:
    

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

    
    模拟动作
    

    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)));
    }
    “`

    本文作者:宋志辉
    个人微博:点击进入

  • 相关阅读:
    nyoj 463-九九乘法表
    nyoj 458-小光棍数 (471)
    nyoj 457-大小写互换
    nyoj 455-黑色帽子
    nyoj 412-Same binary weight (bitset ,to_ulong())
    nyoj 399-整除个数 (整除)
    nyoj 366-D的小L (next_permutation())
    nyoj 324-猴子吃桃问题 (m[i] = (m[i-1] + 1) * 2)
    nyoj 283-对称排序 (sort)
    HBase 在人工智能场景的使用
  • 原文地址:https://www.cnblogs.com/hainange/p/6153450.html
Copyright © 2011-2022 走看看