Best Appium-espresso-driver code snippet using androidx.test.espresso.action.TypeTextAction
LoginRobot.kt
Source:LoginRobot.kt
1package org.dhis2.usescases.login2import androidx.test.espresso.Espresso.onView3import androidx.test.espresso.action.TypeTextAction4import androidx.test.espresso.action.ViewActions5import androidx.test.espresso.action.ViewActions.clearText6import androidx.test.espresso.action.ViewActions.click7import androidx.test.espresso.assertion.ViewAssertions.matches8import androidx.test.espresso.intent.Intents9import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent10import androidx.test.espresso.intent.matcher.IntentMatchers.hasExtra11import androidx.test.espresso.matcher.ViewMatchers.isDisplayed12import androidx.test.espresso.matcher.ViewMatchers.isEnabled13import androidx.test.espresso.matcher.ViewMatchers.withId14import androidx.test.espresso.matcher.ViewMatchers.withText15import org.dhis2.R16import org.dhis2.common.BaseRobot17import org.dhis2.common.viewactions.ClickDrawableAction18import org.dhis2.common.viewactions.clickClickableSpan19import org.dhis2.usescases.BaseTest20import org.dhis2.usescases.about.PolicyView21import org.dhis2.usescases.qrScanner.ScanActivity22import org.dhis2.utils.WebViewActivity23import org.hamcrest.CoreMatchers24import org.hamcrest.CoreMatchers.containsString25import org.hamcrest.CoreMatchers.not26import org.hamcrest.Matchers.isEmptyString27fun loginRobot(loginBody: LoginRobot.() -> Unit) {28 LoginRobot().apply {29 loginBody()30 }31}32class LoginRobot : BaseRobot() {33 fun typeServer(server: String) {34 onView(withId(R.id.server_url_edit)).perform(TypeTextAction(server))35 closeKeyboard()36 }37 fun clearServerField() {38 onView(withId(R.id.server_url_edit)).perform(clearText())39 }40 fun typeUsername(username: String) {41 onView(withId(R.id.user_name_edit)).perform(TypeTextAction(username))42 closeKeyboard()43 }44 fun clearUsernameField() {45 onView(withId(R.id.clearUserNameButton)).perform(click())46 }47 fun typePassword(password: String) {48 onView(withId(R.id.user_pass_edit)).perform(TypeTextAction(password))49 closeKeyboard()50 }51 fun clearPasswordField() {52 onView(withId(R.id.clearPassButton)).perform(click())53 }54 fun clickLoginButton() {55 onView(withId(R.id.login)).perform(click())56 }57 fun clickQRButton() {58 onView(withId(R.id.server_url_edit)).perform(ClickDrawableAction(ClickDrawableAction.RIGHT))59 }60 fun checkLoginButtonIsHidden() {61 onView(withId(R.id.login)).check(matches(not(isEnabled())))62 }...
FiltersRobot.kt
Source:FiltersRobot.kt
1package org.dhis2.common.filters2import androidx.test.espresso.Espresso.onData3import androidx.test.espresso.Espresso.onView4import androidx.test.espresso.action.TypeTextAction5import androidx.test.espresso.action.ViewActions.click6import androidx.test.espresso.assertion.ViewAssertions.matches7import androidx.test.espresso.contrib.PickerActions8import androidx.test.espresso.contrib.RecyclerViewActions9import androidx.test.espresso.contrib.RecyclerViewActions.actionOnItem10import androidx.test.espresso.matcher.ViewMatchers11import androidx.test.espresso.matcher.ViewMatchers.hasDescendant12import androidx.test.espresso.matcher.ViewMatchers.withId13import androidx.test.espresso.matcher.ViewMatchers.withParent14import androidx.test.espresso.matcher.ViewMatchers.withText15import org.dhis2.R16import org.dhis2.common.BaseRobot17import org.dhis2.common.matchers.DatePickerMatchers.Companion.matchesDate18import org.dhis2.common.matchers.RecyclerviewMatchers.Companion.hasItem19import org.dhis2.usescases.orgunitselector.OrgUnitSelectorHolder20import org.dhis2.utils.filters.FilterHolder21import org.hamcrest.Matchers.allOf22fun filterRobotCommon(robotBody: FiltersRobot.() -> Unit) {23 FiltersRobot().apply {24 robotBody()25 }26}27class FiltersRobot : BaseRobot() {28 fun openFilterAtPosition(position: Int) {29 onView(withId(R.id.filterRecycler)).perform(30 RecyclerViewActions.actionOnItemAtPosition<FilterHolder>(position, click())31 )32 }33 34 fun clickOnFromToDateOption() {35 onView(withId(R.id.fromTo)).perform(click())36 }37 fun clickOnOrgUnitTree() {38 onView(withId(R.id.ouTreeButton)).perform(click())39 }40 fun returnToSearch() {41 onView(withId(R.id.menu)).perform(click())42 }43 fun selectDate(year: Int, monthOfYear: Int, dayOfMonth: Int) {44 onView(withId(R.id.datePicker)).perform(45 PickerActions.setDate(year, monthOfYear, dayOfMonth)46 )47 }48 fun typeOrgUnit(orgUnitName: String) {49 onView(withId(R.id.orgUnitSearchEditText)).perform(TypeTextAction(orgUnitName))50 }51 fun clickAddOrgUnit() {52 onView(withId(R.id.addButton)).perform(click())53 }54 fun selectTreeOrgUnit(orgUnitName: String) {55 onView(withId(R.id.orgUnitRecycler))56 .perform(RecyclerViewActions.actionOnItemAtPosition<OrgUnitSelectorHolder>(0, click()))57 onView(allOf(withId(R.id.checkBox), ViewMatchers.hasSibling(withText(orgUnitName))))58 .perform(click())59 }60 fun selectNotSyncedState() {61 onView(withId(R.id.stateNotSynced)).perform(click())62 }63 fun acceptDateSelected(){...
NoteRobot.kt
Source:NoteRobot.kt
1package org.dhis2.usescases.teidashboard.robot2import androidx.test.espresso.Espresso.onView3import androidx.test.espresso.action.TypeTextAction4import androidx.test.espresso.action.ViewActions.click5import androidx.test.espresso.assertion.ViewAssertions.matches6import androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition7import androidx.test.espresso.matcher.ViewMatchers.hasDescendant8import androidx.test.espresso.matcher.ViewMatchers.isDisplayed9import androidx.test.espresso.matcher.ViewMatchers.withId10import androidx.test.espresso.matcher.ViewMatchers.withText11import org.dhis2.R12import org.dhis2.common.BaseRobot13import org.dhis2.common.matchers.RecyclerviewMatchers.Companion.atPosition14import org.dhis2.common.matchers.RecyclerviewMatchers.Companion.isNotEmpty15import org.dhis2.usescases.notes.NotesViewHolder16import org.hamcrest.CoreMatchers.allOf17import org.hamcrest.CoreMatchers.not18fun noteRobot(noteRobot: NoteRobot.() -> Unit) {19 NoteRobot().apply {20 noteRobot()21 }22}23class NoteRobot : BaseRobot() {24 fun clickOnFabAddNewNote() {25 onView(withId(R.id.addNoteButton)).check(matches(isDisplayed())).perform(click())26 }27 fun clickOnNoteWithPosition(position: Int) {28 onView(withId(R.id.notes_recycler))29 .perform(actionOnItemAtPosition<NotesViewHolder>(position, click()))30 }31 fun typeNote(text: String) {32 onView(withId(R.id.noteText)).perform(TypeTextAction(text))33 closeKeyboard()34 }35 fun clickOnSaveButton() {36 onView(withId(R.id.saveButton))37 .perform(click())38 }39 fun clickYesOnAlertDialog() {40 onView(withText(R.string.yes))41 .perform(click())42 }43 fun checkNoteWasNotCreated(text: String) {44 onView(withId(R.id.notes_recycler)).check(45 matches(46 not(...
LoginActivityTest.kt
Source:LoginActivityTest.kt
1package com.example.phonebook.login.ui2import android.content.Intent3import androidx.test.espresso.Espresso.onView4import androidx.test.espresso.action.TypeTextAction5import androidx.test.espresso.action.ViewActions.click6import androidx.test.espresso.action.ViewActions.closeSoftKeyboard7import androidx.test.espresso.intent.Intents8import androidx.test.espresso.intent.Intents.intended9import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent10import androidx.test.espresso.matcher.ViewMatchers.withId11import androidx.test.ext.junit.runners.AndroidJUnit412import androidx.test.rule.ActivityTestRule13import com.example.phonebook.R14import com.example.phonebook.main.ui.MainActivity15import com.example.phonebook.main.ui.getCredentials16import org.junit.After17import org.junit.Before18import org.junit.Rule19import org.junit.Test20import org.junit.runner.RunWith21@RunWith(AndroidJUnit4::class)22class LoginActivityTest {23 /*24 * This test is just to help development & confirm navigation to new activity occurs upon login,25 * & ensure mainActivity is initialising correctly26 *27 * APi_key will expire eventually, then test will fail28 * */29 @get:Rule30 val activityRule = ActivityTestRule(LoginActivity::class.java)31 lateinit var username: String32 lateinit var password: String33 @Before34 @Throws(Exception::class)35 fun setUp() {36 username = activityRule.activity.getCredentials().username37 password = activityRule.activity.getCredentials().apiKey38 Intents.init()39 }40 @After41 @Throws(java.lang.Exception::class)42 fun tearDown() {43 Intents.release()44 }45 @Test46 fun loginTest() {47 onView(withId(R.id.username))48 .perform(TypeTextAction(username))49 onView(withId(R.id.password))50 .perform(TypeTextAction(password))51 .perform(closeSoftKeyboard())52 Thread.sleep(50)53 onView(withId(R.id.login))54 .perform(click())55 activityRule.launchActivity(Intent())56 intended(hasComponent(MainActivity::class.java.getName()))57 }58}...
MainActivityTest.kt
Source:MainActivityTest.kt
2import android.app.Activity3import android.app.LauncherActivity4import android.content.Intent5import androidx.test.espresso.Espresso.onView6import androidx.test.espresso.action.TypeTextAction7import androidx.test.espresso.action.ViewActions.click8import androidx.test.espresso.action.ViewActions.typeText9import androidx.test.espresso.assertion.ViewAssertions.matches10import androidx.test.espresso.matcher.ViewMatchers.isDisplayed11import androidx.test.espresso.matcher.ViewMatchers.withId12import androidx.test.ext.junit.rules.ActivityScenarioRule13import androidx.test.rule.ActivityTestRule14import com.shamanth.twsearchclient.R15import junit.framework.TestCase16import org.hamcrest.CoreMatchers.not17import org.junit.Rule18import org.junit.Test19class MainActivityTest{20 @get:Rule...
TypeTextAndEnableAction.kt
Source:TypeTextAndEnableAction.kt
2import android.view.View3import android.widget.TextView4import androidx.test.espresso.UiController5import androidx.test.espresso.ViewAction6import androidx.test.espresso.action.TypeTextAction7import androidx.test.espresso.matcher.ViewMatchers8import androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom9import androidx.test.espresso.matcher.ViewMatchers.supportsInputMethods10import org.hamcrest.Matcher11import org.hamcrest.Matchers12import java.util.*13class TypeTextAndEnableAction(val text: String?) : ViewAction {14 private val originalTypeTextAction: TypeTextAction = TypeTextAction(text)15 override fun getConstraints(): Matcher<View> {16 var matchers = Matchers.allOf(ViewMatchers.isDisplayed())17 // SearchView does not support input methods itself (rather it delegates to an internal text18 // view for input).19 return Matchers.allOf(20 matchers, Matchers.anyOf(21 supportsInputMethods(), isAssignableFrom(22 TextView::class.java23 )24 )25 )26 }27 override fun getDescription() = String.format(Locale.ROOT, "enable and type text(%s)", text)28 override fun perform(uiController: UiController, view: View) {29 if (!view.isEnabled) {30 view.isEnabled = true31 }32 originalTypeTextAction.perform(uiController, view)33 }34}...
RepositoryFragmentUiTest.kt
Source:RepositoryFragmentUiTest.kt
1package com.bottlerocketstudios.brarchitecture.ui.repository2import androidx.fragment.app.testing.launchFragmentInContainer3import androidx.test.espresso.Espresso.onView4import androidx.test.espresso.action.TypeTextAction5import androidx.test.espresso.assertion.ViewAssertions.matches6import androidx.test.espresso.matcher.ViewMatchers.isEnabled7import androidx.test.espresso.matcher.ViewMatchers.withId8import androidx.test.ext.junit.runners.AndroidJUnit49import com.bottlerocketstudios.brarchitecture.R10import com.bottlerocketstudios.brarchitecture.ui.auth.LoginFragment11import org.hamcrest.Matchers.not12import org.junit.Test13import org.junit.runner.RunWith14@RunWith(AndroidJUnit4::class)15class RepositoryFragmentUiTest {16 @Test17 fun launchAFragment() {18 val scenario = launchFragmentInContainer<LoginFragment>(themeResId = R.style.AppTheme)19 onView(withId(R.id.login)).check(matches(not(isEnabled())))20 onView(withId(R.id.id)).perform(TypeTextAction("test@example.com"))21 onView(withId(R.id.password)).perform(TypeTextAction("password1"))22 onView(withId(R.id.login)).check(matches(isEnabled()))23 }24}...
EditTextExt.kt
Source:EditTextExt.kt
1package zone.bokmark.espressokit2import androidx.test.espresso.ViewInteraction3import androidx.test.espresso.action.ReplaceTextAction4import androidx.test.espresso.action.TypeTextAction5/**6 * è¾å
¥ææ¬ã7 * éè¿è°ç¨[EditText]ç[setText]æ¹æ³è®¾ç½®ææ¬ãæ¯æä¸æ8 */9fun ViewInteraction.replaceText(text: String) {10 perform(ReplaceTextAction(text))11}12/**13 * è¾å
¥ææ¬14 * éè¿æ¨¡æç¹å»é®ç模æè¾å
¥ï¼ä¸æ¯æä¸æã15 */16fun ViewInteraction.typeText(text: String) {17 perform(TypeTextAction(text))18}...
TypeTextAction
Using AI Code Generation
1TypeTextAction typeTextAction = new TypeTextAction("Hello World");2typeTextAction.perform(UiController uiController, View view);3typeTextAction.getDescription();4typeTextAction.getConstraints();5typeTextAction.toString();6typeTextAction.hashCode();7typeTextAction.equals(Object obj);8typeTextAction.getConstraints();9typeTextAction.toString();10typeTextAction.hashCode();11typeTextAction.equals(Object obj);12typeTextAction.getConstraints();13typeTextAction.toString();14typeTextAction.hashCode();15typeTextAction.equals(Object obj);16typeTextAction.getConstraints();17typeTextAction.toString();18typeTextAction.hashCode();19typeTextAction.equals(Object obj);20typeTextAction.getConstraints();21typeTextAction.toString();22typeTextAction.hashCode();23typeTextAction.equals(Object obj);
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!!