Best Appium-espresso-driver code snippet using android.support.test.espresso.matcher.ViewMatchers.withHint
PageExtensions.kt
Source:PageExtensions.kt
...50fun BasePage.onViewWithResourceName(arg0: Matcher<String>): ViewInteraction = Espresso.onView(ViewMatchers.withResourceName(arg0))51fun BasePage.onViewWithTagKey(arg0: Int, arg1: Matcher<Any>): ViewInteraction = Espresso.onView(ViewMatchers.withTagKey(arg0, arg1))52fun BasePage.onViewWithTagKey(arg0: Int): ViewInteraction = Espresso.onView(ViewMatchers.withTagKey(arg0))53fun BasePage.onViewWithTagValue(arg0: Matcher<Any>): ViewInteraction = Espresso.onView(ViewMatchers.withTagValue(arg0))54fun BasePage.onViewWithHint(arg0: Int): ViewInteraction = Espresso.onView(ViewMatchers.withHint(arg0))55fun BasePage.onViewWithHint(arg0: Matcher<String>): ViewInteraction = Espresso.onView(ViewMatchers.withHint(arg0))56fun BasePage.onViewWithHint(arg0: String): ViewInteraction = Espresso.onView(ViewMatchers.withHint(arg0))57fun BasePage.onViewWithEffectiveVisibility(arg0: ViewMatchers.Visibility): ViewInteraction = Espresso.onView(ViewMatchers.withEffectiveVisibility(arg0))58fun BasePage.onViewWithChild(arg0: Matcher<View>): ViewInteraction = Espresso.onView(ViewMatchers.withChild(arg0))59fun BasePage.onViewWithSpinnerText(arg0: String): ViewInteraction = Espresso.onView(ViewMatchers.withSpinnerText(arg0))60fun BasePage.onViewWithSpinnerText(arg0: Matcher<String>): ViewInteraction = Espresso.onView(ViewMatchers.withSpinnerText(arg0))61fun BasePage.onViewWithSpinnerText(arg0: Int): ViewInteraction = Espresso.onView(ViewMatchers.withSpinnerText(arg0))62fun BasePage.onViewWithInputType(arg0: Int): ViewInteraction = Espresso.onView(ViewMatchers.withInputType(arg0))63fun BasePage.waitForViewWithParent(arg0: Matcher<View>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withParent(arg0))64fun BasePage.waitForViewWithText(arg0: String): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withText(arg0))65fun BasePage.waitForViewWithText(arg0: Matcher<String>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withText(arg0))66fun BasePage.waitForViewWithText(arg0: Int): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withText(arg0))67fun BasePage.waitForViewWithId(arg0: Int): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withId(arg0))68fun BasePage.waitForViewWithId(arg0: Matcher<Int>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withId(arg0))69fun BasePage.waitForViewWithClassName(arg0: Matcher<String>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withClassName(arg0))70fun BasePage.waitForViewWithContentDescription(arg0: Int): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withContentDescription(arg0))71fun BasePage.waitForViewWithContentDescription(arg0: Matcher<out CharSequence>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withContentDescription(arg0))72fun BasePage.waitForViewWithContentDescription(arg0: String): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withContentDescription(arg0))73fun BasePage.waitForViewWithResourceName(arg0: String): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withResourceName(arg0))74fun BasePage.waitForViewWithResourceName(arg0: Matcher<String>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withResourceName(arg0))75fun BasePage.waitForViewWithTagKey(arg0: Int, arg1: Matcher<Any>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withTagKey(arg0, arg1))76fun BasePage.waitForViewWithTagKey(arg0: Int): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withTagKey(arg0))77fun BasePage.waitForViewWithTagValue(arg0: Matcher<Any>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withTagValue(arg0))78fun BasePage.waitForViewWithHint(arg0: Int): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withHint(arg0))79fun BasePage.waitForViewWithHint(arg0: Matcher<String>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withHint(arg0))80fun BasePage.waitForViewWithHint(arg0: String): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withHint(arg0))81fun BasePage.waitForViewWithEffectiveVisibility(arg0: ViewMatchers.Visibility): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withEffectiveVisibility(arg0))82fun BasePage.waitForViewWithChild(arg0: Matcher<View>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withChild(arg0))83fun BasePage.waitForViewWithSpinnerText(arg0: String): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withSpinnerText(arg0))84fun BasePage.waitForViewWithSpinnerText(arg0: Matcher<String>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withSpinnerText(arg0))85fun BasePage.waitForViewWithSpinnerText(arg0: Int): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withSpinnerText(arg0))86fun BasePage.waitForViewWithInputType(arg0: Int): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withInputType(arg0))87fun BasePage.getStringFromResource(stringResource: Int): String{88 val targetContext = InstrumentationRegistry.getTargetContext()89 return targetContext.resources.getString(stringResource)90}91fun BasePage.callOnClick(matcher: Matcher<View>) = ViewCallOnClick.callOnClick(matcher)92fun BasePage.scrollTo(viewId: Int) = BaristaScrollInteractions.safelyScrollTo(viewId)93fun BasePage.scrollTo(text: String) = BaristaScrollInteractions.safelyScrollTo(text)94fun BasePage.waitScrollClick(viewId: Int) {...
ContactFormTest.kt
Source:ContactFormTest.kt
...50 }51 @Test52 fun testRetryButton() {53 configureContactForm()54 Espresso.onView(ViewMatchers.withHint(PHONE_FIELD)).perform(typeText("81995614241"))55 Espresso.onView(ViewMatchers.withHint(NAME_FIELD)).perform(typeText("Felipe Joffer"))56 Espresso.onView(ViewMatchers.withHint(EMAIL_FIELD)).perform(typeText("felipejmartins@gmail.com"), closeSoftKeyboard())57 Espresso.onView(ViewMatchers.withText(SEND_FIELD)).perform(ViewActions.click())58 Espresso.onView(ViewMatchers.withId(R.id.message_success)).check(matches(isDisplayed()))59 Espresso.onView(ViewMatchers.withId(R.id.title_success)).check(matches(isDisplayed()))60 Espresso.onView(ViewMatchers.withId(R.id.btn_retry_message)).perform(click())61 Espresso.onView(ViewMatchers.withId(R.id.message_success)).check(matches(not(isDisplayed())))62 Espresso.onView(ViewMatchers.withId(R.id.title_success)).check(matches(not(isDisplayed())))63 Espresso.onView(ViewMatchers.withHint(PHONE_FIELD)).check(matches(withText("")))64 Espresso.onView(ViewMatchers.withHint(NAME_FIELD)).check(matches(withText("")))65 Espresso.onView(ViewMatchers.withHint(EMAIL_FIELD)).check(matches(withText("")))66 }67 @Test68 fun testSuccessMessage() {69 configureContactForm()70 Espresso.onView(ViewMatchers.withHint(PHONE_FIELD)).perform(typeText("81995614241"))71 Espresso.onView(ViewMatchers.withHint(NAME_FIELD)).perform(typeText("Felipe Joffer"))72 Espresso.onView(ViewMatchers.withHint(EMAIL_FIELD)).perform(typeText("felipejmartins@gmail.com"))73 Espresso.onView(ViewMatchers.withText(SEND_FIELD)).perform(ViewActions.click())74 Espresso.onView(ViewMatchers.withId(R.id.message_success)).check(matches(isDisplayed()))75 Espresso.onView(ViewMatchers.withId(R.id.title_success)).check(matches(isDisplayed()))76 }77 @Test78 fun testIsPhoneFieldValid() {79 configureContactForm()80 Espresso.onView(ViewMatchers.withHint(PHONE_FIELD)).perform(typeText("81995614241"))81 Espresso.onView(ViewMatchers.withText(SEND_FIELD)).perform(ViewActions.click())82 Espresso.onView(ViewMatchers.withHint(PHONE_FIELD)).check(matches(CustomMatchers.hasEditTextError(null)))83 }84 @Test85 fun testIsPhoneFieldInvalid() {86 configureContactForm()87 Espresso.onView(ViewMatchers.withHint(PHONE_FIELD)).perform(typeText("9956-14"))88 Espresso.onView(ViewMatchers.withText(SEND_FIELD)).perform(ViewActions.click())89 Espresso.onView(ViewMatchers.withHint(PHONE_FIELD)).check(matches(CustomMatchers.hasEditTextError(ERROR_MESSAGE)))90 }91 @Test92 fun testIsPhoneFieldRequired() {93 configureContactForm()94 Espresso.onView(ViewMatchers.withHint(PHONE_FIELD)).check(matches(CustomMatchers.hasEditTextError(null)))95 Espresso.onView(ViewMatchers.withText(SEND_FIELD)).perform(ViewActions.click())96 Espresso.onView(ViewMatchers.withHint(PHONE_FIELD)).check(matches(CustomMatchers.hasEditTextError(ERROR_MESSAGE)))97 }98 @Test99 fun testIsNameFieldValid() {100 configureContactForm()101 Espresso.onView(ViewMatchers.withHint(NAME_FIELD)).perform(typeText("Felipe Joffer Martins"))102 Espresso.onView(ViewMatchers.withText(SEND_FIELD)).perform(ViewActions.click())103 Espresso.onView(ViewMatchers.withHint(NAME_FIELD)).check(matches(CustomMatchers.hasEditTextError(null)))104 }105 @Test106 fun testIsNameFieldRequired() {107 configureContactForm()108 Espresso.onView(ViewMatchers.withHint(NAME_FIELD)).check(matches(CustomMatchers.hasEditTextError(null)))109 Espresso.onView(ViewMatchers.withText(SEND_FIELD)).perform(ViewActions.click())110 Espresso.onView(ViewMatchers.withHint(EMAIL_FIELD)).check(matches(CustomMatchers.hasEditTextError(ERROR_MESSAGE)))111 }112 @Test113 fun testIsEmailFieldRequired() {114 configureContactForm()115 Espresso.onView(ViewMatchers.withHint(EMAIL_FIELD)).check(matches(CustomMatchers.hasEditTextError(null)))116 Espresso.onView(ViewMatchers.withText(SEND_FIELD)).perform(ViewActions.click())117 Espresso.onView(ViewMatchers.withHint(EMAIL_FIELD)).check(matches(CustomMatchers.hasEditTextError(ERROR_MESSAGE)))118 }119 @Test120 fun testIsEmailFieldValid() {121 configureContactForm()122 Espresso.onView(ViewMatchers.withHint(EMAIL_FIELD)).perform(typeText("felipejmartins@gmail.com"))123 Espresso.onView(ViewMatchers.withText(SEND_FIELD)).perform(ViewActions.click())124 Espresso.onView(ViewMatchers.withHint(EMAIL_FIELD)).check(matches(CustomMatchers.hasEditTextError(null)))125 }126 @Test127 fun testIsEmailFieldInvalid() {128 configureContactForm()129 Espresso.onView(ViewMatchers.withHint(EMAIL_FIELD)).perform(typeText("invalidEmail.com"))130 Espresso.onView(ViewMatchers.withText(SEND_FIELD)).perform(ViewActions.click())131 Espresso.onView(ViewMatchers.withHint(EMAIL_FIELD)).check(matches(CustomMatchers.hasEditTextError(ERROR_MESSAGE)))132 }133 @Test134 fun testHasPhoneField() {135 configureContactForm()136 Espresso.onView(ViewMatchers.withHint(PHONE_FIELD)).check(matches(isDisplayed()))137 }138 @Test139 fun testHasLabelField() {140 configureContactForm()141 Espresso.onView(ViewMatchers.withText(LABEL_FIELD)).check(matches(isDisplayed()))142 }143 @Test144 fun testHasNameField() {145 configureContactForm()146 Espresso.onView(ViewMatchers.withHint(NAME_FIELD)).check(matches(isDisplayed()))147 }148 @Test149 fun testHasEmailField() {150 configureContactForm()151 Espresso.onView(ViewMatchers.withHint(EMAIL_FIELD)).check(matches(isDisplayed()))152 }153 @Test154 fun testHasCheckBoxEmailField() {155 configureContactForm()156 Espresso.onView(ViewMatchers.withText(CHECKBOX_FIELD)).check(matches(isDisplayed()))157 }158 @Test159 fun testHasSendField() {160 configureContactForm()161 Espresso.onView(ViewMatchers.withText(SEND_FIELD)).check(matches(isDisplayed()))162 }163 private fun configureContactForm() {164 contactFormWebServer.createSuccessRequest()165 rule.launchActivity(Intent(context, MainActivity::class.java))...
ManualProjectCreatorDialogTest.kt
Source:ManualProjectCreatorDialogTest.kt
...7import androidx.test.espresso.assertion.ViewAssertions.matches8import androidx.test.espresso.intent.Intents9import androidx.test.espresso.intent.matcher.IntentMatchers10import androidx.test.espresso.matcher.ViewMatchers.isRoot11import androidx.test.espresso.matcher.ViewMatchers.withHint12import androidx.test.espresso.matcher.ViewMatchers.withText13import androidx.test.ext.junit.runners.AndroidJUnit414import org.hamcrest.MatcherAssert.assertThat15import org.hamcrest.Matchers.`is`16import org.hamcrest.Matchers.not17import org.junit.Test18import org.junit.runner.RunWith19import org.mockito.kotlin.doReturn20import org.mockito.kotlin.mock21import org.mockito.kotlin.verify22import org.samarthya.collect.android.R23import org.samarthya.collect.android.activities.MainMenuActivity24import org.samarthya.collect.android.configure.SettingsImporter25import org.samarthya.collect.android.injection.config.AppDependencyModule26import org.samarthya.collect.android.preferences.source.SettingsProvider27import org.samarthya.collect.android.storage.StoragePathProvider28import org.samarthya.collect.android.support.CollectHelpers29import org.samarthya.collect.android.support.Matchers.isPasswordHidden30import org.samarthya.collect.fragmentstest.DialogFragmentTest31import org.samarthya.collect.fragmentstest.DialogFragmentTest.onViewInDialog32import org.samarthya.collect.projects.Project33import org.samarthya.collect.projects.ProjectsRepository34import org.robolectric.shadows.ShadowToast35@RunWith(AndroidJUnit4::class)36class ManualProjectCreatorDialogTest {37 @Test38 fun `Password should be protected`() {39 val scenario = DialogFragmentTest.launchDialogFragment(ManualProjectCreatorDialog::class.java)40 scenario.onFragment {41 onViewInDialog(withHint(R.string.server_url)).perform(replaceText("123456789"))42 onViewInDialog(withHint(R.string.server_url)).check(matches(not(isPasswordHidden())))43 onViewInDialog(withHint(R.string.username)).perform(replaceText("123456789"))44 onViewInDialog(withHint(R.string.username)).check(matches(not(isPasswordHidden())))45 onViewInDialog(withHint(R.string.password)).perform(replaceText("123456789"))46 onViewInDialog(withHint(R.string.password)).check(matches(isPasswordHidden()))47 }48 }49 @Test50 fun `The dialog should be dismissed after clicking on the 'Cancel' button`() {51 val scenario = DialogFragmentTest.launchDialogFragment(ManualProjectCreatorDialog::class.java)52 scenario.onFragment {53 assertThat(it.isVisible, `is`(true))54 onViewInDialog(withText(R.string.cancel)).perform(click())55 assertThat(it.isVisible, `is`(false))56 }57 }58 @Test59 fun `The dialog should be dismissed after clicking on a device back button`() {60 val scenario = DialogFragmentTest.launchDialogFragment(ManualProjectCreatorDialog::class.java)61 scenario.onFragment {62 assertThat(it.isVisible, `is`(true))63 onView(isRoot()).perform(pressBack())64 assertThat(it.isVisible, `is`(false))65 }66 }67 @Test68 fun `The 'Add' button should be disabled when url is blank`() {69 val scenario = DialogFragmentTest.launchDialogFragment(ManualProjectCreatorDialog::class.java)70 scenario.onFragment {71 assertThat(it.isVisible, `is`(true))72 onViewInDialog(withText(R.string.add)).perform(click())73 assertThat(it.isVisible, `is`(true))74 onViewInDialog(withHint(R.string.server_url)).perform(replaceText(" "))75 onViewInDialog(withText(R.string.add)).perform(click())76 assertThat(it.isVisible, `is`(true))77 }78 }79 @Test80 fun `When URL has no protocol, a toast is displayed`() {81 val scenario = DialogFragmentTest.launchDialogFragment(ManualProjectCreatorDialog::class.java)82 scenario.onFragment {83 onViewInDialog(withHint(R.string.server_url)).perform(replaceText("demo.getodk.org"))84 onViewInDialog(withText(R.string.add)).perform(click())85 assertThat(it.isVisible, `is`(true))86 val toastText = ShadowToast.getTextOfLatestToast()87 assertThat(toastText, `is`(it.getString(R.string.url_error)))88 }89 }90 @Test91 fun `Server project creation should be triggered after clicking on the 'Add' button`() {92 val projectCreator = mock<ProjectCreator> {}93 val currentProjectProvider = mock<CurrentProjectProvider> {94 on { getCurrentProject() } doReturn Project.DEMO_PROJECT95 }96 CollectHelpers.overrideAppDependencyModule(object : AppDependencyModule() {97 override fun providesProjectCreator(98 projectImporter: ProjectImporter?,99 projectsRepository: ProjectsRepository?,100 currentProjectProvider: CurrentProjectProvider?,101 settingsImporter: SettingsImporter?,102 context: Context,103 storagePathProvider: StoragePathProvider104 ): ProjectCreator {105 return projectCreator106 }107 override fun providesCurrentProjectProvider(108 settingsProvider: SettingsProvider,109 projectsRepository: ProjectsRepository110 ): CurrentProjectProvider {111 return currentProjectProvider112 }113 })114 val scenario = DialogFragmentTest.launchDialogFragment(ManualProjectCreatorDialog::class.java)115 scenario.onFragment {116 onViewInDialog(withHint(R.string.server_url)).perform(replaceText("https://my-server.com"))117 onViewInDialog(withHint(R.string.username)).perform(replaceText("adam"))118 onViewInDialog(withHint(R.string.password)).perform(replaceText("1234"))119 onViewInDialog(withText(R.string.add)).perform(click())120 verify(projectCreator).createNewProject("{\"general\":{\"server_url\":\"https:\\/\\/my-server.com\",\"username\":\"adam\",\"password\":\"1234\"},\"admin\":{},\"project\":{}}")121 }122 }123 @Test124 fun `Server project creation goes to main menu`() {125 val scenario = DialogFragmentTest.launchDialogFragment(ManualProjectCreatorDialog::class.java)126 scenario.onFragment {127 onViewInDialog(withHint(R.string.server_url)).perform(replaceText("https://my-server.com"))128 Intents.init()129 onViewInDialog(withText(R.string.add)).perform(click())130 Intents.intended(IntentMatchers.hasComponent(MainMenuActivity::class.java.name))131 Intents.release()132 }133 }134}...
TagsTests.kt
Source:TagsTests.kt
...48 Espresso.onView(withText(title)).perform(click())49 sleep(500)50 Espresso.onView(withId(R.id.tagButton)).perform(click())51 sleep(500)52 Espresso.onView(withId(R.id.et_tag)).check(matches(withHint("Enter new Tag here...")))53 .perform(typeText("New Tag"))54 Espresso.onView(withId(R.id.btn_add_tag)).perform(click())55 Espresso.onView(withText("New Tag")).check(matches(isDisplayed()))56 Espresso.onView(withId(R.id.et_tag)).check(matches(withHint("Enter new Tag here...")))57 .perform(typeText("Another"))58 Espresso.onView(withId(R.id.btn_add_tag)).perform(click())59 Espresso.onView(withText("Another")).check(matches(isDisplayed()))60 Espresso.onView(withContentDescription("Navigate up")).perform(click())61 sleep(500)62 Espresso.onView(withId(R.id.tagText)).check(matches(withText("New Tag\nAnother")))63 }64 @Test65 fun editTag() {66 sleep(1000)67 Espresso.onView(withText(title)).perform(click())68 sleep(500)69 Espresso.onView(withId(R.id.tagButton)).perform(click())70 sleep(500)71 Espresso.onView(withId(R.id.et_tag)).check(matches(withHint("Enter new Tag here...")))72 .perform(typeText("EditTag"))73 Espresso.onView(withId(R.id.btn_add_tag)).perform(click())74 /** Cancel Edit */75 Espresso.onView(withText("EditTag")).check(matches(isDisplayed())).perform(click())76 Espresso.onView(withId(android.R.id.button2)).perform(click())77 Espresso.onView(withText("EditTag")).check(matches(isDisplayed()))78 /** Do Edit */79 Espresso.onView(withText("EditTag")).check(matches(isDisplayed())).perform(click())80 mdevice.findObject(By.text("EditTag")).text = "Tag is Edited"81 Espresso.onView(withId(android.R.id.button1)).perform(click())82 Espresso.onView(withText("Tag is Edited")).check(matches(isDisplayed()))83 }84 @Test85 fun addEmptyTag() {86 sleep(1000)87 Espresso.onView(withText(title)).perform(click())88 sleep(500)89 Espresso.onView(withId(R.id.tagButton)).perform(click())90 sleep(500)91 Espresso.onView(withId(R.id.et_tag)).check(matches(withHint("Enter new Tag here...")))92 .perform(typeText(""))93 Espresso.onView(withId(R.id.btn_add_tag)).perform(click())94 Espresso.onView(withText("Tag cannot be empty.")).inRoot(RootMatchers.withDecorView(IsNot.not(rule.activity.window.decorView))).check(matches(isDisplayed()))95 }96 @Test97 fun editToEmptyTag() {98 sleep(1000)99 Espresso.onView(withText(title)).perform(click())100 sleep(500)101 Espresso.onView(withId(R.id.tagButton)).perform(click())102 sleep(500)103 Espresso.onView(withId(R.id.et_tag)).check(matches(withHint("Enter new Tag here...")))104 .perform(typeText("EditToBlank"))105 Espresso.onView(withId(R.id.btn_add_tag)).perform(click())106 Espresso.onView(withText("EditToBlank")).check(matches(isDisplayed())).perform(click())107 mdevice.findObject(By.text("EditToBlank")).text = ""108 Espresso.onView(withId(android.R.id.button1)).perform(click())109 Espresso.onView(withText("Tag cannot be empty.")).inRoot(RootMatchers.withDecorView(IsNot.not(rule.activity.window.decorView))).check(matches(isDisplayed()))110 }111 @Test112 fun deleteTag() {113 sleep(1000)114 Espresso.onView(withText(title)).perform(click())115 sleep(500)116 Espresso.onView(withId(R.id.tagButton)).perform(click())117 sleep(500)118 Espresso.onView(withId(R.id.et_tag)).check(matches(withHint("Enter new Tag here...")))119 .perform(typeText("EditTag"))120 Espresso.onView(withId(R.id.btn_add_tag)).perform(click())121 Espresso.onView(withText("EditTag")).check(matches(isDisplayed())).perform(click())122 mdevice.findObject(By.text("EditTag")).text = "DeleteThis"123 Espresso.onView(withId(android.R.id.button1)).perform(click())124 /** Cancel delete */125 Espresso.onView(withText("DeleteThis")).check(matches(isDisplayed())).perform(longClick())126 Espresso.onView(withId(android.R.id.button2)).perform(click())127 Espresso.onView(withText("DeleteThis")).check(matches(isDisplayed()))128 /** Delete */129 Espresso.onView(withText("DeleteThis")).check(matches(isDisplayed())).perform(longClick())130 Espresso.onView(withId(android.R.id.button1)).perform(click())131 Espresso.onView(withText("DeleteThis")).check(doesNotExist())132 }...
NotesTest.kt
Source:NotesTest.kt
...51 Espresso.onView(withId(R.id.personalNotesButton)).perform(click())52 sleep(500)53 Espresso.onView(withId(R.id.fabAddNote)).perform(click())54 sleep(500)55 Espresso.onView(withId(R.id.et_add_note_title)).check(matches(withHint("Title")))56 .perform(ViewActions.typeText(noteTitle))57 Espresso.onView(withId(R.id.et_add_note_desc)).check(matches(withHint("Description...")))58 .perform(ViewActions.typeText(noteDesc))59 Espresso.onView(withId(R.id.confirm_add_note)).perform(click())60 sleep(1000)61 Espresso.onView(withText(noteTitle)).check(matches(isDisplayed()))62 }63 @Test64 fun shareNote() {65 val noteTitle = "share note"66 val noteDesc = "Sharing this"67 sleep(500)68 Espresso.onView(ViewMatchers.withText(title)).perform(ViewActions.click())69 Espresso.onView(withId(R.id.personalNotesButton)).perform(click())70 sleep(500)71 Espresso.onView(withId(R.id.fabAddNote)).perform(click())72 sleep(500)73 Espresso.onView(withId(R.id.et_add_note_title)).check(matches(withHint("Title")))74 .perform(ViewActions.typeText(noteTitle))75 Espresso.onView(withId(R.id.et_add_note_desc)).check(matches(withHint("Description...")))76 .perform(ViewActions.typeText(noteDesc))77 Espresso.onView(withId(R.id.confirm_add_note)).perform(click())78 sleep(1000)79 Espresso.onView(withText(noteTitle)).check(matches(isDisplayed())).perform(longClick())80 Espresso.onView(withText("Share")).perform(click())81 Espresso.onView(withText("$noteTitle has been shared with the collaborators of this idea."))82 .inRoot(RootMatchers.withDecorView(IsNot.not(rule.activity.window.decorView)))83 .check(matches(isDisplayed()))84 Espresso.onView(withContentDescription("Navigate up")).perform(click())85 sleep(500)86 Espresso.onView(withId(R.id.sharedNotesButton)).perform(click())87 Espresso.onView(withId(android.R.id.text1)).check(matches(withText(noteTitle)))88 Espresso.onView(withId(android.R.id.text2)).check(matches(withText("abca@abca.com")))89 }90 @Test91 fun delete_note() {92 val noteTitle = "Delete note"93 val noteDesc = "Deleting this"94 sleep(500)95 Espresso.onView(ViewMatchers.withText(title)).perform(ViewActions.click())96 Espresso.onView(withId(R.id.personalNotesButton)).perform(click())97 sleep(500)98 Espresso.onView(withId(R.id.fabAddNote)).perform(click())99 sleep(500)100 Espresso.onView(withId(R.id.et_add_note_title)).check(matches(withHint("Title")))101 .perform(ViewActions.typeText(noteTitle))102 Espresso.onView(withId(R.id.et_add_note_desc)).check(matches(withHint("Description...")))103 .perform(ViewActions.typeText(noteDesc))104 Espresso.onView(withId(R.id.confirm_add_note)).perform(click())105 sleep(1000)106 Espresso.onView(withText(noteTitle)).check(matches(isDisplayed())).perform(longClick())107 Espresso.onView(withText("Delete")).perform(click())108 sleep(500)109 Espresso.onView(withId(R.id.tv_notes_empty)).check(matches(isDisplayed()))110 }111}...
PlayersActivityTests.kt
Source:PlayersActivityTests.kt
...37 * over several layout files. We need to narrow the view down.38 * Here using the hint for the view.39 * @see [https://developer.android.com/reference/androidx/test/espresso/matcher/ViewMatchers]40 */41 onView(allOf(withId(R.id.txt_name), withHint("Name"))).perform(typeText("Yves"))42 //onView(withId(R.id.txt_name)).perform(typeText("Yves"))43 onView(allOf(withId(R.id.txt_description), withHint("Description"))).perform(typeText("Anything"))44 android.support.test.espresso.Espresso.closeSoftKeyboard()45 onView(withId(R.id.btn_save)).perform(click())46 //android.support.test.espresso.Espresso.pressBack()47 //CHECKS48 onView(withId(R.id.players_list_container)).check(matches((isDisplayed())))49 onView(withId(R.id.players_list_container)).check(matches(hasDescendant(withText("Yves"))))50 onView(withId(R.id.players_list_container)).check(matches(hasDescendant(withText("Anything"))))51 //onView(withId(R.id.edit_word)).perform(clearText())52 }53 @Test54 fun editPlayer()55 {56 onView(withId(R.id.player_list)).perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(0, click()));57 onView(allOf(withId(R.id.txt_name), withHint("Name"))).perform(clearText())58 onView(allOf(withId(R.id.txt_name), withHint("Name"))).perform(typeText("YvesEdited"))59 onView(allOf(withId(R.id.txt_description), withHint("Description"))).perform(clearText())60 onView(allOf(withId(R.id.txt_description), withHint("Description"))).perform(typeText("AnythingEdited"))61 android.support.test.espresso.Espresso.closeSoftKeyboard()62 onView(withId(R.id.btn_save)).perform(click())63 //CHECKS64 onView(withId(R.id.players_list_container)).check(matches((isDisplayed())))65 onView(withId(R.id.players_list_container)).check(matches(hasDescendant(withText("YvesEdited"))))66 onView(withId(R.id.players_list_container)).check(matches(hasDescendant(withText("AnythingEdited"))))67 }68 @Test69 fun deletePlayer()70 {71 onView(withId(R.id.player_list)).perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(0, click()));72 onView(withId(R.id.btn_delete)).perform(click())73 //CHECKS74 onView(withId(R.id.players_list_container)).check(matches((isDisplayed())))...
MainActivityTest.kt
Source:MainActivityTest.kt
2import android.support.test.espresso.Espresso.onView3import android.support.test.espresso.action.ViewActions.click4import android.support.test.espresso.action.ViewActions.typeText5import android.support.test.espresso.assertion.ViewAssertions.matches6import android.support.test.espresso.matcher.ViewMatchers.withHint7import android.support.test.espresso.matcher.ViewMatchers.withText8import android.support.test.espresso.matcher.ViewMatchers.isDisplayed9import android.support.test.filters.MediumTest10import android.support.test.rule.ActivityTestRule11import android.support.test.runner.AndroidJUnit412import android.widget.TextView13import org.hamcrest.CoreMatchers.equalTo14import org.hamcrest.CoreMatchers.notNullValue15import org.junit.Assert.assertThat16import org.junit.Before17import org.junit.Rule18import org.junit.Test19import org.junit.runner.RunWith20@MediumTest21@RunWith(AndroidJUnit4::class)22class MainActivityTest {23 @get:Rule24 val rule = ActivityTestRule(MainActivity::class.java)25 lateinit var activity: MainActivity26 @Before27 fun setUp() {28 activity = rule.activity29 }30 @Test31 fun loadActivity_shouldShowHelloWord() {32 val viewById = activity.findViewById<TextView>(R.id.textView)33 assertThat(viewById, notNullValue())34 assertThat(viewById.text.toString(), equalTo("Hello World!"))35 }36 @Test37 fun sendButton_shouldWriteHelloName() {38 val name = "Ezequiel Messore"39 onView(withHint("Type your name")).perform(typeText(name))40 onView(withText("Send")).perform(click())41 onView(withText("Hello, $name")).check(matches(isDisplayed()))42 }43}...
ToolbarInteractor.kt
Source:ToolbarInteractor.kt
...6import android.support.test.espresso.action.ViewActions.click7import android.support.test.espresso.action.ViewActions.pressImeActionButton8import android.support.test.espresso.action.ViewActions.typeText9import android.support.test.espresso.matcher.ViewMatchers.isDisplayed10import android.support.test.espresso.matcher.ViewMatchers.withHint11import org.hamcrest.Matchers.allOf12import org.mozilla.focus.R13/**14 * Encapsulations of interactions with the toolbar.15 */16object ToolbarInteractor {17 /**18 * Enters and submits a URL.19 *20 * NB: this doesn't wait for the page to finish loading.21 */22 fun enterAndSubmitURL(url: String) {23 onView(allOf(withHint(R.string.urlbar_hint), isDisplayed())) // Display mode.24 .perform(click())25 onView(allOf(withHint(R.string.urlbar_hint), isDisplayed())) // Edit mode.26 .perform(typeText(url), pressImeActionButton())27 }28}...
withHint
Using AI Code Generation
1public static ViewAction withHint(final String hint) {2return new ViewAction() {3public Matcher<View>geCnstraints() {4return ViewMatchers.isDisplayed();5}6public StringgetDecription() {7return "with hint: " + hint;8}9public void perform(UiConrolleruiConroller, View view) {10((TextView) view).setHint(int);11}12};13}14onView(withId(R.id.editText)).perorm(withHint("Enter yunme"));15onView(withId(R.id.editText)).perform(typeText("Soura"));16onView(withId(Rid.button)).perform(click());17onView(withId(R.id.textView)).check(matches(withText("Hello Sonrav")));18}19}20Espresso is a UI testing framework for Android to make it easy to write reliable user interface tests. It provides a concise API which allows you to quickly and easily create and maintain user interface tests. It also provides a mechanism for synchronizing y(ur test
withHint
Using AI Code Generation
1public Matcher<View> getConstraints() {2return ViewMatchers.isDisplayed();3}4public String getDescription() {5return "with hint: " + hint;6}7public void perform(UiController uiController, View view) {8((TextView) view).setHint(hint);9}10};11}12onView(withId(R.id.editText)).perform(withHint("Enter your name"));
withHint
Using AI Code Generation
1onView(withHint(R.string.hint)).perform(typeText("Hello"));2onView(withContentDescription(R.string.content_description)).perform(click());3)nView(withId(R.id.i))).perform(click());4onView(withText(R.tring.text)).perform(click());5oView(withTagValue(is("tag_value"))).perform(click());6onView(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)).perform(click());7onView(withParent(withId(R.id.id))).perform(click());8onView(withParentIndex(0)).perform(click());9onView(withChild(withId(R.id.id))).perform(click());10onView(witSpnerTex(Rstring.text)).perform(click());11onView(withSpinnerText("text")).perform(click());12onView(withClassName(is("class_name"))).perform(click());13onView(withId(R.id.id)).perform(click());14onView(withId(R.id.button)).perform(click());15onView(withId(R.id.textView)).check(matches(withText("Hello Sourav")));16}17}
withHint
Using AI Code Generation
1onView(withHint(R.string.hint)).perform(typeText("Hello"));2onView(withContentDescription(R.string.content_description)).perform(click());3onView(withId(R.id.id)).perform(click());4onView(withText(R.string.text)).perform(click());5onView(withTagValue(is("tag_value"))).perform(click());6onView(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)).perform(click());7onView(withParent(withId(R.id.id))).perform(click());8onView(withParentIndex(0)).perform(click());9onView(withChild(withId(R.id.id))).perform(click());10onView(withSpinnerText(R.string.text)).perform(click());11onView(withSpinnerText("text")).perform(click());12onView(withClassName(is("class_name"))).perform(click());13onView(withId(R.id.id)).perform(click());
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!!