Android Academy · पाठ

Espresso के साथ UI परीक्षण

Espresso के साथ स्वचालित UI tests लिखें। onView() से views खोजें, perform() से उनके साथ इंटरैक्ट करें, check() से स्थिति की पुष्टि करें और RecyclerView इंटरैक्शन का परीक्षण करें।

पाठ 3, कुल 4 में से13 चरण

Espresso के साथ UI परीक्षण, CoddyKit पर Android Academy का एक निःशुल्क पाठ है। यह 4 में से 3वाँ पाठ है। इस अध्ययन पथ के 3 तक कोई भी पाठ पूरा पढ़ना निःशुल्क है — इसके बाद CoddyKit PRO हर पाठ अनलॉक करता है, साथ ही अंतर्निर्मित कोड संपादक और चौबीसों घंटे एआई शिक्षक के साथ व्यावहारिक अभ्यास भी उपलब्ध कराता है। यह Android Academy सीखने के मार्ग का हिस्सा है और आपकी प्रगति वेब तथा CoddyKit ऐप पर सिंक होती रहती है। Android Academy पाठ्यक्रम में कुल 4 पाठ शामिल हैं।

Espresso क्या है?

Espresso Android का आधिकारिक UI परीक्षण framework है। यह आपको ऐसे परीक्षण लिखने देता है जो आपके ऐप के UI के साथ interaction करते हैं — buttons पर टैप करना, text लिखना, lists को scroll करना — और स्क्रीन पर दिख रही चीज़ का अभिकथन करते हैं।

Espresso किसी device या emulator पर चलता है (instrumented test) और UI thread के साथ अपने-आप synchronization करता है — flaky sleep() calls की आवश्यकता नहीं होती।

सेटअप

Espresso Android testing toolkit में शामिल है। इन dependencies को app/build.gradle में जोड़ें:

// app/build.gradle:
android {
    defaultConfig {
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
}

dependencies {
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.5.1'  // RecyclerView
}

ActivityScenarioRule

प्रत्येक परीक्षण से पहले Activity लॉन्च करने और उसके बाद बंद करने के लिए ActivityScenarioRule का उपयोग करें:

@RunWith(AndroidJUnit4::class)
class LoginActivityTest {

    @get:Rule
    val activityRule = ActivityScenarioRule(LoginActivity::class.java)

    @Test
    fun `login button is displayed`() {
        onView(withId(R.id.btnLogin))
            .check(matches(isDisplayed()))
    }
}

onView() और ViewMatchers

onView(matcher) वर्तमान hierarchy में कोई view ढूँढ़ता है। सटीकता के लिए matchers को मिलाएँ:

// Match by ID:
onView(withId(R.id.tvTitle))

// Match by text:
onView(withText("Submit"))

// Match by content description (for accessibility):
onView(withContentDescription("Close dialog"))

// Combine matchers:
onView(allOf(withId(R.id.btnSave), isEnabled()))

// Inside a specific parent:
onView(withId(R.id.btnSave)).inRoot(isDialog())

ViewActions — Views के साथ interaction

किसी view को ढूँढ़ने के बाद .perform() से कोई action करें:

// Tap a button:
onView(withId(R.id.btnLogin)).perform(click())

// Type into an EditText:
onView(withId(R.id.etEmail))
    .perform(typeText("alice@example.com"), closeSoftKeyboard())

// Clear text and retype:
onView(withId(R.id.etPassword))
    .perform(clearText(), typeText("secret123"), closeSoftKeyboard())

// Scroll to a view:
onView(withId(R.id.btnSubmit)).perform(scrollTo(), click())

// Long press:
onView(withId(R.id.ivImage)).perform(longClick())

ViewAssertions — स्थिति सत्यापित करना

.check() से जाँचें कि स्क्रीन पर क्या दिख रहा है:

// View is displayed:
onView(withId(R.id.tvError)).check(matches(isDisplayed()))

// View has specific text:
onView(withId(R.id.tvTitle)).check(matches(withText("Welcome!")))

// View is not displayed (GONE or INVISIBLE):
onView(withId(R.id.progressBar)).check(matches(not(isDisplayed())))

// View is enabled:
onView(withId(R.id.btnSave)).check(matches(isEnabled()))

// View does not exist:
onView(withId(R.id.tvError)).check(doesNotExist())

पूर्ण Login परीक्षण

Credentials लिखने और login पर टैप करने वाला पूरा परीक्षण:

@Test
fun `successful login shows home screen`() {
    // Type email
    onView(withId(R.id.etEmail))
        .perform(typeText("alice@example.com"), closeSoftKeyboard())

    // Type password
    onView(withId(R.id.etPassword))
        .perform(typeText("password123"), closeSoftKeyboard())

    // Tap login button
    onView(withId(R.id.btnLogin)).perform(click())

    // Assert welcome message is shown
    onView(withId(R.id.tvWelcome))
        .check(matches(withText("Welcome, Alice!")))
}

RecyclerView का परीक्षण

सूची के items के साथ interaction करने के लिए espresso-contrib से RecyclerViewActions का उपयोग करें:

import androidx.test.espresso.contrib.RecyclerViewActions

@Test
fun `clicking item opens detail screen`() {
    // Scroll to position 5 and click:
    onView(withId(R.id.recyclerView))
        .perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(
            5, click()
        ))

    // Assert detail screen is shown:
    onView(withId(R.id.tvDetailTitle)).check(matches(isDisplayed()))
}

