एस्प्रेसो रेसिपी

इस दस्तावेज़ में, Espresso के अलग-अलग तरह के सामान्य टेस्ट सेट अप करने का तरीका बताया गया है.

एक व्यू को दूसरे व्यू के बगल में मैच करना

किसी लेआउट में कुछ ऐसे व्यू हो सकते हैं जो अपने-आप में यूनीक नहीं हैं. उदाहरण के लिए, संपर्कों की टेबल में बार-बार दिखने वाले कॉल बटन का R.id एक जैसा हो सकता है. साथ ही, उसमें एक जैसा टेक्स्ट हो सकता है. इसके अलावा, व्यू हैरारकी (व्यू और व्यू ग्रुप के लेआउट का क्रम) में मौजूद अन्य कॉल बटन जैसी प्रॉपर्टी हो सकती हैं.

उदाहरण के लिए, इस गतिविधि में "7" टेक्स्ट वाला व्यू, कई पंक्तियों में दोहराया जाता है:

एक सूची गतिविधि, जिसमें तीन आइटम वाली सूची में एक ही व्यू एलिमेंट की तीन कॉपी दिखाई गई हैं

अक्सर, यूनीक न होने वाले व्यू को उसके बगल में मौजूद किसी यूनीक लेबल के साथ जोड़ा जाएगा. जैसे, कॉल बटन के बगल में मौजूद संपर्क का नाम. इस मामले में, hasSibling() मैचर का इस्तेमाल करके, अपनी पसंद को सीमित किया जा सकता है:

onView(allOf(withText("7"), hasSibling(withText("item: 0"))))
    .perform(click())

कार्रवाई बार में मौजूद व्यू से मैच करना

ActionBarTestActivity में दो अलग-अलग कार्रवाई बार होते हैं: एक सामान्य कार्रवाई बार और एक काम का कार्रवाई बार, जिसे विकल्प मेन्यू से बनाया जाता है. दोनों कार्रवाई बार में एक आइटम होता है, जो हमेशा दिखता है. साथ ही, दो आइटम होते हैं, जो सिर्फ़ ओवरफ़्लो मेन्यू में दिखते हैं. किसी आइटम पर क्लिक करने से, TextView उस आइटम के कॉन्टेंट में बदल जाता है.

दोनों कार्रवाई बार पर दिखने वाले आइकॉन को मैच करना आसान है. इसके लिए, यहां दिया गया कोड स्निपेट देखें:

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")))
}

कार्रवाई बार में ओवरफ़्लो मेन्यू बटन दिखता है. साथ ही, विकल्पों की सूची कार्रवाई बार के नीचे, स्क्रीन के सबसे ऊपर दिखती है

इन सैंपल का पूरा कोड देखने के लिए, GitHub पर मौजूद ActionBarTest.java सैंपल देखें.

यह दावा करना कि कोई व्यू नहीं दिखाया गया है

कई कार्रवाइयां करने के बाद, आपको टेस्ट किए जा रहे यूज़र इंटरफ़ेस (यूआई) की स्थिति के बारे में ज़रूर बताना होगा. कभी-कभी, यह नेगेटिव केस हो सकता है. जैसे, जब कुछ नहीं हो रहा हो. ध्यान रखें कि ViewAssertions.matches() का इस्तेमाल करके, किसी भी Hamcrest व्यू मैचर को ViewAssertion में बदला जा सकता है.

नीचे दिए गए उदाहरण में, हम 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() का इस्तेमाल करने की ज़रूरत नहीं है. इसके बजाय, हम AdapterView को ढूंढने के लिए onView() का इस्तेमाल करते हैं. इसके बाद, व्यू में मौजूद डेटा पर काम करने के लिए, दूसरे मैचर का इस्तेमाल करते हैं.

सबसे पहले, मैच करने वाला:

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
        }
    }
}

इसके बाद, AdapterView का पता लगाने के लिए, हमें सिर्फ़ onView() की ज़रूरत होगी:

fun testDataItemNotInAdapter() {
    onView(withId(R.id.list))
        .check(matches(not(withAdaptedData(withItemContent("item: 168")))))
}

और हमारे पास एक दावा है जो विफल हो जाएगा यदि कोई आइटम जो "item: 168" के बराबर है आईडी सूची के साथ एक एडेप्टर दृश्य में मौजूद है.

पूरा सैंपल देखने के लिए, GitHub पर AdapterViewTest.java क्लास में मौजूद testDataItemNotInAdapter() तरीका देखें.

कस्टम फ़ेलियर हैंडलर का इस्तेमाल करना

Espresso में डिफ़ॉल्ट FailureHandler को कस्टम से बदलने पर, गड़बड़ी को ज़्यादा बेहतर तरीके से हैंडल किया जा सकता है. जैसे, स्क्रीनशॉट लेना या डीबग करने की अतिरिक्त जानकारी पास करना.

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)
        }
    }
}

यह फ़ेलर हैंडलर, NoMatchingViewException के बजाय MySpecialException दिखाता है. साथ ही, अन्य सभी गड़बड़ियों को 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())

androidx.test.espresso.matcher.ViewMatchers की तरह ही, Espresso में androidx.test.espresso.matcher.RootMatchers में पहले से मौजूद रूट मैचर होते हैं. जैसे, withDecorView(), isDialog(), और isPlatformPopup(). पहले से मौजूद मैचर की खास जानकारी को तुरंत देखने के लिए, Espresso चीट शीट देखें या अपना Matcher<Root> ऑब्जेक्ट लागू करें.

GitHub पर MultipleWindowTest सैंपल देखें.

ListViews में हेडर और फ़ुटर जोड़ने के लिए, addHeaderView() और addFooterView() मेथड का इस्तेमाल किया जाता है. यह पक्का करने के लिए कि Espresso.onData() को पता हो कि किस डेटा ऑब्जेक्ट से मैच करना है, addHeaderView() और addFooterView() के लिए दूसरे पैरामीटर के तौर पर, पहले से सेट की गई डेटा ऑब्जेक्ट वैल्यू को पास करना न भूलें. उदाहरण के लिए:

इसके बाद, फ़ुटर के लिए एक मैचर लिखा जा सकता है:

टेस्ट में व्यू लोड करना आसान है:

GitHub पर AdapterViewTest.java के testClickFooter() तरीके में मौजूद, पूरा कोड सैंपल देखें.