Espresso测试用真机测试,测试ui等一些信息。
支持的API
Codename | API |
---|---|
Froyo | 8 |
Gingerbread | 10 |
Ice Cream Sandwich | 15 |
Jelly Bean | 16, 17 ,18 |
KitKat | 19 |
Lollipop | 21 |
测试之前的准备
- 关闭窗口动画缩放
- 关闭过度动画缩放
- 关闭动画程序时长缩放
添加依赖
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)));