เอกสารนี้อธิบายวิธีตั้งค่าการทดสอบ Espresso ทั่วไปที่หลากหลาย
จับคู่มุมมองที่อยู่ติดกับอีกมุมมองหนึ่ง
เลย์เอาต์อาจมีมุมมองบางอย่างที่ไม่ซ้ำกันในตัวเอง ตัวอย่างเช่น ปุ่มโทรซ้ำในตารางรายชื่อติดต่ออาจมี R.id เดียวกัน มีข้อความเดียวกัน และมีพร็อพเพอร์ตี้เดียวกันกับปุ่มโทรอื่นๆ ภายในลำดับชั้นการแสดงผล
ตัวอย่างเช่น ในกิจกรรมนี้ มุมมองที่มีข้อความ "7" จะซ้ำกันในหลายแถว

บ่อยครั้งที่มุมมองที่ไม่ซ้ำกันจะจับคู่กับป้ายกำกับที่ไม่ซ้ำกันซึ่งอยู่ข้างๆ เช่น ชื่อของผู้ติดต่อข้างปุ่มโทร ในกรณีนี้ คุณสามารถใช้ตัวจับคู่ hasSibling() เพื่อจำกัดการเลือกให้แคบลงได้ ดังนี้
onView(allOf(withText("7"), hasSibling(withText("item: 0")))) .perform(click())
จับคู่มุมมองที่อยู่ภายในแถบการดำเนินการ
ActionBarTestActivity มีแถบการดำเนินการ 2 แถบที่แตกต่างกัน ได้แก่ แถบการดำเนินการปกติและแถบการดำเนินการตามบริบทที่สร้างขึ้นจากเมนูตัวเลือก แถบการดำเนินการทั้ง 2 แถบมีรายการ 1 รายการที่มองเห็นได้เสมอ และรายการ 2 รายการที่มองเห็นได้เฉพาะในเมนูรายการเพิ่มเติม เมื่อมีการคลิกรายการ ระบบจะเปลี่ยน TextView เป็นเนื้อหาของรายการที่คลิก
การจับคู่ไอคอนที่มองเห็นได้บนแถบการดำเนินการทั้ง 2 แถบนั้นทำได้ง่ายๆ ดังที่แสดงในข้อมูลโค้ดต่อไปนี้
fun testClickActionBarItem() { // We make sure the contextual action bar is hidden. onView(withId(R.id.hide_contextual_action_bar)) .perform(click()) // Click on the icon - we can find it by the r.Id. onView(withId(R.id.action_save)) .perform(click()) // Verify that we have really clicked on the icon // by checking the TextView content. onView(withId(R.id.text_action_bar_result)) .check(matches(withText("Save"))) }

โค้ดจะมีลักษณะเหมือนกันสำหรับแถบการดำเนินการตามบริบท ดังนี้
fun testClickActionModeItem() { // Make sure we show the contextual action bar. onView(withId(R.id.show_contextual_action_bar)) .perform(click()) // Click on the icon. onView((withId(R.id.action_lock))) .perform(click()) // Verify that we have really clicked on the icon // by checking the TextView content. onView(withId(R.id.text_action_bar_result)) .check(matches(withText("Lock"))) }

การคลิกรายการในเมนูรายการเพิ่มเติมจะยุ่งยากกว่าเล็กน้อยสำหรับการดำเนินการปกติ เนื่องจากอุปกรณ์บางเครื่องมีปุ่มเมนูรายการเพิ่มเติมที่เป็นฮาร์ดแวร์ ซึ่งจะเปิด รายการเพิ่มเติมในเมนูตัวเลือก และอุปกรณ์บางเครื่องมีปุ่มเมนูรายการเพิ่มเติมที่เป็นซอฟต์แวร์ ซึ่งจะเปิดเมนูรายการเพิ่มเติมปกติ โชคดีที่ Espresso จัดการเรื่องนี้ให้เรา
สำหรับแถบการดำเนินการปกติ ให้ทำดังนี้
fun testActionBarOverflow() { // Make sure we hide the contextual action bar. onView(withId(R.id.hide_contextual_action_bar)) .perform(click()) // Open the options menu OR open the overflow menu, depending on whether // the device has a hardware or software overflow menu button. openActionBarOverflowOrOptionsMenu( ApplicationProvider.getApplicationContext<Context>() ) // Click the item. onView(withText("World")) .perform(click()) // Verify that we have really clicked on the icon by checking // the TextView content. onView(withId(R.id.text_action_bar_result)) .check(matches(withText("World"))) }

ลักษณะที่ปรากฏในอุปกรณ์ที่มีปุ่มเมนูรายการเพิ่มเติมบนฮาร์ดแวร์มีดังนี้

สำหรับแถบการดำเนินการตามบริบทนั้นทำได้ง่ายมาก ดังนี้
fun testActionModeOverflow() { // Show the contextual action bar. onView(withId(R.id.show_contextual_action_bar)) .perform(click()) // Open the overflow menu from contextual action mode. openContextualActionModeOverflowMenu() // Click on the item. onView(withText("Key")) .perform(click()) // Verify that we have really clicked on the icon by // checking the TextView content. onView(withId(R.id.text_action_bar_result)) .check(matches(withText("Key"))) }

หากต้องการดูโค้ดแบบเต็มของตัวอย่างเหล่านี้ ให้ดูตัวอย่าง ActionBarTest.java ใน GitHub
ยืนยันว่ามุมมองไม่แสดง
หลังจากดำเนินการหลายอย่างแล้ว คุณจะต้องยืนยันสถานะของ UI ที่อยู่ระหว่างการทดสอบอย่างแน่นอน บางครั้งอาจเป็นกรณีเชิงลบ เช่น เมื่อไม่มีเหตุการณ์เกิดขึ้น โปรดทราบว่าคุณสามารถเปลี่ยน Hamcrest View Matcher เป็น ViewAssertion ได้โดยใช้ ViewAssertions.matches()
ในตัวอย่างด้านล่าง เราใช้ตัวจับคู่ isDisplayed() และกลับค่าโดยใช้ตัวจับคู่ not() มาตรฐาน
// import androidx.test.espresso.Espresso.onView // import androidx.test.espresso.assertion.ViewAssertions.matches // import androidx.test.espresso.matcher.ViewMatchers.isDisplayed // import androidx.test.espresso.matcher.ViewMatchers.withId // import org.hamcrest.Matchers.not onView(withId(R.id.bottom_left)) .check(matches(not(isDisplayed())))
แนวทางข้างต้นจะใช้ได้หากมุมมองยังคงเป็นส่วนหนึ่งของลำดับชั้น หากไม่เป็นเช่นนั้น คุณจะได้รับ NoMatchingViewException และต้องใช้ ViewAssertions.doesNotExist()
ยืนยันว่าไม่มีมุมมองอยู่
หากมุมมองหายไปจากลำดับชั้นการแสดงผล ซึ่งอาจเกิดขึ้นเมื่อการดำเนินการทำให้เกิดการเปลี่ยนไปใช้กิจกรรมอื่น คุณควรใช้ ViewAssertions.doesNotExist() ดังนี้
// import androidx.test.espresso.Espresso.onView // import androidx.test.espresso.assertion.ViewAssertions.doesNotExist // import androidx.test.espresso.matcher.ViewMatchers.withId onView(withId(R.id.bottom_left)) .check(doesNotExist())
ยืนยันว่ารายการข้อมูลไม่ได้อยู่ในอะแดปเตอร์
หากต้องการพิสูจน์ว่ารายการข้อมูลหนึ่งๆ ไม่อยู่ใน AdapterView คุณจะต้องดำเนินการแตกต่างออกไปเล็กน้อย เราต้องหา AdapterView ที่เราสนใจและตรวจสอบข้อมูลที่เก็บไว้ เราไม่จำเป็นต้องใช้ onData()
แต่เราใช้ onView() เพื่อค้นหา AdapterView แล้วใช้ Matcher อื่นเพื่อทำงานกับข้อมูลภายในมุมมองแทน
อันดับแรกคือตัวจับคู่
private fun withAdaptedData(dataMatcher: Matcher<Any>): Matcher<View> { return object : TypeSafeMatcher<View>() { override fun describeTo(description: Description) { description.appendText("with class name: ") dataMatcher.describeTo(description) } public override fun matchesSafely(view: View): Boolean { if (view !is AdapterView<*>) { return false } val adapter = view.adapter for (i in 0 until adapter.count) { if (dataMatcher.matches(adapter.getItem(i))) { return true } } return false } } }
จากนั้นสิ่งที่เราต้องการก็คือ onView() เพื่อค้นหา AdapterView ดังนี้
fun testDataItemNotInAdapter() { onView(withId(R.id.list)) .check(matches(not(withAdaptedData(withItemContent("item: 168"))))) }
และเรามีการยืนยันที่จะไม่สำเร็จหากมีรายการที่เท่ากับ "item: 168" อยู่ในมุมมองอะแดปเตอร์ที่มีรายการรหัส
ดูตัวอย่างแบบเต็มได้ที่เมธอด testDataItemNotInAdapter() ภายในคลาส AdapterViewTest.java ใน GitHub
ใช้เครื่องจัดการความล้มเหลวที่กำหนดเอง
การแทนที่ FailureHandler เริ่มต้นใน Espresso ด้วย ที่กำหนดเองจะช่วยให้สามารถ
จัดการข้อผิดพลาดเพิ่มเติมหรือแตกต่างออกไปได้ เช่น การจับภาพหน้าจอหรือการส่ง
ข้อมูลการแก้ไขข้อบกพร่องเพิ่มเติม
ตัวอย่าง CustomFailureHandlerTest แสดงวิธีติดตั้งใช้งานเครื่องจัดการความล้มเหลวที่กำหนดเอง
private class CustomFailureHandler(targetContext: Context) : FailureHandler { private val delegate: FailureHandler init { delegate = DefaultFailureHandler(targetContext) } override fun handle(error: Throwable, viewMatcher: Matcher<View>) { try { delegate.handle(error, viewMatcher) } catch (e: NoMatchingViewException) { throw MySpecialException(e) } } }
เครื่องมือจัดการความล้มเหลวนี้จะแสดง MySpecialException แทน NoMatchingViewException และมอบสิทธิ์ความล้มเหลวอื่นๆ ทั้งหมดให้กับ DefaultFailureHandler คุณสามารถลงทะเบียน CustomFailureHandler ด้วย
Espresso ในเมธอด setUp() ของการทดสอบได้ดังนี้
@Throws(Exception::class) override fun setUp() { super.setUp() getActivity() setFailureHandler( CustomFailureHandler( ApplicationProvider.getApplicationContext<Context>() ) ) }
ดูข้อมูลเพิ่มเติมได้ที่อินเทอร์เฟซ FailureHandler และ Espresso.setFailureHandler()
กำหนดเป้าหมายหน้าต่างที่ไม่ใช่ค่าเริ่มต้น
Android รองรับหลายหน้าต่าง โดยปกติแล้ว ผู้ใช้และนักพัฒนาแอปจะไม่เห็นหน้าต่างเหล่านี้ แต่ในบางกรณีอาจมีหน้าต่างหลายหน้าต่างปรากฏขึ้น เช่น เมื่อหน้าต่างเติมข้อความอัตโนมัติแสดงขึ้นเหนือหน้าต่างแอปพลิเคชันหลักในวิดเจ็ต Search Espresso จะใช้การวิเคราะห์พฤติกรรมเพื่อคาดเดา Window ที่คุณต้องการโต้ตอบด้วยโดยค่าเริ่มต้นเพื่อลดความซับซ้อน การเรียนรู้ด้วยตนเองนี้เกือบจะดีพอเสมอ อย่างไรก็ตาม ในบางกรณีที่เกิดขึ้นไม่บ่อยนัก คุณจะต้องระบุหน้าต่างที่การโต้ตอบควรกำหนดเป้าหมาย ซึ่งทำได้โดยระบุตัวจับคู่หน้าต่างรูทของคุณเองหรือตัวจับคู่ Root ดังนี้
onView(withText("South China Sea")) .inRoot(withDecorView(not(`is`(getActivity().getWindow().getDecorView())))) .perform(click())
Espresso มีตัวจับคู่รูทในตัวใน androidx.test.espresso.matcher.RootMatchers เช่น withDecorView(), isDialog() และ isPlatformPopup() คล้ายกับ androidx.test.espresso.matcher.ViewMatchers ดูสรุปภาพรวมอย่างรวดเร็วของ Matcher ในตัวได้ที่เอกสารสรุปของ Espresso หรือใช้ออบเจ็กต์ Matcher<Root> ของคุณเอง
ดูตัวอย่าง MultipleWindowTest ใน GitHub
จับคู่ส่วนหัวหรือส่วนท้ายในมุมมองรายการ
ระบบจะเพิ่มส่วนหัวและส่วนท้ายลงใน ListViews โดยใช้เมธอด addHeaderView() และ addFooterView() เพื่อให้แน่ใจว่า Espresso.onData() ทราบว่าต้องจับคู่ออบเจ็กต์ข้อมูลใด โปรดส่งค่าออบเจ็กต์ข้อมูลที่กำหนดล่วงหน้าเป็นพารามิเตอร์ที่ 2 ไปยัง addHeaderView() และ addFooterView() เช่น
const val FOOTER = "FOOTER" // ... val footerView = layoutInflater.inflate(R.layout.list_item, listView, false) footerView.findViewById<TextView>(R.id.item_content).text = "count:" footerView.findViewById<TextView>(R.id.item_size).text = data.size.toString() listView.addFooterView(footerView, FOOTER, true)
จากนั้นคุณสามารถเขียน Matcher สำหรับส่วนท้ายได้ดังนี้
// import org.hamcrest.Matchers.allOf // import org.hamcrest.Matchers.instanceOf // import org.hamcrest.Matchers.`is` fun isFooter(): Matcher<Any> { return allOf( `is`(instanceOf(String::class.java)), `is`(LongListActivity.FOOTER) ) }
และการโหลดมุมมองในการทดสอบก็เป็นเรื่องง่ายๆ ดังนี้
// import androidx.test.espresso.Espresso.onData // import androidx.test.espresso.action.ViewActions.click // import androidx.test.espresso.sample.LongListMatchers.isFooter fun testClickFooter() { onData(isFooter()) .perform(click()) // ... }
ดูตัวอย่างโค้ดแบบเต็มได้ในเมธอด testClickFooter() ของ AdapterViewTest.java บน GitHub