Input Validation का परीक्षण

जब input अमान्य हो, तब error messages दिखाई देने का सत्यापन करें:

@Test
fun `empty email shows error message`() {
    // Leave email blank, tap login
    onView(withId(R.id.btnLogin)).perform(click())

    // Assert error is shown
    onView(withId(R.id.tilEmail))
        .check(matches(hasDescendant(withText("Email is required"))))
}

@Test
fun `short password shows error`() {
    onView(withId(R.id.etPassword))
        .perform(typeText("123"), closeSoftKeyboard())
    onView(withId(R.id.btnLogin)).perform(click())
    onView(withId(R.id.tilPassword))
        .check(matches(hasDescendant(withText("Minimum 6 characters"))))
}

Idling Resources

Espresso UI thread के idle होने तक अपने-आप प्रतीक्षा करता है, लेकिन network calls या coroutines के लिए नहीं। अपने asynchronous work के लिए Espresso को प्रतीक्षा कराने हेतु IdlingResource का उपयोग करें:

// CountingIdlingResource: increment before async work, decrement when done
val idlingResource = CountingIdlingResource("NetworkCall")

// In your ViewModel/Repository:
idlingResource.increment()
viewModelScope.launch {
    loadData()
    idlingResource.decrement()
}

// In test setUp:
@Before
fun setUp() {
    IdlingRegistry.getInstance().register(idlingResource)
}

@After
fun tearDown() {
    IdlingRegistry.getInstance().unregister(idlingResource)
}

Espresso परीक्षण चलाना

किसी connected device या emulator पर instrumented tests चलाएँ:

  • Android Studio में: किसी परीक्षण class पर right-click करें → Run
  • Terminal से: ./gradlew connectedAndroidTest
  • परिणाम app/build/reports/androidTests/ में दिखाई देंगे

स्थिर और तेज़ परीक्षण runs के लिए Android Emulator API 28+ का उपयोग करें। Flakiness रोकने के लिए developer settings में animations बंद करें।

त्वरित जाँच

किसी view को ढूँढ़ने और उसके text की जाँच करने का सही Espresso पैटर्न क्या है?

पुनरावलोकन: Espresso के साथ UI परीक्षण

