Best Appium-espresso-driver code snippet using android.support.test.espresso.ViewInteraction.perform
EspressoDsl.kt
Source:EspressoDsl.kt
...28fun dataInstanceOf(clazz: Class<*>): DataInteraction = Espresso.onData(CoreMatchers.instanceOf(clazz))29/**30 * ViewActions31 */32fun ViewInteraction.click(): ViewInteraction = perform(ViewActions.click())33fun ViewInteraction.type(text: String): ViewInteraction = perform(ViewActions.typeText(text))34fun ViewInteraction.replace(text: String): ViewInteraction = perform(ViewActions.replaceText(text))35fun ViewInteraction.closeKeyboard(): ViewInteraction = perform(ViewActions.closeSoftKeyboard())36/**37 * ViewInteraction extensions38 */39fun ViewInteraction.checkHasChildByText(text: String): ViewInteraction =40 check(ViewAssertions.matches(ViewMatchers.withChild(ViewMatchers.withText(text))))41fun ViewInteraction.checkHasChildByText(id: Int): ViewInteraction =42 check(ViewAssertions.matches(ViewMatchers.withChild(ViewMatchers.withText(id))))43fun ViewInteraction.checkHasChildById(id: Int): ViewInteraction =44 check(ViewAssertions.matches(ViewMatchers.withChild(ViewMatchers.withId(id))))45fun ViewInteraction.checkDisplayed(): ViewInteraction =46 check(ViewAssertions.matches(ViewMatchers.isDisplayed()))47fun ViewInteraction.checkNotDisplayed(): ViewInteraction =48 check(ViewAssertions.matches(not(ViewMatchers.isDisplayed())))49fun ViewInteraction.checkDoesNotExist(): ViewInteraction =50 check(ViewAssertions.doesNotExist())51fun ViewInteraction.checkMatches(matcher: Matcher<View>): ViewInteraction =52 check(ViewAssertions.matches(matcher))53fun ViewInteraction.clickTodoCheckBoxWithTitle(text: String): ViewInteraction =54 perform(CustomRecyclerViewActions.ClickTodoCheckBoxWithTitleViewAction.clickTodoCheckBoxWithTitle(text))55fun ViewInteraction.scrollToLastItem(): ViewInteraction =56 perform(CustomRecyclerViewActions.ScrollToLastHolder.scrollToLastHolder())57fun ViewInteraction.pressEspressoBack() = Espresso.pressBack()58/**59 * Expand function for web view test case.60 * It contains a Thread.sleep() each time key event is sent.61 *62 * @param key - keycode from {@link KeyEvent}63 * @param milliseconds - milliseconds to sleep64 * @param count - amount of times {@link KeyEvent} should be executed65 */66fun ViewInteraction.sleepAndPressKey(key: Int, milliseconds: Long, count: Int = 1): ViewInteraction {67 for (i in 1..count) {68 /**69 * Having Thread.sleep() in tests is a bad practice.70 * Here we are using it just to solve specific issue and nothing more.71 */72 Thread.sleep(milliseconds)73 perform(pressKey(key))74 }75 return this76}77/**78 * Aggregated matchers79 */80fun ViewInteraction.allOf(vararg matcher: Matcher<View>): ViewInteraction {81 return check(ViewAssertions.matches(Matchers.allOf(matcher.asIterable())))82}83/**84 * DataInteraction extensions85 */86fun DataInteraction.click(): ViewInteraction = perform(ViewActions.click())87fun DataInteraction.checkDisplayed(): ViewInteraction =88 check(ViewAssertions.matches(ViewMatchers.isDisplayed()))89fun DataInteraction.checkWithText(text: String): ViewInteraction =90 check(ViewAssertions.matches(ViewMatchers.withText(text)))91fun DataInteraction.checkMatches(matcher: Matcher<View>): ViewInteraction =92 check(ViewAssertions.matches(matcher))93fun DataInteraction.childById(id: Int): DataInteraction = onChildView(withId(id))94fun DataInteraction.inAdapterById(id: Int): DataInteraction = inAdapterView(withId(id))95/**96 * RecyclerView actions97 */98fun ViewInteraction.actionAtPosition(position: Int,99 action: ViewAction): ViewInteraction =100 perform(actionOnItemAtPosition<RecyclerView.ViewHolder>(position, action))101fun ViewInteraction.scrollToHolder(matcher: Matcher<RecyclerView.ViewHolder>): ViewInteraction =102 perform(scrollToHolder<RecyclerView.ViewHolder>(matcher))103fun ViewInteraction.actionOnItem(matcher: Matcher<RecyclerView.ViewHolder>,104 action: ViewAction): ViewInteraction =105 perform(actionOnHolderItem<RecyclerView.ViewHolder>(matcher, action))106fun ViewInteraction.scrollToPosition(position: Int): ViewInteraction =107 perform(scrollToPosition<RecyclerView.ViewHolder>(position))108fun ViewInteraction.scrollTo(matcher: Matcher<View>): ViewInteraction =109 perform(RecyclerViewActions.scrollTo<RecyclerView.ViewHolder>(matcher))110/**111 * ViewMatchers112 */113fun parentWithId(id: Int): Matcher<View> = ViewMatchers.withParent(withId(id))114/**115 * Waiters116 */117private const val FOUR_SECONDS = 4000118fun ViewInteraction.wait(): ViewInteraction =119 ConditionWatchers.waitForElement(this, FOUR_SECONDS)120fun ViewInteraction.waitFullyVisible(): ViewInteraction =121 ConditionWatchers.waitForElementFullyVisible(this, FOUR_SECONDS)122fun ViewInteraction.waitForGone(): ViewInteraction =123 ConditionWatchers.waitForElementIsGone(this, FOUR_SECONDS)...
ViewInteractionExtensions.kt
Source:ViewInteractionExtensions.kt
...50 = check(ViewAssertions.matches(ViewMatchers.withContentDescription(stringId)))51fun ViewInteraction.assertHasChild(matcher: Matcher<View>): ViewInteraction52 = check(ViewAssertions.matches(ViewMatchers.withChild(matcher)))53// Extensions for ViewActions54fun ViewInteraction.typeText(arg0: String): ViewInteraction = perform(ViewActions.typeText(arg0))55fun ViewInteraction.pressKey(arg0: android.support.test.espresso.action.EspressoKey): ViewInteraction = perform(ViewActions.pressKey(arg0))56fun ViewInteraction.pressKey(arg0: Int): ViewInteraction = perform(ViewActions.pressKey(arg0))57fun ViewInteraction.swipeUp(): ViewInteraction = perform(ViewActions.swipeUp())58fun ViewInteraction.click(): ViewInteraction = perform(ViewActions.click())59fun ViewInteraction.click(arg0: android.support.test.espresso.ViewAction): ViewInteraction = perform(ViewActions.click(arg0))60fun ViewInteraction.actionWithAssertions(arg0: android.support.test.espresso.ViewAction): ViewInteraction = perform(ViewActions.actionWithAssertions(arg0))61fun ViewInteraction.clearText(): ViewInteraction = perform(ViewActions.clearText())62fun ViewInteraction.swipeLeft(): ViewInteraction = perform(ViewActions.swipeLeft())63fun ViewInteraction.swipeRight(): ViewInteraction = perform(ViewActions.swipeRight())64fun ViewInteraction.swipeDown(): ViewInteraction = perform(ViewActions.swipeDown())65fun ViewInteraction.closeSoftKeyboard(): ViewInteraction = perform(ViewActions.closeSoftKeyboard())66fun ViewInteraction.pressImeActionButton(): ViewInteraction = perform(ViewActions.pressImeActionButton())67fun ViewInteraction.pressBack(): ViewInteraction = perform(ViewActions.pressBack())68fun ViewInteraction.pressMenuKey(): ViewInteraction = perform(ViewActions.pressMenuKey())69fun ViewInteraction.doubleClick(): ViewInteraction = perform(ViewActions.doubleClick())70fun ViewInteraction.longClick(): ViewInteraction = perform(ViewActions.longClick())71fun ViewInteraction.scrollTo(): ViewInteraction = perform(ViewActions.scrollTo())72fun ViewInteraction.typeTextIntoFocusedView(arg0: String): ViewInteraction = perform(ViewActions.typeTextIntoFocusedView(arg0))73fun ViewInteraction.replaceText(arg0: String): ViewInteraction = perform(ViewActions.replaceText(arg0))74fun ViewInteraction.openLinkWithText(arg0: org.hamcrest.Matcher<String>): ViewInteraction = perform(ViewActions.openLinkWithText(arg0))75fun ViewInteraction.openLinkWithText(arg0: String): ViewInteraction = perform(ViewActions.openLinkWithText(arg0))76fun ViewInteraction.openLink(arg0: org.hamcrest.Matcher<String>, arg1: org.hamcrest.Matcher<android.net.Uri>): ViewInteraction = perform(ViewActions.openLink(arg0, arg1))77fun ViewInteraction.openLinkWithUri(arg0: org.hamcrest.Matcher<android.net.Uri>): ViewInteraction = perform(ViewActions.openLinkWithUri(arg0))78fun ViewInteraction.openLinkWithUri(arg0: String): ViewInteraction = perform(ViewActions.openLinkWithUri(arg0))79fun ViewInteraction.pageToItem(pageNumber: Int): ViewInteraction = perform(80 SetViewPagerCurrentItemAction(81 pageNumber82 )83)...
BaseTestRobot.kt
Source:BaseTestRobot.kt
...19import org.hamcrest.Matchers20import org.hamcrest.TypeSafeMatcher21open class BaseTestRobot {22 fun fillEditText(resId: Int, text: String): ViewInteraction =23 onView(withId(resId)).perform(ViewActions.replaceText(text), ViewActions.closeSoftKeyboard())24 fun clickButton(resId: Int): ViewInteraction = onView((withId(resId))).perform(ViewActions.click())25 fun openMenu(){26 openActionBarOverflowOrOptionsMenu(getInstrumentation().targetContext)27 }28 fun pressBack() {29 onView(isRoot()).perform(ViewActions.pressBack())30 }31 fun selectMenuItem(menuString: String){32 onView(withText(menuString))33 .perform(click())34 }35 fun checkMenuItemDoesntExist(menuString: String){36 onView(withText(menuString)).check(ViewAssertions.doesNotExist())37 }38 fun checkMenuItemExist(menuString: String){39 onView(withText(menuString)).check(matches(isDisplayed()))40 }41 fun textView(resId: Int): ViewInteraction = onView(withId(resId))42 fun matchText(viewInteraction: ViewInteraction, text: String): ViewInteraction = viewInteraction43 .check(ViewAssertions.matches(ViewMatchers.withText(text)))44 fun matchText(resId: Int, text: String): ViewInteraction = matchText(textView(resId), text)45 fun clickListItem(listRes: Int, position: Int) {46 onData(anything())47 .inAdapterView(allOf(withId(listRes)))48 .atPosition(position).perform(ViewActions.click())49 }50 fun checkSnackBarDisplayedByMessageId(@StringRes message: Int) {51 onView(allOf(withId(android.support.design.R.id.snackbar_text), withText(message)))52 .check(matches(withEffectiveVisibility(53 ViewMatchers.Visibility.VISIBLE54 )))55 }56 fun checkSnackBarDisplayedByMessageString(message: String) {57 onView(withText(message))58 .check(matches(withEffectiveVisibility(59 ViewMatchers.Visibility.VISIBLE60 )))61 }62 fun matchToolbarTitle(63 title: CharSequence): ViewInteraction {64 return onView(isAssignableFrom(Toolbar::class.java))65 .check(matches(withToolbarTitle(`is`(title))))66 }67 fun withToolbarTitle(68 textMatcher: Matcher<CharSequence>): Matcher<Any> {69 return object : BoundedMatcher<Any, Toolbar>(Toolbar::class.java!!) {70 public override fun matchesSafely(toolbar: Toolbar): Boolean {71 return textMatcher.matches(toolbar.title)72 }73 override fun describeTo(description: Description) {74 description.appendText("with toolbar title: ")75 textMatcher.describeTo(description)76 }77 }78 }79 fun pressHomeUp(){80 onView(81 Matchers.allOf(ViewMatchers.withContentDescription("Navigate up"),82 childAtPosition(83 Matchers.allOf(ViewMatchers.withId(R.id.main_tbar_toolbar),84 childAtPosition(85 ViewMatchers.withId(R.id.main_ll_container),86 0)),87 2),88 ViewMatchers.isDisplayed())).perform(ViewActions.click())89 }90 private fun childAtPosition(91 parentMatcher: Matcher<View>, position: Int): Matcher<View> {92 return object : TypeSafeMatcher<View>() {93 override fun describeTo(description: Description) {94 description.appendText("Child at position $position in parent ")95 parentMatcher.describeTo(description)96 }97 public override fun matchesSafely(view: View): Boolean {98 val parent = view.parent99 return parent is ViewGroup && parentMatcher.matches(parent)100 && view == parent.getChildAt(position)101 }102 }...
extensions.kt
Source:extensions.kt
...19fun text(@StringRes resource: Int): ViewInteraction = onView(withText(resource))20infix fun ViewInteraction.check(action: ViewAssertion): ViewInteraction = this.check(action)21infix fun ViewInteraction.hasHint(@StringRes string: Int): ViewInteraction = this check matches(withHint(string))22infix fun ViewInteraction.hasText(@StringRes string: Int): ViewInteraction = this check matches(withText(string))23infix fun Int.perform(action: ViewAction): ViewInteraction = onView(withId(this)).perform(action)24infix fun Int.check(action: ViewAssertion): ViewInteraction = onView(withId(this)).check(action)25infix fun Int.hasHint(@StringRes resource: Int): ViewInteraction = onView(withId(this)) hasHint resource26infix fun Int.hasText(@StringRes resource: Int): ViewInteraction = onView(withId(this)) hasText resource
BaseRobot.kt
Source:BaseRobot.kt
...14 /**15 * Generic editing of text16 */17 fun fillEditText(resId: Int, text: String): ViewInteraction =18 onView(withId(resId)).perform(ViewActions.replaceText(text), ViewActions.closeSoftKeyboard())19 fun clickButton(resId: Int): ViewInteraction = onView((withId(resId))).perform(ViewActions.click())20 fun clickText(resId: Int): ViewInteraction = onView((withId(resId))).perform(ViewActions.click())21 fun textView(resId: Int): ViewInteraction = onView(withId(resId))22 fun matchErrorText(viewInteraction: ViewInteraction, text: String): ViewInteraction = viewInteraction23 .check(matches(hasErrorText(text)))24 fun matchNoTErrorText(viewInteraction: ViewInteraction, text: String): ViewInteraction = viewInteraction25 .check(matches(IsNot(hasErrorText(text))))26}
Helper.kt
Source:Helper.kt
...13 val interaction: ViewInteraction = Espresso.onView(ViewMatchers.withId(layoutId))14}15class KViewActions(private val interaction: ViewInteraction) {16 fun clicOnItem(position: Int) {17 interaction.perform(RecyclerViewActions.18 actionOnItemAtPosition<RecyclerView.ViewHolder>(position, ViewActions.click()))19 }20}21class KViewAssertions(private val interaction: ViewInteraction) {22 fun hasText(text: String) {23 interaction.check(ViewAssertions.matches(ViewMatchers.withText(text)))24 }25}26infix fun KView.perform(func: KViewActions.() -> Unit) {27 func.invoke(KViewActions(interaction))28}29infix fun KView.check(func: KViewAssertions.() -> Unit) {30 func.invoke(KViewAssertions(interaction))31}...
EspressoExtensions.kt
Source:EspressoExtensions.kt
...12import org.hamcrest.Matcher13fun ViewInteraction.check(viewMatcher: Matcher<View>): ViewInteraction =14 check(matches(viewMatcher))15fun ViewInteraction.click(): ViewInteraction =16 perform(ViewActions.click())17fun ViewInteraction.checkText(text: String): ViewInteraction =18 check(withText(text))19fun ViewInteraction.enterText(text: String): ViewInteraction =20 perform(replaceText(text), closeSoftKeyboard())21fun ViewInteraction.clickItem(@IntRange(from = 0) position: Int): ViewInteraction =22 perform(actionOnItemAtPosition<ViewHolder>(position, ViewActions.click()))...
UITest.kt
Source:UITest.kt
...10 onView(withText(textRes)).check(matches(isDisplayed()))11 fun onViewVisibleText(text: String): ViewInteraction =12 onView(withText(text)).check(matches(isDisplayed()))13 fun enterText(viewId: Int, text: String) =14 onView(withId(viewId)).perform(QabelViewAction.setText(text))15 fun performClickText(resId: Int) {16 onView(withText(resId)).perform(click())17 }18}...
perform
Using AI Code Generation
1ViewInteraction viewInteraction = onView(allOf(withId(R.id.et_username), withText("Enter your username"), isDisplayed()));2viewInteraction.perform(replaceText("admin"), closeSoftKeyboard());3ViewInteraction viewInteraction1 = onView(allOf(withId(R.id.et_password), withText("Enter your password"), isDisplayed()));4viewInteraction1.perform(replaceText("admin"), closeSoftKeyboard());5ViewInteraction viewInteraction2 = onView(allOf(withId(R.id.btn_login), withText("Login"), isDisplayed()));6viewInteraction2.perform(click());7ViewInteraction viewInteraction3 = onView(allOf(withId(R.id.btn_logout), withText("Logout"), isDisplayed()));8viewInteraction3.perform(click());9}10}
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!!