(8.1.5.7)Android Testing Support Library翻译之 Espresso Web
来源:互联网 发布:java支持数据库的套件 编辑:程序博客网 时间:2024/06/01 19:43
1. Espresso 概览
2. Espresso 设置说明
3. Espresso 基础
4. Espresso 备忘录
5. Espresso 意图
6. Espresso 高级示例
7. Espresso Web
8. AndroidJUnitRunner
9. ATSL 中的 JUnit4 规则
10. UI Automator
11. 可访问性检查
下载 Espresso-Web
Espresso-web 是测试 Android 上 WebView 的切入点。它使用流行的 WebDriver API 原子内省并控制 WebView 的行为。
与 onData 类似,WebView 的交互实际上是几个视图原子的组合。一个原子可以被看作一个 ViewAction,一个在 UI 上执行操作的自包含单元。然而,它们需要适当的精心策划并且十分啰嗦。Web 和 WebInteraction 对此样本进行了包装,提供了 Espresso 风格的 WebView 交互体验。
WebView 经常在 Java/JavaScript 之间跨界工作,由于没有机会将 JavaScript 中的数据引入到竞态机制(Espresso 得到的所有 Java 端的数据都一个独立的副本),WebInteractions 全面支持数据的返回。
常规 WebInteractions
withElement(ElementReference)
将把ElementReference
提交到原子中,示例如下:
onWebView().withElement(findElement(Locator.ID, "teacher"))
withContextualElement(Atom<ElementReference>)
将把 ElementReference 提交到原子钟,示例如下:
onWebView() .withElement(findElement(Locator.ID, "teacher")) .withContextualElement(findElement(Locator.ID, "person_name"))
check(WebAssertion)
将会检查断言的真假性。示例如下:
onWebView() .withElement(findElement(Locator.ID, "teacher")) .withContextualElement(findElement(Locator.ID, "person_name")) .check(webMatches(getText(), containsString("Socrates")));
perform(Atom)
将在当前的上下文中执行提供的原子操作,示例如下:
onWebView() .withElement(findElement(Locator.ID, "teacher")) .perform(webClick());
- 当之前的操作(如点击)改变了界面导航,从而使 ElementReference 和 WindowReference 点失效时,必须使用
reset()
。
WebView 示例
Espresso web 需要启用 JavaScript 来控制 WebView。你可以通过覆写 ActivityTestRule 类中的 afterActivityLaunched 方法强制启用它。
@Rulepublic ActivityTestRule<WebViewActivity> mActivityRule = new ActivityTestRule<WebViewActivity>(WebViewActivity.class, false, false) { @Override protected void afterActivityLaunched() { // Enable JS! onWebView().forceJavascriptEnabled(); }}@Testpublic void typeTextInInput_clickButton_SubmitsForm() { // Lazily launch the Activity with a custom start Intent per test mActivityRule.launchActivity(withWebFormIntent()); // Selects the WebView in your layout. If you have multiple WebViews you can also use a // matcher to select a given WebView, onWebView(withId(R.id.web_view)). onWebView() // Find the input element by ID .withElement(findElement(Locator.ID, "text_input")) // Clear previous input .perform(clearElement()) // Enter text into the input element .perform(DriverAtoms.webKeys(MACCHIATO)) // Find the submit button .withElement(findElement(Locator.ID, "submitBtn")) // Simulate a click via javascript .perform(webClick()) // Find the response element by ID .withElement(findElement(Locator.ID, "response")) // Verify that the response page contains the entered text .check(webMatches(getText(), containsString(MACCHIATO)));}
在 GitHub 上查看 Espresso Web sample。
下载 Espresso-Web
- 确保你已经安装了 Android Support Repository(查看说明)
- 打开应用的
build.gradle
文件。它通常不是顶级build.gradle
,而是app/build.gradle
。
在 dependencies 中添加以下行:
androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.2'
Espresso-Web 只兼容 Espresso 2.2+ 和 testing supprot library 0.3+,所以你也要更新如下行:
androidTestCompile 'com.android.support.test:runner:0.5'androidTestCompile 'com.android.support.test:rules:0.5'androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
- (8.1.5.7)Android Testing Support Library翻译之 Espresso Web
- (8.1.5.1)Android Testing Support Library翻译之Espresso 概览
- (8.1.5.2)Android Testing Support Library翻译之Espresso 设置说明
- (8.1.5.3)Android Testing Support Library翻译之Espresso 基础
- (8.1.5.4)Android Testing Support Library翻译之Espresso 备忘录
- (8.1.5.5)Android Testing Support Library翻译之Espresso 意图
- (8.1.5.6)Android Testing Support Library翻译之Espresso 高级示例
- (8.1.5.8)Android Testing Support Library翻译之 AndroidJUnitRunner
- (8.1.5.9)Android Testing Support Library翻译之 ATSL 中的 JUnit4 规则
- (8.1.5.10)Android Testing Support Library翻译之 UI Automator
- (8.1.5.11)Android Testing Support Library翻译之 可访问性检查
- Android Testing Support Library
- 【翻译】Android Support Library
- 【翻译】Android Support Library(一)
- 【翻译】Android Support Library Features(二)
- 【翻译】Android Support Library Setup(三)
- 【翻译】Android Support Library(一)
- 【翻译】Android Support Library Features(二)
- Lua内置库的实现(一)_string模块
- 在ubuntu 16.04上安装weblogic 12c
- 小白学java----arraylist排序
- Java String和Date的转换
- 数组的基本操作
- (8.1.5.7)Android Testing Support Library翻译之 Espresso Web
- (8.1.5.8)Android Testing Support Library翻译之 AndroidJUnitRunner
- Mysql学习笔记二——基础查询where练习
- 设计模式学习之备忘录模式
- 初识线程
- 如何提交代码到GitHub
- 正则速记理解技巧
- Java并发编程实战
- (8.1.5.9)Android Testing Support Library翻译之 ATSL 中的 JUnit4 规则