स्वचालित UI परीक्षण उपयोगकर्ताओं के सामने आने से पहले regressions पकड़ लेते हैं:

  • onView(matcher) — कोई view ढूँढ़ें
  • .perform(action) — टैप करें, लिखें, scroll करें
  • .check(matches(...)) — visibility, text और state का अभिकथन करें
  • सूची interactions के लिए RecyclerViewActions
  • asynchronous operations के लिए IdlingResource
  • flaky tests रोकने के लिए system animations बंद करें

अगला विषय: अपने ऐप को विशेषज्ञ की तरह debug और profile करें।

शुरुआत निःशुल्क

एआई शिक्षक के साथ Kotlin सीखें — निःशुल्क

अपने ब्राउज़र में वास्तविक कोड लिखें और चलाएँ, चौबीसों घंटे एआई शिक्षक से तुरंत सहायता पाएँ, और वेब या ऐप पर वहीं से शुरू करें जहाँ आपने छोड़ा था।

पाठ्यक्रम
36
पाठ
152

अक्सर पूछे जाने वाले प्रश्न

क्या “Espresso के साथ UI परीक्षण” पाठ निःशुल्क है?

हाँ — Android Academy अध्ययन पथ के 3 तक कोई भी पाठ, जिसमें “Espresso के साथ UI परीक्षण” भी शामिल है, यहाँ वेब पर पूरा पढ़ना निःशुल्क है। इसके बाद CoddyKit PRO हर पाठ अनलॉक करता है, साथ ही अंतर्निर्मित कोड संपादक और चौबीसों घंटे एआई शिक्षक के साथ इंटरैक्टिव अभ्यास भी उपलब्ध कराता है। Android Academy पाठ्यक्रम में कुल 4 पाठ शामिल हैं।

“Espresso के साथ UI परीक्षण” में मैं क्या सीखूँगा?

Espresso के साथ स्वचालित UI tests लिखें। onView() से views खोजें, perform() से उनके साथ इंटरैक्ट करें, check() से स्थिति की पुष्टि करें और RecyclerView इंटरैक्शन का परीक्षण करें। आप ब्राउज़र में सीधे चलाए जाने वाले व्यावहारिक कोड के साथ Android Academy का अभ्यास करते हैं, और पाठ पूरा करते समय 24/7 एआई ट्यूटर आपके प्रश्नों के उत्तर देता है।

क्या Android Academy शुरू करने के लिए मुझे किसी अनुभव की आवश्यकता है?

पहले के अनुभव की आवश्यकता नहीं है। CoddyKit पर Android Academy शुरुआती से लेकर उन्नत शिक्षार्थियों तक सभी के लिए व्यवस्थित किया गया है, इसलिए आप यहीं से या शुरुआत से सीखना शुरू कर सकते हैं और अपनी गति से आगे बढ़ सकते हैं। यह 4 में से 3वाँ पाठ है।

“Espresso के साथ UI परीक्षण” पाठ पूरा करने में कितना समय लगता है?

CoddyKit का अधिकांश पाठ लगभग 5–10 मिनट में पूरा हो जाता है। हर पाठ छोटा और संवादात्मक है, इसलिए आप लगातार प्रगति करते हैं और वेब या ऐप पर वहीं से सीखना जारी रख सकते हैं जहाँ आपने छोड़ा था।

क्या मैं इस Android Academy पाठ में कोड लिख और चला सकता हूँ?

हाँ। हर Android Academy पाठ में एक अंतर्निर्मित कोड संपादक शामिल है, जिससे आप सीधे अपने ब्राउज़र में वास्तविक कोड लिख और चला सकते हैं और तुरंत एआई प्रतिक्रिया पा सकते हैं—स्थानीय सेटअप की आवश्यकता नहीं है।

इस पाठ्यक्रम के सभी पाठ

  1. JUnit के साथ यूनिट परीक्षण
  2. Mockito के साथ मॉकिंग
  3. Espresso के साथ UI परीक्षण
  4. डीबगिंग और प्रोफाइलिंग
← Android Academy पर वापस जाएँ