天天看點

Android單元測試02--Espresso測試支援的API測試之前的準備建立的一個測試基本的操作符web 頁面

Espresso測試用真機測試,測試ui等一些資訊。

支援的API

Codename API
Froyo 8
Gingerbread 10
Ice Cream Sandwich 15
Jelly Bean 16, 17 ,18
KitKat 19
Lollipop 21

測試之前的準備

  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

額外添加依賴

用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;

Android單元測試02--Espresso測試支援的API測試之前的準備建立的一個測試基本的操作符web 頁面
Android單元測試02--Espresso測試支援的API測試之前的準備建立的一個測試基本的操作符web 頁面

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 頁面

@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)));