Best Appium-espresso-driver code snippet using android.support.test.espresso.matcher.ViewMatchers.isEnabled
BackupActivityValidateSwitchBetweenScreens.kt
Source:BackupActivityValidateSwitchBetweenScreens.kt  
...13import android.support.test.espresso.intent.Intents14import android.support.test.espresso.intent.Intents.intended15import android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent16import android.support.test.espresso.matcher.ViewMatchers.isDisplayed17import android.support.test.espresso.matcher.ViewMatchers.isEnabled18import android.support.test.espresso.matcher.ViewMatchers.withClassName19import android.support.test.espresso.matcher.ViewMatchers.withId20import android.support.test.espresso.matcher.ViewMatchers.withParent21import android.support.test.espresso.matcher.ViewMatchers.withText22import android.support.test.filters.LargeTest23import android.support.test.rule.ActivityTestRule24import android.support.test.runner.AndroidJUnit425import android.support.test.uiautomator.UiDevice26import android.widget.TextView27import kin.backup.and.restore.ui.tests.UiTestUtils.childAtPosition28import kin.backup.and.restore.ui.tests.UiTestUtils.hasValueEqualTo29import kin.backup.and.restore.ui.tests.UiTestUtils.isSameDrawable30import kin.backupandrestore.backup.view.BackupActivity31import kin.sdk.Environment32import kin.sdk.KinClient33import org.hamcrest.Matchers.`is`34import org.hamcrest.Matchers.allOf35import org.hamcrest.Matchers.instanceOf36import org.hamcrest.Matchers.not37import org.hamcrest.core.IsInstanceOf38import org.junit.After39import org.junit.Before40import org.junit.Rule41import org.junit.Test42import org.junit.runner.RunWith43import kotlin.test.assertEquals44@LargeTest45@RunWith(AndroidJUnit4::class)46class BackupActivityValidateSwitchBetweenScreens {47    private lateinit var kinClient: KinClient48    private lateinit var stubIntent: Intent49    private var componentName: ComponentName? = null50    @Rule51    @JvmField52    var activityTestRule = ActivityTestRule(BackupActivity::class.java, false, false)53    @Before54    fun setup() {55        kinClient = KinClient(InstrumentationRegistry.getTargetContext(), Environment.TEST, UiTestUtils.appId, UiTestUtils.storeKey)56        kinClient.clearAllAccounts()57        val kinAccount = kinClient.addAccount()58        stubIntent = UiTestUtils.createStubIntent(BackupActivity::class.java.name, kinAccount.publicAddress)59        Intents.init()60        componentName = UiTestUtils.componentName61    }62    @After63    fun teardown() {64        if (::kinClient.isInitialized) {65            kinClient.clearAllAccounts()66        }67    }68    @Test69    fun backupActivity_FirstScreenToSecondScreen_CorrectComponents() {70        launchActivity()71        val backImageButton = onView(72            allOf(childAtPosition(73                allOf(withId(R.id.toolbar),74                    childAtPosition(75                        IsInstanceOf.instanceOf(android.view.ViewGroup::class.java),76                        0)),77                0),78                isDisplayed()))79        backImageButton.check(matches(isDisplayed()))80            .check(matches(isSameDrawable(R.drawable.back)))81        val toolbarTitle = onView(allOf(instanceOf(TextView::class.java), withParent(withId(R.id.toolbar))))82        toolbarTitle.check(doesNotExist())83        val textView = onView(withId(R.id.steps_text))84        textView.check(matches(withText("")))85            .check(matches(not(isDisplayed())))86        val appCompatButton = onView(87            allOf(withId(R.id.lets_go_button), withText(R.string.backup_and_restore_lets_go),88                isDisplayed()))89        appCompatButton.perform(click())90        closeSoftKeyboard()91        onView(allOf(instanceOf(TextView::class.java),92            withParent(withId(R.id.toolbar))))93            .check(matches(withText(R.string.backup_and_restore_create_password)))94        val textView2 = onView(95            allOf(withId(R.id.steps_text), withText("1/2"),96                isDisplayed()))97        textView2.check(matches(withText("1/2")))98        val targetContext = InstrumentationRegistry.getTargetContext()99        onView(withId(R.id.enter_pass_edittext)).check(matches(hasValueEqualTo(targetContext.resources.getString(R.string.backup_and_restore_enter_password))))100        onView(withId(R.id.confirm_pass_edittext)).check(matches(hasValueEqualTo(targetContext.resources.getString(R.string.backup_and_restore_confirm_password))))101        val button = onView(102            allOf(withId(R.id.next_button), not(isEnabled()), isDisplayed()))103        button.check(matches(isDisplayed()))104    }105    @Test106    fun backupActivity_SecondScreenToThirdScreen_CorrectComponents() {107        launchActivity()108        val letsGoButton = onView(allOf(withId(R.id.lets_go_button), withText(R.string.backup_and_restore_lets_go)))109        letsGoButton.perform(click())110        closeSoftKeyboard()111        val password = onView(112            allOf(childAtPosition(113                allOf(withId(R.id.enter_pass_edittext),114                    childAtPosition(115                        withClassName(`is`("android.support.constraint.ConstraintLayout")),116                        2)),117                0),118                isDisplayed()))119        password.perform(replaceText("qwertyU1!"))120        val confirm = onView(121            allOf(childAtPosition(122                allOf(withId(R.id.confirm_pass_edittext),123                    childAtPosition(124                        withClassName(`is`("android.support.constraint.ConstraintLayout")),125                        3)),126                0),127                isDisplayed()))128        confirm.perform(replaceText("qwertyU1!"))129        closeSoftKeyboard()130        val understandCheckbox = onView(withId(R.id.understand_checkbox))131        understandCheckbox.perform(click())132        val nextButton = onView(withId(R.id.next_button))133        nextButton.perform(click())134        val toolbarTitle = onView(allOf(instanceOf(TextView::class.java), withParent(withId(R.id.toolbar))))135        toolbarTitle.check(matches(withText(R.string.backup_and_restore_my_kin_wallet_qr_code)))136        val textView = onView(137            allOf(withId(R.id.steps_text), withText("2/2"),138                isDisplayed()))139        textView.check(matches(withText("2/2")))140        val button = onView(141            allOf(withId(R.id.send_email_button), isEnabled(), isDisplayed()))142        button.check(matches(isDisplayed()))143            .check(matches(isEnabled()))144        val imageButton = onView(145            allOf(childAtPosition(146                allOf(withId(R.id.toolbar),147                    childAtPosition(148                        IsInstanceOf.instanceOf(android.view.ViewGroup::class.java),149                        0)),150                0),151                isDisplayed()))152        imageButton.check(matches(isDisplayed()))153            .check(matches(isSameDrawable(R.drawable.back)))154    }155    @Test156    fun backupActivity_ThirdScreen_ToFourth_CorrectComponents() {157        launchActivity()158        val letsGoButton = onView(allOf(withId(R.id.lets_go_button), withText(R.string.backup_and_restore_lets_go)))159        letsGoButton.perform(click())160        val password = onView(161            allOf(childAtPosition(162                allOf(withId(R.id.enter_pass_edittext),163                    childAtPosition(164                        withClassName(`is`("android.support.constraint.ConstraintLayout")),165                        2)),166                0),167                isDisplayed()))168        password.perform(replaceText("qwertyU1!"))169        val confirm = onView(170            allOf(childAtPosition(171                allOf(withId(R.id.confirm_pass_edittext),172                    childAtPosition(173                        withClassName(`is`("android.support.constraint.ConstraintLayout")),174                        3)),175                0),176                isDisplayed()))177        confirm.perform(replaceText("qwertyU1!"))178        closeSoftKeyboard()179        val understandCheckbox = onView(withId(R.id.understand_checkbox))180        understandCheckbox.perform(click())181        val nextButton = onView(withId(R.id.next_button))182        nextButton.perform(click())183        val toolbarTitle = onView(allOf(instanceOf(TextView::class.java), withParent(withId(R.id.toolbar))))184        toolbarTitle.check(matches(withText(R.string.backup_and_restore_my_kin_wallet_qr_code)))185        val button = onView(186            allOf(withId(R.id.send_email_button), isEnabled(), isDisplayed()))187        button.perform(click())188        val device = UiDevice.getInstance(getInstrumentation())189        device.pressBack()190        val understandCheckbox2 = onView(withId(R.id.i_saved_my_qr_checkbox))191        understandCheckbox2.perform(click())192        val toolbarTitle2 = onView(allOf(instanceOf(TextView::class.java), withParent(withId(R.id.toolbar))))193        toolbarTitle2.check(doesNotExist())194        val textView = onView(withId(R.id.steps_text))195        textView.check(matches(withText("")))196            .check(matches(not(isDisplayed())))197        val imageButton = onView(198            allOf(childAtPosition(199                allOf(withId(R.id.toolbar),200                    childAtPosition(...RestoreActivityValidateSwitchBetweenScreens.kt
Source:RestoreActivityValidateSwitchBetweenScreens.kt  
...12import android.support.test.espresso.intent.Intents13import android.support.test.espresso.intent.Intents.intended14import android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent15import android.support.test.espresso.matcher.ViewMatchers.isDisplayed16import android.support.test.espresso.matcher.ViewMatchers.isEnabled17import android.support.test.espresso.matcher.ViewMatchers.withClassName18import android.support.test.espresso.matcher.ViewMatchers.withId19import android.support.test.espresso.matcher.ViewMatchers.withParent20import android.support.test.espresso.matcher.ViewMatchers.withText21import android.support.test.filters.LargeTest22import android.support.test.rule.ActivityTestRule23import android.support.test.runner.AndroidJUnit424import android.widget.TextView25import kin.backup.and.restore.ui.tests.UiTestUtils.childAtPosition26import kin.backup.and.restore.ui.tests.UiTestUtils.hasValueEqualTo27import kin.backup.and.restore.ui.tests.UiTestUtils.intendingStubQRIntent28import kin.backup.and.restore.ui.tests.UiTestUtils.isSameDrawable29import kin.backup.and.restore.ui.tests.UiTestUtils.isTransparentDrawable30import kin.backupandrestore.restore.view.RestoreActivity31import kin.sdk.Environment32import kin.sdk.KinClient33import org.hamcrest.Matcher34import org.hamcrest.Matchers.`is`35import org.hamcrest.Matchers.allOf36import org.hamcrest.Matchers.instanceOf37import org.hamcrest.Matchers.not38import org.hamcrest.core.IsInstanceOf39import org.junit.After40import org.junit.Before41import org.junit.Rule42import org.junit.Test43import org.junit.runner.RunWith44import kotlin.test.assertEquals45@LargeTest46@RunWith(AndroidJUnit4::class)47class RestoreActivityValidateSwitchBetweenScreens {48    private lateinit var kinClient: KinClient49    private lateinit var stubIntent: Intent50    private var componentName: ComponentName? = null51    private var expectedIntent: Matcher<Intent>? = null52    @get: Rule53    var activityTestRule = ActivityTestRule(RestoreActivity::class.java, false, false)54    @Before55    fun setup() {56        kinClient = KinClient(InstrumentationRegistry.getTargetContext(), Environment.TEST, UiTestUtils.appId, UiTestUtils.storeKey)57        kinClient.clearAllAccounts()58        stubIntent = UiTestUtils.createStubIntent(RestoreActivity::class.java.name)59        componentName = UiTestUtils.componentName60    }61    @After62    fun teardown() {63        if (::kinClient.isInitialized) {64            kinClient.clearAllAccounts()65        }66    }67    @Test68    fun restoreActivity_UploadAnDialogScreen_CorrectComponents() {69        launchActivity()70        Intents.init()71        expectedIntent = intendingStubQRIntent()72        val backImageButton = onView(73            allOf(childAtPosition(74                allOf(withId(R.id.toolbar),75                    childAtPosition(76                        IsInstanceOf.instanceOf(android.view.ViewGroup::class.java),77                        0)),78                0),79                isDisplayed()))80        backImageButton.check(matches(isDisplayed()))81            .check(matches(isSameDrawable(R.drawable.back)))82        val toolbarTitle = onView(allOf(instanceOf(TextView::class.java), withParent(withId(R.id.toolbar))))83        toolbarTitle.check(doesNotExist())84        val textView = onView(withId(R.id.steps_text))85        textView.check(matches(withText("")))86            .check(matches(not(isDisplayed())))87        val appCompatTextView = onView(88            allOf(withId(R.id.upload_btn_text), withText(R.string.backup_and_restore_upload_qr_btn),89                childAtPosition(90                    childAtPosition(91                        withId(R.id.fragment_frame),92                        0),93                    5),94                isDisplayed()))95        appCompatTextView.perform(click())96        val appCompatButton2 = onView(97            allOf(withId(android.R.id.button1), withText("OK"),98                childAtPosition(99                    childAtPosition(100                        withId(R.id.buttonPanel),101                        0),102                    3)))103        appCompatButton2.perform(click())104        Intents.release()105        closeSoftKeyboard()106        val targetContext = InstrumentationRegistry.getTargetContext()107        onView(withId(R.id.kinrecovery_password_edit))108            .check(matches(hasValueEqualTo(targetContext.resources.getString(R.string.backup_and_restore_enter_password))))109        onView(allOf(instanceOf(TextView::class.java),110            withParent(withId(R.id.toolbar))))111            .check(matches(withText(R.string.backup_and_restore_upload_qr_title)))112        val stepsTextView = onView(withId(R.id.steps_text))113        stepsTextView.check(matches(withText("")))114            .check(matches(not(isDisplayed())))115        val button = onView(116            allOf(withId(R.id.kinrecovery_password_done_btn), isDisplayed()))117        button.check(matches(isDisplayed()))118            .check(matches(isEnabled()))119    }120    @Test121    fun restoreActivity_PasswordScreenToDone_CorrectComponents() {122        launchActivity()123        Intents.init()124        expectedIntent = intendingStubQRIntent()125        val appCompatTextView = onView(126            allOf(withId(R.id.upload_btn_text), withText(R.string.backup_and_restore_upload_qr_btn),127                childAtPosition(128                    childAtPosition(129                        withId(R.id.fragment_frame),130                        0),131                    5),132                isDisplayed()))133        appCompatTextView.perform(click())134        val appCompatButton2 = onView(135            allOf(withId(android.R.id.button1), withText("OK"),136                childAtPosition(137                    childAtPosition(138                        withId(R.id.buttonPanel),139                        0),140                    3)))141        appCompatButton2.perform(click())142        Intents.release()143        val editText = onView(144            allOf(childAtPosition(145                allOf(withId(R.id.kinrecovery_password_edit),146                    childAtPosition(147                        withClassName(`is`("android.support.constraint.ConstraintLayout")),148                        3)),149                0),150                isDisplayed()))151        editText.perform(typeText("qwertyU1!"), closeSoftKeyboard())152        val doneButton = onView(153            allOf(withId(R.id.kinrecovery_password_done_btn), isDisplayed()))154        doneButton.perform(click())155        onView(allOf(instanceOf(TextView::class.java),156            withParent(withId(R.id.toolbar))))157            .check(matches(withText(R.string.backup_and_restore_restore_completed_title)))158        val stepsTextView = onView(withId(R.id.steps_text))159        stepsTextView.check(matches(withText("")))160            .check(matches(not(isDisplayed())))161        val imageButton = onView(162            allOf(childAtPosition(163                allOf(withId(R.id.toolbar),164                    childAtPosition(165                        IsInstanceOf.instanceOf(android.view.ViewGroup::class.java),166                        0)),167                0)))168        imageButton.check(matches(isTransparentDrawable(R.drawable.back)))169        val button = onView(170            allOf(withId(R.id.backup_and_restore_v_btn), isDisplayed()))171        button.check(matches(isDisplayed()))172            .check(matches(isEnabled()))173        button.perform(click())174        val f = Activity::class.java.getDeclaredField("mResultCode")175        f.isAccessible = true176        val resultCode = f.getInt(activityTestRule.activity)177        assertEquals(resultCode, 5000) //5000 is success178    }179    //TODO For full coverage we still need to add method to check when pressing back from screen B then go to Screen A and everything is correct180    //TODO Also when putting wrong password or bad QR code181    private fun launchActivity() {182        Intents.init()183        activityTestRule.launchActivity(stubIntent)184        // Check the intent is handled by the app185        val expectedIntent = hasComponent(componentName)186        intended(expectedIntent)...SignInActivityTest.kt
Source:SignInActivityTest.kt  
...24import android.support.test.espresso.assertion.ViewAssertions.matches25import android.support.test.espresso.matcher.ViewMatchers.isChecked26import android.support.test.espresso.matcher.ViewMatchers.isClickable27import android.support.test.espresso.matcher.ViewMatchers.isDisplayed28import android.support.test.espresso.matcher.ViewMatchers.isEnabled29import android.support.test.espresso.matcher.ViewMatchers.isFocusable30import android.support.test.espresso.matcher.ViewMatchers.withId31import android.support.test.espresso.matcher.ViewMatchers.withText32import android.support.test.filters.LargeTest33import android.support.test.rule.ActivityTestRule34import android.support.test.runner.AndroidJUnit435import android.view.View36import com.google.samples.apps.topeka.base.R37import com.google.samples.apps.topeka.TestLogin38import com.google.samples.apps.topeka.helper.login39import com.google.samples.apps.topeka.helper.logout40import com.google.samples.apps.topeka.model.Avatar41import com.google.samples.apps.topeka.model.TEST_AVATAR42import com.google.samples.apps.topeka.model.TEST_FIRST_NAME43import com.google.samples.apps.topeka.model.TEST_LAST_INITIAL44import org.hamcrest.Matcher45import org.hamcrest.Matchers.equalTo46import org.hamcrest.Matchers.isEmptyOrNullString47import org.hamcrest.Matchers.not48import org.junit.Before49import org.junit.Rule50import org.junit.Test51import org.junit.runner.RunWith52@RunWith(AndroidJUnit4::class)53@LargeTest54class SignInActivityTest {55    @Suppress("unused") // actually used by Espresso56    val rule57        @Rule get() = object :58                ActivityTestRule<SignInActivity>(SignInActivity::class.java) {59            override fun beforeActivityLaunched() {60                InstrumentationRegistry.getTargetContext().logout()61                login = TestLogin62            }63            override fun getActivityIntent(): Intent {64                val targetContext = InstrumentationRegistry.getTargetContext()65                return Intent(targetContext, SignInActivity::class.java).putExtra("EDIT", true)66            }67        }68    @Before fun clearPreferences() {69        InstrumentationRegistry.getTargetContext().logout()70    }71    @Test fun checkFab_initiallyNotDisplayed() {72        onView(withId(R.id.done)).check(matches(not(isDisplayed())))73    }74    @Test fun signIn_withoutFirstNameFailed() {75        inputData(null, TEST_LAST_INITIAL, TEST_AVATAR)76        onDoneView().check(matches(not(isDisplayed())))77    }78    @Test fun signIn_withoutLastInitialFailed() {79        inputData(TEST_FIRST_NAME, null, TEST_AVATAR)80        onDoneView().check(matches(not(isDisplayed())))81    }82    @Test fun signIn_withoutAvatarFailed() {83        inputData(TEST_FIRST_NAME, TEST_LAST_INITIAL, null)84        onDoneView().check(matches(not(isDisplayed())))85    }86    @Test fun signIn_withAllPlayerPreferencesSuccessfully() {87        inputData(TEST_FIRST_NAME, TEST_LAST_INITIAL, TEST_AVATAR)88        onDoneView().check(matches(isDisplayed()))89    }90    /* TODO Debug: Espresso does currently not continue after this test. Commenting to keep pace.91    @Test fun signIn_performSignIn() {92        inputData(TEST_FIRST_NAME, TEST_LAST_INITIAL, TEST_AVATAR)93        onDoneView().perform(click())94        assertThat(InstrumentationRegistry.getTargetContext().isLoggedIn(), `is`(true))95    }96    */97    private fun onDoneView() = onView(withId(R.id.done))98    @Test fun signIn_withLongLastName() {99        typeAndHideKeyboard(R.id.last_initial, TEST_FIRST_NAME)100        val expectedValue = TEST_FIRST_NAME[0].toString()101        onView(withId(R.id.last_initial)).check(matches(withText(expectedValue)))102    }103    private fun inputData(firstName: String?, lastInitial: String?, avatar: Avatar?) {104        if (firstName != null) typeAndHideKeyboard(R.id.first_name, firstName)105        if (lastInitial != null) typeAndHideKeyboard(R.id.last_initial, lastInitial)106        if (avatar != null) clickAvatar(avatar)107    }108    private fun typeAndHideKeyboard(targetViewId: Int, text: String) {109        onView(withId(targetViewId)).perform(typeText(text), closeSoftKeyboard())110    }111    private fun clickAvatar(avatar: Avatar) {112        onData(equalTo(avatar))113                .inAdapterView(withId(R.id.avatars))114                .perform(click())115    }116    @Test fun firstName_isInitiallyEmpty() = editTextIsEmpty(R.id.first_name)117    @Test fun lastInitial_isInitiallyEmpty() = editTextIsEmpty(R.id.last_initial)118    private fun editTextIsEmpty(id: Int) {119        onView(withId(id)).check(matches(withText(isEmptyOrNullString())))120    }121    @Test fun avatar_allDisplayed() = checkOnAvatar(isDisplayed())122    @Test fun avatar_isEnabled() = checkOnAvatar(isEnabled())123    @Test fun avatar_notFocusable() = checkOnAvatar(not(isFocusable()))124    @Test fun avatar_notClickable() = checkOnAvatar(not(isClickable()))125    @Test fun avatar_noneChecked() = checkOnAvatar(not(isChecked()))126    private fun checkOnAvatar(matcher: Matcher<View>) {127        (0 until Avatar.values().size).forEach {128            onData(equalTo(Avatar.values()[it]))129                    .inAdapterView(withId(R.id.avatars))130                    .check(matches(matcher))131        }132    }133}...ViewMatchersTest.kt
Source:ViewMatchersTest.kt  
...24import android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom25import android.support.test.espresso.matcher.ViewMatchers.isChecked26import android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA27import android.support.test.espresso.matcher.ViewMatchers.isDisplayed28import android.support.test.espresso.matcher.ViewMatchers.isEnabled29import android.support.test.espresso.matcher.ViewMatchers.isFocusable30import android.support.test.espresso.matcher.ViewMatchers.isSelected31import android.support.test.espresso.matcher.ViewMatchers.supportsInputMethods32import android.support.test.espresso.matcher.ViewMatchers.withChild33import android.support.test.espresso.matcher.ViewMatchers.withClassName34import android.support.test.espresso.matcher.ViewMatchers.withContentDescription35import android.support.test.espresso.matcher.ViewMatchers.withHint36import android.support.test.espresso.matcher.ViewMatchers.withId37import android.support.test.espresso.matcher.ViewMatchers.withParent38import android.support.test.espresso.matcher.ViewMatchers.withText39import org.hamcrest.CoreMatchers.allOf40import org.hamcrest.CoreMatchers.`is`41import org.hamcrest.CoreMatchers.not42/**43 * Lists all ViewMatchers. ViewMatchers here are without functional load.44 * This is done for demonstration purposes.45 */46@RunWith(AndroidJUnit4::class)47class ViewMatchersTest {48    @Test49    fun userProperties() {50        onView(withId(R.id.fab_add_task))51        onView(withText("All TO-DOs"))52        onView(withContentDescription(R.string.menu_filter))53        onView(hasContentDescription())54        onView(withHint(R.string.name_hint))55    }56    @Test57    fun uiProperties() {58        onView(isDisplayed())59        onView(isEnabled())60        onView(isChecked())61        onView(isSelected())62    }63    @Test64    fun objectMatcher() {65        onView(not<View>(isChecked()))66        onView(allOf<View>(withText("item 1"), isChecked()))67    }68    @Test69    fun hierarchy() {70        onView(withParent(withId(R.id.todo_item)))71        onView(withChild(withText("item 2")))72        onView(isDescendantOfA(withId(R.id.todo_item)))73        onView(hasDescendant(isChecked()))74                .check(matches(isDisplayed()))75                .check(matches(isFocusable()))76        onView(hasSibling(withContentDescription(R.string.menu_filter)))77    }78    @Test79    fun input() {80        onView(supportsInputMethods())81        onView(hasImeAction(EditorInfo.IME_ACTION_SEND))82    }83    @Test84    fun classMatchers() {85        onView(isAssignableFrom(CheckBox::class.java))86        onView(withClassName(`is`(FloatingActionButton::class.java.canonicalName)))87    }88    @Test89    fun rootMatchers() {90        onView(isFocusable())91        onView(withText(R.string.name_hint)).inRoot(isTouchable())92        onView(withText(R.string.name_hint)).inRoot(isDialog())93        onView(withText(R.string.name_hint)).inRoot(isPlatformPopup())94    }95    @Test96    fun preferenceMatchers() {97        onData(withSummaryText("3 days"))98        onData(withTitle(R.string.pref_title_send_notifications))99        onData(withKey("example_switch"))100        onView(isEnabled())101    }102    @Test103    fun layoutMatchers() {104        onView(hasEllipsizedText())105        onView(hasMultilineText())106    }107}...MainActivityTest.kt
Source:MainActivityTest.kt  
...73        Espresso.onView(ViewMatchers.withId(R.id.quest_rerollButton))74                .perform(ViewActions.click())75        Thread.sleep(1000)76        Espresso.onView(ViewMatchers.withId(R.id.quest_rerollButton))77                .check(matches(not(isEnabled())))78    }79    @Test80    fun mainActivitySpinnerSelect() {81        Espresso.onView(ViewMatchers.withId(R.id.main_OpenMenu))82                .perform(ViewActions.click())83        Espresso.onView(ViewMatchers.withId(R.id.main_Race))84                .perform(ViewActions.click())85        Espresso.onView(ViewMatchers.withId(R.id.race_TimeSpinner))86                .perform(ViewActions.click())87        Espresso.onData(hasToString(startsWith("30")))88                .perform(ViewActions.click())89    }90}...EnterActivityTest.kt
Source:EnterActivityTest.kt  
...19        checkViewVisibility(ViewMatchers.withId(R.id.titleInput), ViewMatchers.Visibility.VISIBLE)20        checkViewVisibility(ViewMatchers.withId(R.id.networkInput), ViewMatchers.Visibility.VISIBLE)21        // The options menu item should be visible but disabled by default.22        checkViewVisibility(ViewMatchers.withId(R.id.save), ViewMatchers.Visibility.VISIBLE)23        Espresso.onView(ViewMatchers.withId(R.id.save)).check(ViewAssertions.matches(CoreMatchers.not(ViewMatchers.isEnabled())))24    }25    /** Check that the save option is correctly enabled or disabled depending on the state */26    @Test27    fun testSaveEnabling() {28        // Edit a value into the name edit text box and ensure the save button is still not enabled.29        val text = "Some video text"30        val network = "HBO Go"31        // When the title name is entered but the network is not we should not be able to save32        onView(withId(R.id.titleInput)).perform(replaceText(text))33        onView(withId(R.id.save)).check(matches(not(isEnabled())))34        onView(withId(R.id.networkInput)).perform(pressImeActionButton()).check(matches(isDisplayed()))35        // When the title is not entered but the network is entered, we should still not be able to save36        onView(withId(R.id.titleInput)).perform(replaceText(""))37        onView(withId(R.id.networkInput)).perform(replaceText(network))38        onView(withId(R.id.save)).check(matches(not(isEnabled())))39        onView(withId(R.id.networkInput)).perform(pressImeActionButton()).check(matches(isDisplayed()))40        // When both have text, we should be able to save.41        onView(withId(R.id.titleInput)).perform(replaceText(text))42        onView(withId(R.id.networkInput)).perform(replaceText(network))43        onView(withId(R.id.save)).check(matches(isEnabled())).perform(click())44//        Espresso.onView(ViewMatchers.withId(R.id.networkInput)).perform(ViewActions.pressImeActionButton())45        Assert.assertTrue(rule.activity.isFinishing)46    }47}ViewMatchers.kt
Source:ViewMatchers.kt  
...8import org.hamcrest.Matcher9import androidx.test.espresso.matcher.ViewMatchers.hasFocus as espressoHasFocus10import androidx.test.espresso.matcher.ViewMatchers.isChecked as espressoIsChecked11import androidx.test.espresso.matcher.ViewMatchers.isDisplayed as espressoIsDisplayed12import androidx.test.espresso.matcher.ViewMatchers.isEnabled as espressoIsEnabled13import androidx.test.espresso.matcher.ViewMatchers.isSelected as espressoIsSelected14// These functions are defined at the top-level so they appear in autocomplete, like the static methods on15// Android's [ViewMatchers] class.16/**17 * The existing [espressoHasFocus] function but uses a boolean to invert rather than requiring the [not] matcher.18 */19fun hasFocus(hasFocus: Boolean): Matcher<View> = maybeInvertMatcher(espressoHasFocus(), hasFocus)20/**21 * The existing [espressoIsChecked] function but uses a boolean to invert rather than requiring the [not] matcher.22 */23fun isChecked(isChecked: Boolean): Matcher<View> = maybeInvertMatcher(espressoIsChecked(), isChecked)24/**25 * The existing [espressoIsDisplayed] function but uses a boolean to invert rather than requiring the [not] matcher.26 */27fun isDisplayed(isDisplayed: Boolean): Matcher<View> = maybeInvertMatcher(espressoIsDisplayed(), isDisplayed)28/**29 * The existing [espressoIsEnabled] function but uses a boolean to invert rather than requiring the [not] matcher.30 */31fun isEnabled(isEnabled: Boolean): Matcher<View> = maybeInvertMatcher(espressoIsEnabled(), isEnabled)32/**33 * The existing [espressoIsSelected] function but uses a boolean to invert rather than requiring the [not] matcher.34 */35fun isSelected(isSelected: Boolean): Matcher<View> = maybeInvertMatcher(espressoIsSelected(), isSelected)...ViewAssertions.kt
Source:ViewAssertions.kt  
...10fun ViewInteraction.isNotDisplayed() = check(matches(not(ViewMatchers.isDisplayed())))11fun ViewInteraction.doesNotExist() = check(ViewAssertions.doesNotExist())12fun ViewInteraction.hasText(@StringRes textId: Int) = check(matches(withText(textId)))13fun ViewInteraction.hasText(text: String) = check(matches(withText(text)))14fun ViewInteraction.isEnabled() = check(matches(ViewMatchers.isEnabled()))15fun ViewInteraction.isDisabled() = check(matches(not(ViewMatchers.isEnabled())))16fun ViewInteraction.isChecked() = check(matches(ViewMatchers.isChecked()))17fun ViewInteraction.isNotChecked() = check(matches(not(ViewMatchers.isChecked())))...isEnabled
Using AI Code Generation
1import static android.support.test.espresso.matcher.ViewMatchers.isEnabled;2import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;3import static android.support.test.espresso.matcher.ViewMatchers.withId;4import static android.support.test.espresso.matcher.ViewMatchers.withText;5import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;6import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;7import static android.support.test.espresso.matcher.ViewMatchers.isClickable;8import static android.support.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed;9import static android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA;10import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant;11import static android.support.test.espresso.matcher.ViewMatchers.hasFocus;12import static android.support.test.espresso.matcher.ViewMatchers.hasSibling;13import static android.support.test.espresso.matcher.ViewMatchers.isRoot;14import static android.support.test.espresso.matcher.ViewMatchers.isFocusable;15import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility;16import static android.support.test.espresso.matcher.ViewMatchers.withHint;17import static android.support.test.espresso.matcher.ViewMatchers.withSpinnerText;isEnabled
Using AI Code Generation
1import android.support.test.espresso.matcher.ViewMatchers;2ViewMatchers.isEnabled();3import android.support.test.espresso.matcher.ViewMatchers;4ViewMatchers.isFocusable();5import android.support.test.espresso.matcher.ViewMatchers;6ViewMatchers.isFocused();7import android.support.test.espresso.matcher.ViewMatchers;8ViewMatchers.isLongClickable();9import android.support.test.espresso.matcher.ViewMatchers;10ViewMatchers.isNotChecked();11import android.support.test.espresso.matcher.ViewMatchers;12ViewMatchers.isNotChecked();13import android.support.test.espresso.matcher.ViewMatchers;14ViewMatchers.isRoot();15import android.support.test.espresso.matcher.ViewMatchers;16ViewMatchers.isSelected();17import android.support.test.espresso.matcher.ViewMatchers;18ViewMatchers.isClickable();19import android.support.test.espresso.matcher.ViewMatchers;20ViewMatchers.isDescendantOfA();21import android.support.test.espresso.matcher.ViewMatchers;22ViewMatchers.withContentDescription();23import android.support.test.espresso.matcher.ViewMatchers;24ViewMatchers.withEffectiveVisibility();25import android.support.test.espresso.matcher.ViewMatchers;26ViewMatchers.withId();27import android.support.test.espresso.matcher.ViewMatchers;28ViewMatchers.withInputType();29import android.support.test.espresso.matcher.ViewMatchers;30ViewMatchers.withParent();31import android.support.test.espresso.matcher.ViewMatchers;32ViewMatchers.withSpinnerText();isEnabled
Using AI Code Generation
1ViewInteraction appCompatButton = onView(2allOf(withId(R.id.button), withText("Button"),3childAtPosition(4childAtPosition(5withClassName(is("android.widget.LinearLayout")),6isDisplayed()));7appCompatButton.perform(click());8ViewInteraction appCompatButton2 = onView(9allOf(withId(R.id.button), withText("Button"),10childAtPosition(11childAtPosition(12withClassName(is("android.widget.LinearLayout")),13isDisplayed()));14appCompatButton2.perform(click());15}16private static Matcher<View> childAtPosition(17final Matcher<View> parentMatcher, final int position) {18return new TypeSafeMatcher<View>() {19@Override public void describeTo(Description description) {20description.appendText("Child at position " + position + " in parent ");21parentMatcher.describeTo(description);22}23@Override public boolean matchesSafely(View view) {24ViewGroup parent = (ViewGroup) view.getParent();25return parentMatcher.matches(parent)26&& view.equals(parent.getChildAt(position));27}28};29}30}isEnabled
Using AI Code Generation
1onView(withId(R.id.button)).check(matches(isEnabled()));2onView(withId(R.id.button)).check(matches(isNot(isEnabled())));3onView(withId(R.id.button)).check(matches(isClickable()));4onView(withId(R.id.button)).check(matches(isNot(isClickable())));5onView(withId(R.id.button)).check(matches(isSelected()));6onView(withId(R.id.button)).check(matches(isNot(isSelected())));7onView(withId(R.id.button)).check(matches(isChecked()));8onView(withId(R.id.button)).check(matches(isNot(isChecked())));9onView(withId(R.id.button)).check(matches(isDisplayed()));isEnabled
Using AI Code Generation
1package com.example.myapp;2import android.support.test.espresso.matcher.ViewMatchers;3import android.support.test.rule.ActivityTestRule;4import android.support.test.runner.AndroidJUnit4;5import org.junit.Rule;6import org.junit.Test;7import org.junit.runner.RunWith;8import static android.support.test.espresso.Espresso.onView;9import static android.support.test.espresso.assertion.ViewAssertions.matches;10import static android.support.test.espresso.matcher.ViewMatchers.isEnabled;11import static android.support.test.espresso.matcher.ViewMatchers.withText;12@RunWith(AndroidJUnit4.class)13public class EspressoTest {14    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(15            MainActivity.class);16    public void checkButtonEnabled() {17        onView(withText("Click Me")).check(matches(isEnabled()));18    }19}20package com.example.myapp;21import android.support.test.espresso.matcher.ViewMatchers;22import android.support.test.rule.ActivityTestRule;23import android.support.test.runner.AndroidJUnit4;24import org.junit.Rule;25import org.junit.Test;26import org.junit.runner.RunWith;27import static android.support.test.espresso.Espresso.onView;28import static android.support.test.espresso.assertion.ViewAssertions.matches;29import static android.support.test.espresso.matcher.ViewMatchers.isClickable;30import static android.support.test.espresso.matcher.ViewMatchers.withText;31@RunWith(AndroidJUnit4.class)32public class EspressoTest {33    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(34            MainActivity.class);35    public void checkButtonClickable() {36        onView(withText("Click Me")).check(matches(isClickable()));37    }38}Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
