{"id":777,"date":"2023-02-28T10:52:18","date_gmt":"2023-02-28T02:52:18","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=777"},"modified":"2023-04-29T16:56:41","modified_gmt":"2023-04-29T08:56:41","slug":"android-test-espresso-automated-test","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/28\/android-test-espresso-automated-test\/","title":{"rendered":"Android\u6d4b\u8bd5\uff087\uff09\uff1aEspresso\u81ea\u52a8\u5316\u6d4b\u8bd5"},"content":{"rendered":"<p>\u539f\u6587\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/developer.android.com\/training\/testing\/ui-testing\/espresso-testing.html\">https:\/\/developer.android.com\/training\/testing\/ui-testing\/espresso-testing.html<\/a><\/p>\n<p>\u5728\u5355\u4e2a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6d4b\u8bd5\u7528\u6237\u4ea4\u4e92\u6709\u52a9\u4e8e\u786e\u4fdd\u7528\u6237\u5728\u4e0e\u5e94\u7528\u7a0b\u5e8f\u8fdb\u884c\u4ea4\u4e92\u65f6\u4e0d\u4f1a\u9047\u5230\u610f\u5916\u7684\u7ed3\u679c\uff0c\u6216\u9047\u5230\u7cdf\u7cd5\u7684\u4f53\u9a8c\u3002\u5982\u679c\u9700\u8981\u9a8c\u8bc1\u5e94\u7528\u7684UI\u529f\u80fd\u662f\u5426\u6b63\u5e38\uff0c\u5219\u5e94\u8be5\u517b\u6210\u521b\u5efa\u7528\u6237\u754c\u9762\uff08UI\uff09\u6d4b\u8bd5\u7684\u4e60\u60ef\u3002<\/p>\n<p><!-- more --><\/p>\n<p>Espresso \u6d4b\u8bd5\u6846\u67b6\uff0c\u7531Android\u6d4b\u8bd5\u652f\u6301\u5e93\u63d0\u4f9b\uff0c\u7528\u4e8e\u7f16\u5199UI\u6d4b\u8bd5\u7684API\u6765\u6a21\u62df\u5355\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5185\u7684\u7528\u6237\u4ea4\u4e92\u3002Espresso\u6d4b\u8bd5\u53ef\u4ee5\u5728\u8fd0\u884cAndroid 2.3.3\uff08API\u7b49\u7ea710\uff09\u53ca\u66f4\u9ad8\u7248\u672c\u7684\u8bbe\u5907\u4e0a\u8fd0\u884c\u3002 \u4f7f\u7528Espresso\u7684\u4e00\u4e2a\u5173\u952e\u597d\u5904\u662f\uff0c\u5b83\u63d0\u4f9b\u4e86\u6d4b\u8bd5\u64cd\u4f5c\u4e0e\u6b63\u5728\u6d4b\u8bd5\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u7528\u6237\u754c\u9762\u7684\u81ea\u52a8\u540c\u6b65\u3002 Espresso\u68c0\u6d4b\u4e3b\u7ebf\u7a0b\u662f\u5426\u5904\u4e8e\u7a7a\u95f2\u72b6\u6001\uff0c\u4ee5\u4fbf\u80fd\u591f\u5728\u9002\u5f53\u7684\u65f6\u5019\u8fd0\u884c\u6d4b\u8bd5\u547d\u4ee4\uff0c\u4ece\u800c\u63d0\u9ad8\u6d4b\u8bd5\u7684\u53ef\u9760\u6027\u3002\u6b64\u529f\u80fd\u8fd8\u53ef\u4ee5\u51cf\u8f7b\u5728\u6d4b\u8bd5\u4ee3\u7801\u4e2d\u6dfb\u52a0\u4efb\u4f55\u8ba1\u65f6\u53d8\u901a\u65b9\u6cd5\u5982<code>Thread.sleep()<\/code>\u7684\u9ebb\u70e6\u3002<\/p>\n<p>Espresso\u6d4b\u8bd5\u6846\u67b6\u57fa\u4e8eInstrumentation\u7684API\uff0c\u5e76\u901a\u8fc7AndroidJUnitRunner\u8fd0\u884c\u6d4b\u8bd5\u3002<\/p>\n<h2>\u914d\u7f6eEspresso<\/h2>\n<p>\u5728\u4f7f\u7528Espresso\u6784\u5efaUI\u6d4b\u8bd5\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u914d\u7f6e\u6d4b\u8bd5\u6e90\u4ee3\u7801\u4f4d\u7f6e\u548c\u9879\u76ee\u4f9d\u8d56\u5173\u7cfb\uff0c\u5982\u201c<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/www.testclass.net\/android\/fundamentals\/\" title=\"Getting Started with Testing\">Getting Started with Testing<\/a>\u201d\u4e2d\u6240\u8ff0\u3002<\/p>\n<p>\u5728app\u6a21\u5757\u7684build.gradle\u4e2d\u6dfb\u52a0\u4f9d\u8d56\uff1a<\/p>\n<pre><code>dependencies {\n    androidTestImplementation &#039;com.android.support.test.espresso:espresso-core:3.0.1&#039;\n}<\/code><\/pre>\n<p>\u5173\u95ed\u6d4b\u8bd5\u8bbe\u5907\u4e0a\u7684\u52a8\u753b &#8211; \u5728\u6d4b\u8bd5\u8bbe\u5907\u4e2d\u6253\u5f00\u7cfb\u7edf\u52a8\u753b\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u5916\u7684\u7ed3\u679c\uff0c\u6216\u8005\u53ef\u80fd\u5bfc\u81f4\u6d4b\u8bd5\u5931\u8d25\u3002\u901a\u8fc7\u6253\u5f00\u201c\u5f00\u53d1\u4eba\u5458\u201d\u9009\u9879\u5173\u95ed\u201c\u8bbe\u7f6e\u201d\u4e2d\u7684\u52a8\u753b\uff0c\u5e76\u5173\u95ed\u4ee5\u4e0b\u6240\u6709\u9009\u9879\uff1a<\/p>\n<ul>\n<li>\u7a97\u53e3\u52a8\u753b\u6bd4\u4f8b<\/li>\n<li>\u8fc7\u6e21\u52a8\u753b\u6bd4\u4f8b<\/li>\n<li>\u52a8\u753b\u5e08\u6301\u7eed\u65f6\u95f4\u6bd4\u4f8b<\/li>\n<\/ul>\n<p>\u5982\u679c\u5e0c\u671b\u5c06\u9879\u76ee\u8bbe\u7f6e\u4e3a\u4f7f\u7528\u6838\u5fc3API\u63d0\u4f9b\u7684Espresso\u529f\u80fd\u4ee5\u5916\u7684\u529f\u80fd\uff0c\u8bf7\u53c2\u9605<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/developer.android.com\/training\/testing\/espresso\/index.html\">\u6b64\u8d44\u6e90<\/a><\/p>\n<h2>\u521b\u5efa\u4e00\u4e2aEspresso \u6d4b\u8bd5<\/h2>\n<p>\u8981\u521b\u5efaEspresso\u6d4b\u8bd5\uff0c\u8bf7\u521b\u5efa\u9075\u5faa\u6b64\u7f16\u7a0b\u6a21\u578b\u7684Java\u7c7b\uff1a<\/p>\n<ul>\n<li>\n<ol>\n<li>\u627e\u5230\u60f3\u8981\u5728\u6d3b\u52a8\u4e2d\u6d4b\u8bd5\u7684UI\u7ec4\u4ef6(\u4f8b\u5982\uff0c\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u767b\u5f55\u6309\u94ae)\uff0c\u8c03\u7528onView()\u65b9\u6cd5\uff0c\u6216\u8005\u8c03\u7528AdapterView\u63a7\u4ef6\u7684onData()\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"2\">\n<li>\u901a\u8fc7\u8c03\u7528<code>ViewInteraction.perform()<\/code>\u6216<code>ataInteraction.perform()<\/code>\u65b9\u6cd5\u5e76\u4f20\u5165\u7528\u6237\u64cd\u4f5c\uff08\u4f8b\u5982\uff0c\u5355\u51fb\u767b\u5f55\u6309\u94ae\uff09\uff0c\u6a21\u62df\u7279\u5b9a\u7684\u7528\u6237\u4ea4\u4e92\u4ee5\u5728\u8be5UI\u7ec4\u4ef6\u4e0a\u6267\u884c\u3002 \u8981\u5728\u540c\u4e00UI\u7ec4\u4ef6\u4e0a\u5bf9\u591a\u4e2a\u64cd\u4f5c\u8fdb\u884c\u6392\u5e8f\uff0c\u8bf7\u4f7f\u7528\u65b9\u6cd5\u53c2\u6570\u4e2d\u7684\u9017\u53f7\u5206\u9694\u5217\u8868\u94fe\u63a5\u5b83\u4eec\u3002<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"3\">\n<li>\u6839\u636e\u9700\u8981\u91cd\u590d\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6a21\u62df\u76ee\u6807\u5e94\u7528\u4e2d\u591a\u4e2a\u6d3b\u52a8\u7684\u7528\u6237\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"4\">\n<li>\u5728\u6267\u884c\u8fd9\u4e9b\u7528\u6237\u4ea4\u4e92\u4e4b\u540e\uff0c\u4f7f\u7528<code>ViewAssertions<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5UI\u662f\u5426\u53cd\u6620\u4e86\u671f\u671b\u7684\u72b6\u6001\u6216\u884c\u4e3a\u3002<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u5404\u8282\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u663e\u793a\u4e86\u6d4b\u8bd5\u7c7b\u5982\u4f55\u8c03\u7528\u7684\u57fa\u672c\u5de5\u4f5c\u6d41\u7a0b\uff1a<\/p>\n<pre><code>onView(withId(R.id.my_view))            \/\/ withId(R.id.my_view) is a ViewMatcher\n        .perform(click())               \/\/ click() is a ViewAction\n        .check(matches(isDisplayed())); \/\/ matches(isDisplayed()) is a ViewAssertion<\/code><\/pre>\n<h2>\u4f7f\u7528Espresso\u548cActivityTestRule<\/h2>\n<p>\u4ee5\u4e0b\u90e8\u5206\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528JUnit 4\u6837\u5f0f\u521b\u5efa\u65b0\u7684Espresso\u6d4b\u8bd5\uff0c\u5e76\u4f7f\u7528ActivityTestRule\u6765\u51cf\u5c11\u9700\u8981\u7f16\u5199\u7684\u6837\u677f\u4ee3\u7801\u7684\u6570\u91cf\u3002 \u901a\u8fc7\u4f7f\u7528ActivityTestRule\uff0c\u6d4b\u8bd5\u6846\u67b6\u5728\u6bcf\u4e2a\u4f7f\u7528<a target=\"_blank\" rel=\"noopener\" href=\"mailto:code&gt;@Test&lt;\/code\">code>@Test<\/code<\/a>\u6ce8\u91ca\u7684\u6d4b\u8bd5\u65b9\u6cd5\u4e4b\u524d\u4ee5\u53ca\u5728\u4f7f\u7528<a target=\"_blank\" rel=\"noopener\" href=\"mailto:code&gt;@Before&lt;\/code\">code>@Before<\/code<\/a>\u6ce8\u91ca\u7684\u65b9\u6cd5\u4e4b\u524d\u542f\u52a8\u88ab\u6d4b\u6d3b\u52a8\u3002\u6d4b\u8bd5\u5b8c\u6210\u540e\uff0c\u6846\u67b6\u5904\u7406\u5173\u95ed\u6d3b\u52a8\uff0c\u6240\u6709\u4f7f\u7528<a target=\"_blank\" rel=\"noopener\" href=\"mailto:code&gt;@After&lt;\/code\">code>@After<\/code<\/a>\u6ce8\u91ca\u7684\u65b9\u6cd5\u90fd\u4f1a\u8fd0\u884c\u3002<\/p>\n<pre><code class=\"language-java\">import android.support.test.filters.LargeTest;\nimport android.support.test.rule.ActivityTestRule;\nimport android.support.test.runner.AndroidJUnit4;\n\nimport org.junit.Before;\nimport org.junit.Rule;\nimport org.junit.Test;\nimport org.junit.runner.RunWith;\n\nimport static android.support.test.espresso.Espresso.onView;\nimport static android.support.test.espresso.action.ViewActions.click;\nimport static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;\nimport static android.support.test.espresso.action.ViewActions.typeText;\nimport static android.support.test.espresso.assertion.ViewAssertions.matches;\nimport static android.support.test.espresso.matcher.ViewMatchers.withId;\nimport static android.support.test.espresso.matcher.ViewMatchers.withText;\n\n@RunWith(AndroidJUnit4.class)\n@LargeTest\npublic class ChangeTextBehaviorTest {\n    private String mStringToBetyped;\n\n    @Rule\n    public ActivityTestRule&lt;MainActivity&gt; mActivityRule = new ActivityTestRule&lt;&gt;(\n            MainActivity.class);\n\n    @Before\n    public void initValidString() {\n        \/\/ Specify a valid string.\n        mStringToBetyped = &quot;Espresso&quot;;\n    }\n\n    @Test\n    public void changeText_sameActivity() {\n        \/\/ Type text and then press the button.\n        onView(withId(R.id.et_user_input))\n                .perform(typeText(mStringToBetyped), closeSoftKeyboard());\n        onView(withId(R.id.btn_change_text)).perform(click());\n\n        \/\/ Check that the text was changed.\n        onView(withId(R.id.et_text_changed))\n                .check(matches(withText(mStringToBetyped)));\n    }\n}<\/code><\/pre>\n<h2>\u8bbf\u95eeUI\u7ec4\u4ef6<\/h2>\n<p>\u5728Espresso\u53ef\u4ee5\u4e0e\u88ab\u6d4b\u8bd5\u7684\u5e94\u7528\u7a0b\u5e8f\u8fdb\u884c\u4ea4\u4e92\u4e4b\u524d\uff0c\u5fc5\u987b\u5148\u6307\u5b9aUI\u7ec4\u4ef6\u6216\u89c6\u56fe\u3002Espresso\u652f\u6301\u4f7f\u7528[Hamcrest \u5339\u914d\u5668](<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/hamcrest.org\/\">http:\/\/hamcrest.org\/<\/a> Hamcrest \u5339\u914d\u5668)\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6307\u5b9a\u89c6\u56fe\u548c\u9002\u914d\u5668\u3002<\/p>\n<p>\u8981\u67e5\u627e\u89c6\u56fe\uff0c\u8bf7\u8c03\u7528<code>onView()<\/code>\u65b9\u6cd5\u5e76\u4f20\u9012\u4e00\u4e2a\u89c6\u56fe\u5339\u914d\u5668\uff0c\u8be5\u89c6\u56fe\u5339\u914d\u5668\u6307\u5b9a\u8981\u5b9a\u4f4d\u7684\u89c6\u56fe\u3002\u8fd9\u5728\u6307\u5b9a\u89c6\u56fe\u5339\u914d\u5668\u4e2d\u6709\u66f4\u8be6\u7ec6\u7684\u63cf\u8ff0\u3002 <code>onView()<\/code>\u65b9\u6cd5\u8fd4\u56deViewInteraction\u5bf9\u8c61\uff0c\u5141\u8bb8\u6d4b\u8bd5\u4e0e\u89c6\u56fe\u8fdb\u884c\u4ea4\u4e92\u3002 \u4f46\u662f\uff0c\u5982\u679c\u8981\u5728RecyclerView\u5e03\u5c40\u4e2d\u67e5\u627e\u89c6\u56fe\uff0c\u5219\u8c03\u7528<code>onView()<\/code>\u65b9\u6cd5\u53ef\u80fd\u4e0d\u8d77\u4f5c\u7528\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8bf7\u6309\u7167\u5728AdapterView\u4e2d\u67e5\u627e\u89c6\u56fe\u4e2d\u7684\u8bf4\u660e\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<blockquote>\n<p>\u6ce8\u610f\uff1aonView()\u65b9\u6cd5\u4e0d\u68c0\u67e5\u4f60\u6307\u5b9a\u7684\u89c6\u56fe\u662f\u5426\u6709\u6548\u3002\u76f8\u53cd\uff0cEspresso\u53ea\u641c\u7d22\u5f53\u524d\u7684\u89c6\u56fe\u5c42\u6b21\u7ed3\u6784\uff0c\u4f7f\u7528matcher\u63d0\u4f9b\u7684\u89c6\u56fe\u3002\u5982\u679c\u6ca1\u6709\u627e\u5230\u5339\u914d\uff0c\u8be5\u65b9\u6cd5\u5c06\u629b\u51fa\u4e00\u4e2aNoMatchingViewException\u3002<\/p>\n<\/blockquote>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u5c55\u793a\u4e86\u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u6d4b\u8bd5\u6765\u8bbf\u95eeEditText\u5b57\u6bb5\uff0c\u8f93\u5165\u4e00\u4e2a\u6587\u672c\u5b57\u7b26\u4e32\uff0c\u5173\u95ed\u865a\u62df\u952e\u76d8\uff0c\u7136\u540e\u6267\u884c\u6309\u94ae\u5355\u51fb\u3002<\/p>\n<pre><code class=\"language-java\">public void testChangeText_sameActivity() {\n    \/\/ Type text and then press the button.\n    onView(withId(R.id.editTextUserInput))\n            .perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());\n    onView(withId(R.id.changeTextButton)).perform(click());\n\n    \/\/ Check that the text was changed.\n    ...\n}<\/code><\/pre>\n<h3>\u6307\u5b9a\u4e00\u4e2a\u89c6\u56fe\u5339\u914d\u5668<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u6307\u5b9a\u89c6\u56fe\u5339\u914d\u5668\uff1a<\/p>\n<p>\u5728ViewMatchers\u7c7b\u4e2d\u8c03\u7528\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u8981\u67e5\u627e\u663e\u793a\u7684\u6587\u672c\u5b57\u7b26\u4e32\u6765\u67e5\u627e\u89c6\u56fe\uff0c\u53ef\u4ee5\u8c03\u7528\u5982\u4e0b\u6240\u793a\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">onView(withText(&quot;Sign-in&quot;));<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u8c03\u7528withId()\u5e76\u63d0\u4f9b\u89c6\u56fe\u7684\u8d44\u6e90ID(R.id)\uff0c\u5982\u4e0b\u4f8b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-java\">onView(withId(R.id.button_signin));<\/code><\/pre>\n<p>Android\u8d44\u6e90ID\u4e0d\u4fdd\u8bc1\u662f\u552f\u4e00\u7684\u3002\u5982\u679c\u6d4b\u8bd5\u5c1d\u8bd5\u5339\u914d\u591a\u4e2a\u89c6\u56fe\u4f7f\u7528\u7684\u8d44\u6e90ID\uff0c\u5219Espresso\u4f1a\u5f15\u53d1AmbiguousViewMatcherException\u3002<\/p>\n<p>\u4f7f\u7528[Hamcrest Matchers\u7c7b](<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/hamcrest.org\/JavaHamcrest\/javadoc\/1.3\/org\/hamcrest\/Matchers.html\">http:\/\/hamcrest.org\/JavaHamcrest\/javadoc\/1.3\/org\/hamcrest\/Matchers.html<\/a> Hamcrest Matchers\u7c7b)\u3002\u53ef\u4ee5\u4f7f\u7528<code>AllOf()<\/code>\u65b9\u6cd5\u6765\u7ec4\u5408\u591a\u4e2a\u5339\u914d\u5668\uff0c\u4f8b\u5982<code>containsString()<\/code>\u548c<code>instanceOf()<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u66f4\u7a84\u5730\u8fc7\u6ee4\u5339\u914d\u7ed3\u679c\uff0c\u5982\u4ee5\u4e0b\u793a\u4f8b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-java\">onView(allOf(withId(R.id.button_signin), withText(&quot;Sign-in&quot;)));<\/code><\/pre>\n<p>\u4f46\u662f\uff0c\u4e0d\u80fd\u4f7f\u7528\u5173\u952e\u5b57\u6765\u7b5b\u9009\u4e0d\u5339\u914d\u5339\u914d\u5668\u7684\u89c6\u56fe\uff0c\u5982\u4ee5\u4e0b\u793a\u4f8b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-java\">onView(allOf(withId(R.id.button_signin), not(withText(&quot;Sign-out&quot;))));<\/code><\/pre>\n<p>\u8981\u5728\u6d4b\u8bd5\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u8bf7\u5bfc\u5165<code>org.hamcrest.Matchers<\/code>\u5305\u3002\u8981\u4e86\u89e3\u6709\u5173Hamcrest\u5339\u914d\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605Hamcrest\u7f51\u7ad9<\/p>\n<p>\u8981\u6539\u5584Espresso\u6d4b\u8bd5\u7684\u6027\u80fd\uff0c\u8bf7\u6307\u5b9a\u67e5\u627e\u76ee\u6807\u89c6\u56fe\u6240\u9700\u7684\u6700\u4f4e\u5339\u914d\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u67d0\u4e2a\u89c6\u56fe\u53ef\u4ee5\u901a\u8fc7\u5176\u63cf\u8ff0\u6027\u6587\u672c\u552f\u4e00\u6807\u8bc6\uff0c\u5219\u4e0d\u9700\u8981\u6307\u5b9a\u6a21\u7cca\u6216\u5197\u4f59\u8fc7\u6ee4\u3002<\/p>\n<h3>\u5728AdapterView\u4e2d\u67e5\u627e\u89c6\u56fe<\/h3>\n<p>\u5728AdapterView\u5c0f\u90e8\u4ef6\u4e2d\uff0c\u89c6\u56fe\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u586b\u5145\u5b50\u89c6\u56fe\u3002\u5982\u679c\u8981\u6d4b\u8bd5\u7684\u76ee\u6807\u89c6\u56fe\u4f4d\u4e8eAdapterView\u4e2d\uff08\u5982ListView\uff0cGridView\u6216Spinner\uff09\uff0c\u5219<code>onView()<\/code>\u65b9\u6cd5\u53ef\u80fd\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\uff0c\u56e0\u4e3a\u53ea\u6709\u89c6\u56fe\u7684\u5b50\u96c6\u53ef\u80fd\u4f1a\u52a0\u8f7d\u5230\u5f53\u524d\u89c6\u56fe\u5c42\u6b21\u7ed3\u6784\u4e2d\u3002<\/p>\n<p>\u76f8\u53cd\uff0c\u8c03\u7528<code>onData()<\/code>\u65b9\u6cd5\u83b7\u53d6DataInteraction\u5bf9\u8c61\u6765\u8bbf\u95ee\u76ee\u6807\u89c6\u56fe\u5143\u7d20\u3002Espresso\u5c06\u76ee\u6807\u89c6\u56fe\u5143\u7d20\u52a0\u8f7d\u5230\u5f53\u524d\u89c6\u56fe\u5c42\u6b21\u7ed3\u6784\u4e2d\u3002Espresso\u8fd8\u8d1f\u8d23\u6eda\u52a8\u76ee\u6807\u5143\u7d20\uff0c\u5e76\u5c06\u5143\u7d20\u653e\u5728\u7126\u70b9\u4e0a\u3002<\/p>\n<blockquote>\n<p>\u6ce8\u610f\uff1a<code>onData()<\/code>\u65b9\u6cd5\u4e0d\u4f1a\u68c0\u67e5\u4f60\u6307\u5b9a\u7684\u9879\u76ee\u662f\u5426\u4e0e\u89c6\u56fe\u5bf9\u5e94\u3002Espresso\u53ea\u641c\u7d22\u5f53\u524d\u7684\u89c6\u56fe\u5c42\u6b21\u7ed3\u6784\u3002\u5982\u679c\u627e\u4e0d\u5230\u5339\u914d\uff0c\u5219\u8be5\u65b9\u6cd5\u5c06\u5f15\u53d1NoMatchingViewException\u3002<\/p>\n<\/blockquote>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u7247\u6bb5\u663e\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>onData()<\/code>\u65b9\u6cd5\u548cHamcrest\u5339\u914d\u4e00\u8d77\u641c\u7d22\u5305\u542b\u7ed9\u5b9a\u5b57\u7b26\u4e32\u7684\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u884c\u3002\u5728\u6b64\u793a\u4f8b\u4e2d\uff0cLongListActivity\u7c7b\u5305\u542b\u901a\u8fc7SimpleAdapter\u516c\u5f00\u7684\u5b57\u7b26\u4e32\u5217\u8868\u3002<\/p>\n<pre><code>onData(allOf(is(instanceOf(Map.class)),\n        hasEntry(equalTo(LongListActivity.ROW_TEXT), is(&quot;test input&quot;)));<\/code><\/pre>\n<h2>\u6267\u884c\u64cd\u4f5c<\/h2>\n<p>\u8c03\u7528<code>ViewInteraction.perform()<\/code>\u6216<code>DataInteraction.perform()<\/code>\u65b9\u6cd5\u6765\u6a21\u62dfUI\u7ec4\u4ef6\u4e0a\u7684\u7528\u6237\u4ea4\u4e92\u3002\u5fc5\u987b\u4f20\u5165\u4e00\u4e2a\u6216\u591a\u4e2aViewAction\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u3002Espresso\u6839\u636e\u7ed9\u5b9a\u7684\u987a\u5e8f\u4f9d\u6b21\u89e6\u53d1\u6bcf\u4e2a\u52a8\u4f5c\uff0c\u5e76\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u6267\u884c\u5b83\u4eec\u3002<\/p>\n<p>ViewActions\u7c7b\u63d0\u4f9b\u4e86\u7528\u4e8e\u6307\u5b9a\u5e38\u7528\u64cd\u4f5c\u7684\u5e2e\u52a9\u7a0b\u5e8f\u65b9\u6cd5\u5217\u8868\u3002\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u4f5c\u4e3a\u65b9\u4fbf\u7684\u5feb\u6377\u65b9\u5f0f\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u548c\u914d\u7f6e\u5355\u72ec\u7684ViewAction\u5bf9\u8c61\u3002\u53ef\u4ee5\u6307\u5b9a\u5982\u4e0b\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li>ViewActions.click()\uff1a\u70b9\u51fb\u89c6\u56fe<\/li>\n<li>ViewActions.typeText()\uff1a\u70b9\u51fb\u4e00\u4e2a\u89c6\u56fe\u5e76\u8f93\u5165\u4e00\u4e2a\u6307\u5b9a\u7684\u5b57\u7b26\u4e32<\/li>\n<li>ViewActions.scrollTo()\uff1a\u6eda\u52a8\u5230\u89c6\u56fe\u3002\u76ee\u6807\u89c6\u56fe\u5fc5\u987b\u4eceScrollView\u7ee7\u627f\uff0c\u5176android:visibility\u5c5e\u6027\u7684\u503c\u5fc5\u987b\u662f\u53ef\u89c1\u7684\u3002\u5bf9\u4e8e\u6269\u5c55AdapterView\u7684\u89c6\u56fe\uff08\u4f8b\u5982ListView\uff09\uff0conData()\u65b9\u6cd5\u8d1f\u8d23\u6eda\u52a8\u3002<\/li>\n<li>ViewActions.pressKey()\uff1a\u4f7f\u7528\u6307\u5b9a\u7684\u952e\u7801\u8fdb\u884c\u6309\u952e\u64cd\u4f5c<\/li>\n<li>ViewActions.clearText()\uff1a\u6e05\u9664\u76ee\u6807\u89c6\u56fe\u4e2d\u7684\u6587\u672c<\/li>\n<\/ul>\n<p>\u5982\u679c\u76ee\u6807\u89c6\u56fe\u4f4d\u4e8eScrollView\u7684\u5185\u90e8\uff0c\u5219\u5148\u6267\u884c<code>ViewActions.scrollTo()<\/code>\u64cd\u4f5c\uff0c\u7136\u540e\u518d\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\u3002\u5982\u679c\u5df2\u7ecf\u663e\u793a\u89c6\u56fe\uff0c\u5219<code>ViewActions.scrollTo()<\/code>\u64cd\u4f5c\u5c06\u4e0d\u8d77\u4f5c\u7528\u3002<\/p>\n<h2>\u7528Espresso Intents\u9694\u79bb\u6d4b\u8bd5<\/h2>\n<p>Espresso Intents\u53ef\u4ee5\u9a8c\u8bc1\u5e94\u7528\u7a0b\u5e8f\u53d1\u9001\u7684\u610f\u56fe\u3002\u901a\u8fc7Espresso Intents\uff0c\u53ef\u4ee5\u62e6\u622a\u4f20\u51fa\u7684\u610f\u56fe\uff0c\u5bf9\u7ed3\u679c\u8fdb\u884c\u5b58\u6839\u5e76\u5c06\u5176\u53d1\u9001\u56de\u88ab\u6d4b\u7ec4\u4ef6\u6765\u9694\u79bb\u6d4b\u8bd5\u5e94\u7528\u7a0b\u5e8f\u7684\u6d3b\u52a8\u6216\u670d\u52a1\u3002<\/p>\n<pre><code>dependencies {\n    androidTestImplementation &#039;com.android.support.test.espresso:espresso-intents:3.0.1&#039;\n}<\/code><\/pre>\n<p>\u4e3a\u4e86\u6d4b\u8bd5\u4e00\u4e2aIntent\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2aIntentsTestRule\u7c7b\u7684\u5b9e\u4f8b\uff0c\u5b83\u4e0eActivityTestRule\u7c7b\u975e\u5e38\u76f8\u4f3c\u3002IntentsTestRule\u7c7b\u5728\u6bcf\u6b21\u6d4b\u8bd5\u4e4b\u524d\u521d\u59cb\u5316Espresso Intents\uff0c\u7ec8\u6b62\u4e3b\u6d3b\u52a8\uff0c\u5e76\u5728\u6bcf\u6b21\u6d4b\u8bd5\u540e\u91ca\u653eEspresso Intents\u3002<\/p>\n<pre><code class=\"language-java\">import android.support.test.espresso.intent.Intents;\nimport android.support.test.espresso.intent.rule.IntentsTestRule;\nimport android.support.test.filters.LargeTest;\nimport android.support.test.runner.AndroidJUnit4;\n\nimport org.junit.Rule;\nimport org.junit.Test;\nimport org.junit.runner.RunWith;\n\nimport static android.support.test.espresso.Espresso.onView;\nimport static android.support.test.espresso.action.ViewActions.click;\nimport static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;\nimport static android.support.test.espresso.action.ViewActions.typeText;\nimport static android.support.test.espresso.intent.matcher.ComponentNameMatchers.hasShortClassName;\nimport static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent;\nimport static android.support.test.espresso.intent.matcher.IntentMatchers.hasExtra;\nimport static android.support.test.espresso.intent.matcher.IntentMatchers.toPackage;\nimport static android.support.test.espresso.matcher.ViewMatchers.withId;\nimport static org.hamcrest.core.AllOf.allOf;\n\n@LargeTest\n@RunWith(AndroidJUnit4.class)\npublic class SimpleIntentTest {\n    private static final String MESSAGE = &quot;This is a test&quot;;\n    private static final String PACKAGE_NAME = &quot;me.yezhou.AutoTest&quot;;\n\n    \/* Instantiate an IntentsTestRule object. *\/\n    @Rule\n    public IntentsTestRule&lt;MainActivity&gt; mIntentsRule =\n            new IntentsTestRule&lt;&gt;(MainActivity.class);\n\n    @Test\n    public void verifyMessageSentToMessageActivity() {\n        \/\/ Types a message into a EditText element.\n        onView(withId(R.id.edit_message))\n                .perform(typeText(MESSAGE), closeSoftKeyboard());\n\n        \/\/ Clicks a button to send the message to another\n        \/\/ activity through an explicit intent.\n        onView(withId(R.id.send_message)).perform(click());\n\n        \/\/ Verifies that the DisplayMessageActivity received an intent\n        \/\/ with the correct package name and message.\n        Intents.intended(allOf(\n                hasComponent(hasShortClassName(&quot;.DisplayMessageActivity&quot;)),\n                toPackage(PACKAGE_NAME),\n                hasExtra(MainActivity.EXTRA_MESSAGE, MESSAGE)));\n\n    }\n}<\/code><\/pre>\n<p>\u6709\u5173 Espresso Intents\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605Android\u6d4b\u8bd5\u652f\u6301\u5e93\u7f51\u7ad9\u4e0a\u7684Espresso Intent\u6587\u6863\u3002\u8fd8\u53ef\u4ee5\u4e0b\u8f7dIntentsBasicSample\u548cIntentsAdvancedSample\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<h2>\u4f7f\u7528Espresso Web\u6d4b\u8bd5WebViews<\/h2>\n<p>Espresso Web\u5141\u8bb8\u6d4b\u8bd5\u6d3b\u52a8\u4e2d\u5305\u542b\u7684WebView\u7ec4\u4ef6\u3002\u5b83\u4f7f\u7528WebDriver API\u6765\u68c0\u67e5\u548c\u63a7\u5236WebView\u7684\u884c\u4e3a\u3002<\/p>\n<p>\u8981\u5f00\u59cb\u4f7f\u7528Espresso Web\u8fdb\u884c\u6d4b\u8bd5\uff0c\u9700\u8981\u5728build.gradle\u4e2d\u6dfb\u52a0\u4f9d\u8d56\uff1a<\/p>\n<pre><code>dependencies {\n    androidTestImplementation &#039;com.android.support.test.espresso:espresso-web:3.0.1&#039;\n}<\/code><\/pre>\n<p>\u4f7f\u7528Espresso Web\u521b\u5efa\u6d4b\u8bd5\u65f6\uff0c\u9700\u8981\u5728\u5b9e\u4f8b\u5316ActivityTestRule\u5bf9\u8c61\u4ee5\u6d4b\u8bd5\u6d3b\u52a8\u65f6\u5728WebView\u4e0a\u542f\u7528JavaScript\u3002\u5728\u6d4b\u8bd5\u4e2d\uff0c\u53ef\u4ee5\u9009\u62e9\u663e\u793a\u5728WebView\u4e2d\u7684HTML\u5143\u7d20\uff0c\u5e76\u6a21\u62df\u7528\u6237\u4ea4\u4e92\uff0c\u4f8b\u5982\u5728\u6587\u672c\u6846\u4e2d\u8f93\u5165\u6587\u672c\uff0c\u7136\u540e\u5355\u51fb\u6309\u94ae\u3002\u5728\u5b8c\u6210\u64cd\u4f5c\u540e\uff0c\u53ef\u4ee5\u9a8c\u8bc1\u7f51\u9875\u4e0a\u7684\u7ed3\u679c\u662f\u5426\u7b26\u5408\u9884\u671f\u7ed3\u679c\u3002<\/p>\n<p>\u5728\u4ee5\u4e0b\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u8fd9\u4e2a\u7c7b\u6d4b\u8bd5\u4e00\u4e2aWebView\u7ec4\u4ef6\uff0c\u8be5\u7ec4\u4ef6\u5728\u88ab\u6d4b\u8bd5\u6d3b\u52a8\u4e2d\u7684id\u503c\u4e3a<code>web_view<\/code>\u3002<code>verifyValidInputYieldsSuccesfulSubmission()<\/code>\u6d4b\u8bd5\u9009\u62e9\u7f51\u9875\u4e0a\u7684<code>input<\/code>\u5143\u7d20\uff0c\u8f93\u5165\u4e00\u4e9b\u6587\u672c\uff0c\u5e76\u68c0\u67e5\u51fa\u73b0\u5728\u53e6\u4e00\u4e2a\u5143\u7d20\u4e2d\u7684\u6587\u672c\u3002<\/p>\n<pre><code class=\"language-java\">import android.support.test.espresso.web.webdriver.DriverAtoms;\nimport android.support.test.espresso.web.webdriver.Locator;\nimport android.support.test.filters.LargeTest;\nimport android.support.test.rule.ActivityTestRule;\nimport android.support.test.runner.AndroidJUnit4;\n\nimport org.junit.Rule;\nimport org.junit.Test;\nimport org.junit.runner.RunWith;\n\nimport static android.support.test.espresso.web.assertion.WebViewAssertions.webMatches;\nimport static android.support.test.espresso.web.sugar.Web.onWebView;\nimport static android.support.test.espresso.web.webdriver.DriverAtoms.clearElement;\nimport static android.support.test.espresso.web.webdriver.DriverAtoms.findElement;\nimport static android.support.test.espresso.web.webdriver.DriverAtoms.getText;\nimport static android.support.test.espresso.web.webdriver.DriverAtoms.webClick;\nimport static org.hamcrest.CoreMatchers.containsString;\n\n@LargeTest\n@RunWith(AndroidJUnit4.class)\npublic class WebViewActivityTest {\n    private static final String MACCHIATO = &quot;Macchiato&quot;;\n    private static final String DOPPIO = &quot;Doppio&quot;;\n\n    @Rule\n    public ActivityTestRule mActivityRule =\n            new ActivityTestRule(WebViewActivity.class,\n                    false \/* Initial touch mode *\/, false \/*  launch activity *\/) {\n                @Override\n                protected void afterActivityLaunched() {\n                    \/\/ Enable JavaScript.\n                    onWebView().forceJavascriptEnabled();\n                }\n            };\n\n    @Test\n    public void typeTextInInput_clickButton_SubmitsForm() {\n        \/\/ Lazily launch the Activity with a custom start Intent per test\n        mActivityRule.launchActivity(withWebFormIntent());\n\n        \/\/ Selects the WebView in your layout.\n        \/\/ If you have multiple WebViews you can also use a\n        \/\/ matcher to select a given WebView, onWebView(withId(R.id.web_view)).\n        onWebView()\n            \/\/ Find the input element by ID\n            .withElement(findElement(Locator.ID, &quot;text_input&quot;))\n            \/\/ Clear previous input\n            .perform(clearElement())\n            \/\/ Enter text into the input element\n            .perform(DriverAtoms.webKeys(MACCHIATO))\n            \/\/ Find the submit button\n            .withElement(findElement(Locator.ID, &quot;submit_btn&quot;))\n            \/\/ Simulate a click via JavaScript\n            .perform(webClick())\n            \/\/ Find the response element by ID\n            .withElement(findElement(Locator.ID, &quot;response&quot;))\n            \/\/ Verify that the response page contains the entered text\n            .check(webMatches(getText(), containsString(MACCHIATO)));\n    }\n}<\/code><\/pre>\n<p>\u6709\u5173Espresso Web\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605Android\u6d4b\u8bd5\u652f\u6301\u5e93\u7f51\u7ad9\u4e0a\u7684Espresso Web\u6587\u6863\u3002\u60a8\u4e5f\u53ef\u4ee5\u5c06\u6b64\u4ee3\u7801\u6bb5\u4f5c\u4e3aEspresso Web\u4ee3\u7801\u793a\u4f8b\u7684\u4e00\u90e8\u5206\u4e0b\u8f7d\u3002<\/p>\n<h2>\u9a8c\u8bc1\u7ed3\u679c<\/h2>\n<p>\u8c03\u7528<code>ViewInteraction.check()<\/code>\u6216<code>DataInteraction.check()<\/code>\u65b9\u6cd5\u6765\u58f0\u660eUI\u4e2d\u7684\u89c6\u56fe\u5339\u914d\u67d0\u4e2a\u9884\u671f\u7684\u72b6\u6001\u3002\u5fc5\u987b\u4f20\u9012\u7ed9ViewAssertion\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u3002\u5982\u679c\u65ad\u8a00\u5931\u8d25\uff0cEspresso\u5c06\u629b\u51fa\u4e00\u4e2aAssertionFailedError\u3002<\/p>\n<p>ViewAssertions\u7c7b\u63d0\u4f9b\u4e86\u7528\u4e8e\u6307\u5b9a\u516c\u5171\u65ad\u8a00\u7684\u5e2e\u52a9\u5668\u65b9\u6cd5\u5217\u8868\u3002\u53ef\u4ee5\u4f7f\u7528\u7684\u65ad\u8a00\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>doesNotExist\uff1a\u65ad\u8a00\u5f53\u524d\u89c6\u56fe\u5c42\u6b21\u7ed3\u6784\u4e2d\u6ca1\u6709\u4e0e\u6307\u5b9a\u6761\u4ef6\u5339\u914d\u7684\u89c6\u56fe<\/li>\n<li>matches\uff1a\u65ad\u8a00\u6307\u5b9a\u7684\u89c6\u56fe\u5b58\u5728\u4e8e\u5f53\u524d\u7684\u89c6\u56fe\u5c42\u6b21\u7ed3\u6784\u4e2d\uff0c\u5e76\u4e14\u5176\u72b6\u6001\u5339\u914d\u7ed9\u5b9a\u7684Hamcrest\u5339\u914d\u5668<\/li>\n<li>selectedDescendentsMatch\uff1a\u58f0\u660e\u6307\u5b9a\u7684\u7236\u89c6\u56fe\u7684\u5b50\u89c6\u56fe\u5b58\u5728\uff0c\u5e76\u4e14\u5b83\u4eec\u7684\u72b6\u6001\u5339\u914d\u7ed9\u5b9a\u7684Hamcrest\u5339\u914d\u5668<\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u7247\u6bb5\u663e\u793a\u5982\u4f55\u68c0\u67e5\u7528\u6237\u754c\u9762\u4e2d\u663e\u793a\u7684\u6587\u672c\u4e0e\u5148\u524d\u5728EditText\u5b57\u6bb5\u4e2d\u8f93\u5165\u7684\u6587\u672c\u5177\u6709\u76f8\u540c\u7684\u503c\u3002<\/p>\n<pre><code class=\"language-java\">public void testChangeText_sameActivity() {\n    \/\/ Type text and then press the button.\n    ...\n\n    \/\/ Check that the text was changed.\n    onView(withId(R.id.textToBeChanged))\n            .check(matches(withText(STRING_TO_BE_TYPED)));\n}<\/code><\/pre>\n<h2>\u5728\u8bbe\u5907\u6216\u6a21\u62df\u5668\u4e0a\u8fd0\u884cEspresso\u6d4b\u8bd5<\/h2>\n<p>\u53ef\u4ee5\u4eceAndroid Studio\u6216\u4ece\u547d\u4ee4\u884c\u8fd0\u884cEspresso\u6d4b\u8bd5\u3002\u786e\u4fdd\u5c06AndroidJUnitRunner\u6307\u5b9a\u4e3a\u9879\u76ee\u4e2d\u7684\u9ed8\u8ba4\u68c0\u6d4b\u5de5\u5177\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\uff1ahttps:\/\/developer.android.com\/training\/testing\/ui-te [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[222],"tags":[],"class_list":["post-777","post","type-post","status-publish","format-standard","hentry","category-android-test"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/777","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=777"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/777\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}