(4.5.5.5)Espresso的进阶: ViewAssertions
来源:互联网 发布:数据分析研究生专业 编辑:程序博客网 时间:2024/06/06 20:33
- 一ViewAssertion
- 二支持函数
- 1 matchesMatcher
- 2 Layout Assertions
- 3 Position Assertions
- 4 其他
- 三AdapterView包含某数据
- 1 遵循adpater协议的
- 2
一、ViewAssertion
public interface ViewAssertion { /** * Checks the state of the given view (if such a view is present). * * @param view the view, if one was found during the view interaction or null if it was not * (which may be an acceptable option for an assertion) * @param noViewFoundException an exception detailing why the view could not be found or null if * the view was found */ void check(View view, NoMatchingViewException noViewFoundException);}
二、支持函数
2.1 matches(Matcher)
我把这个函数放在首位,就是由于借助 这个函数,我们就可以完全灵活使用 上一章 (4.5.5.4)Espresso的进阶: OnView & onData & Matchers的强大Matcher功能
ViewAssertion matches(final Matcher<? super View> viewMatcher)
2.2 Layout Assertions
android.support.test.espresso.assertion.LayoutAssertions
- noEllipsizedText 其子view中的TextView都不是Ellipsized
/** * Returns a {@link ViewAssertion} that asserts that view hierarchy does not contain * ellipsized or cut off text views. */ public static ViewAssertion noEllipsizedText() { return selectedDescendantsMatch( isAssignableFrom(TextView.class), not(hasEllipsizedText())); }
- noMultilineButtons 其子view中的Button都不是 Multiline
/** * Returns a {@link ViewAssertion} that asserts that view hierarchy does not contain * multiline buttons. */ public static ViewAssertion noMultilineButtons() { return selectedDescendantsMatch( isAssignableFrom(Button.class), not(hasMultilineText())); }
- noOverlaps
/** * Returns a {@link ViewAssertion} that asserts that descendant views matching the selector * do not overlap each other. * <p> * Example: onView(rootView).check(noOverlaps(isAssignableFrom(TextView.class)); */ public static ViewAssertion noOverlaps(final Matcher<View> selector)
2.3 Position Assertions
- isLeftOf(Matcher matcher)
- isRightOf(Matcher matcher)
- isLeftAlignedWith(Matcher matcher)
- isRightAlignedWith(Matcher matcher)
- isAbove(Matcher matcher)
- isBelow(Matcher matcher)
- isBottomAlignedWith(Matcher matcher)
- isTopAlignedWith(Matcher matcher)
2.4 其他
- doesNotExist() 指定View不存在
Returns an assert that ensures the view matcher does not find any matching view in the hierarchy.
- selectedDescendantsMatch
/** * Returns a generic {@link ViewAssertion} that asserts that the descendant views selected by the * selector match the specified matcher. * selector选中的view的子节点是否都满足matcher * Example: onView(rootView).check(selectedDescendantsMatch( * not(isAssignableFrom(TextView.class)), hasContentDescription())); */ public static ViewAssertion selectedDescendantsMatch( final Matcher<View> selector, final Matcher<View> matcher)
三、AdapterView包含某数据
使用matches() + Matcher< data >()
3.1 遵循adpater协议的
/** * AdapterView * 依赖于getItem(), 如果getItem()返回数据不对,则自行重构,可参考LegWorkMatcher * 根据 Matcher<Object> 查找对应 view */private static Matcher<View> withAdaptedData(final Matcher<Object> dataMatcher) { return new TypeSafeMatcher<View>() { @Override public void describeTo(Description description) { description.appendText("with class name: "); dataMatcher.describeTo(description); } @Override public boolean matchesSafely(View view) { if (!(view instanceof AdapterView)) { return false; } @SuppressWarnings("rawtypes") Adapter adapter = ((AdapterView) view).getAdapter(); for (int i = 0; i < adapter.getCount(); i++) { if (dataMatcher.matches(adapter.getItem(i))) { return true; } } return false; } };}
@SuppressWarnings("unchecked")public void testDataItemNotInAdapter(){ onView(withId(R.id.list)) .check(matches(not(withAdaptedData(withItemContent("item: 168"))))); }
3.2
/** * AdapterView * 不依赖于getItem() * 根据 Matcher<Object> 查找对应 view */ public static Matcher<View> withAdaptedData(final Matcher<Object> dataMatcher) { return new TypeSafeMatcher<View>() { @Override public void describeTo(Description description) { description.appendText("with class name: "); dataMatcher.describeTo(description); } @Override public boolean matchesSafely(View view) { if (!(view instanceof AdapterView)) { return false; } @SuppressWarnings("rawtypes") BaseLegWrkListActivity.BaseLegwrkListAdapter adapter = null; if( ((AdapterView) view).getAdapter() instanceof HeaderViewListAdapter){ adapter = (BaseLegWrkListActivity.BaseLegwrkListAdapter) ( (HeaderViewListAdapter) ((AdapterView) view).getAdapter()).getWrappedAdapter(); } else{ adapter = (BaseLegWrkListActivity.BaseLegwrkListAdapter) ((AdapterView) view).getAdapter(); } for (int i = 0; i < adapter.getCount(); i++) { if (dataMatcher.matches(adapter.getLegWorkLineVo(i))) { return true; } } return false; } }; }
onView(isAssignableFrom(ListView.class)).check(matches(LegWorkMatcher.withAdaptedData(LegWorkMatcher.searchMainItemWithName(legworkName))));
0 0
- (4.5.5.5)Espresso的进阶: ViewAssertions
- (4.5.5.3)Espresso的进阶: ViewAction
- (4.5.5.6)Espresso的进阶: IdlingResource
- (4.5.5.4)Espresso的进阶: AdapterViewProtocol
- (4.5.5.4)Espresso的进阶: OnView & onData & Matchers
- (4.5.5.9)Espresso之UiAutomator2与Espresso的结合
- (4.5.5.10)Espresso之Robotium与Espresso的结合
- (4.5.5.2)Espresso的基础
- Espresso自动化测试(十三)- UiAutomator2与Espresso的结合
- (4.5.5.1) Espresso的简介、下载和安装
- 大牛们的浓咖啡(Espresso)简单介绍
- (4.5.5.7)Espresso之Intent测试
- Espresso指南二(Espresso意图)
- Espresso
- Espresso 自动化测试(四)-中文字符的输入
- Espresso 自动化测试(五)- onData() 的使用
- Espresso 自动化测试(八)- SeekBar的测试
- Espresso 不依赖源代码的测试
- 闲话mysql-5.7.18-winx64的secure_file_priv配置项
- chrome on mac
- 水乳交融:论VMware与OpenStack的整合—1
- yii定义常量
- 学习记录
- (4.5.5.5)Espresso的进阶: ViewAssertions
- 开张
- ThinkPHP分页总结
- 【最小生成树】POJ 2349 Arctic Network
- 如何绕过PowerShell Execution Policy限制?
- (4.5.5.6)Espresso的进阶: IdlingResource
- bzoj2152: 聪聪可可
- python运行异常
- 基于 React Native 的开源项目