Best Appium-espresso-driver code snippet using android.support.test.espresso.UiController
ContactDetailFragmentTest.kt
Source:ContactDetailFragmentTest.kt
1package net.radityalabs.contactapp2import android.app.Activity3import android.app.Instrumentation4import android.os.Build5import android.support.test.espresso.UiController6import android.support.test.espresso.ViewAction7import android.support.test.espresso.contrib.RecyclerViewActions8import android.support.test.espresso.intent.rule.IntentsTestRule9import android.support.test.filters.LargeTest10import android.support.test.rule.ActivityTestRule11import android.support.test.runner.AndroidJUnit412import android.view.View13import net.radityalabs.contactapp.data.network.response.ContactListResponse14import net.radityalabs.contactapp.presentation.ui.activity.ContactDetailActivity15import net.radityalabs.contactapp.presentation.ui.activity.ContactListActivity16import net.radityalabs.contactapp.presentation.ui.fragment.ContactDetailFragment17import net.radityalabs.contactapp.presentation.util.PhoneUtil18import net.radityalabs.contactapp.test.DrawableMatcher19import net.radityalabs.contactapp.test.FragmentTestRule20import org.hamcrest.Matcher21import org.junit.Before22import org.junit.Rule23import org.junit.Test24import org.junit.runner.RunWith25import android.support.test.espresso.Espresso.onView26import android.support.test.espresso.action.ViewActions.closeSoftKeyboard27import android.support.test.espresso.assertion.ViewAssertions.matches28import android.support.test.espresso.intent.Intents.intending29import android.support.test.espresso.intent.matcher.IntentMatchers.toPackage30import android.support.test.espresso.matcher.ViewMatchers.isDisplayed31import android.support.test.espresso.matcher.ViewMatchers.withId32import android.support.test.espresso.matcher.ViewMatchers.withText33import net.radityalabs.contactapp.test.ViewAction.setTextInTextView34/**35 * Created by radityagumay on 4/19/17.36 */37@RunWith(AndroidJUnit4::class)38@LargeTest39class ContactDetailFragmentTest {40 @Rule41 var mContactDetailFragmentRule = FragmentTestRule<ContactDetailFragment>(ContactDetailFragment::class.java)42 @Rule43 var mContactListRule = ActivityTestRule<ContactListActivity>(ContactListActivity::class.java)44 @Rule45 var mContactDetailRule = IntentsTestRule<ContactDetailActivity>(ContactDetailActivity::class.java, true, false)46 @Before47 fun setup() {48 startActivity()49 }50 @Test51 fun fragment_can_be_instantiated() {52 onView(withId(R.id.container)).check(matches(isDisplayed()))53 }54 @Test55 fun is_view_available() {56 onView(withId(R.id.rv_info)).check(matches(isDisplayed()))57 onView(withId(R.id.toolbar)).check(matches(isDisplayed()))58 onView(withId(R.id.tv_full_name)).check(matches(isDisplayed()))59 onView(withId(R.id.iv_image)).check(matches(isDisplayed()))60 }61 @Test62 fun is_widget_filled() {63 onView(withId(R.id.tv_full_name)).perform(setTextInTextView(STRING_TO_BE_TYPED), closeSoftKeyboard())64 onView(withId(R.id.tv_full_name)).check(matches(withText(STRING_TO_BE_TYPED)))65 onView(withId(R.id.iv_image)).check(matches(withDrawable(R.mipmap.ic_betty_allen)))66 }67 @Test68 fun send_sms_test() {69 val result = Instrumentation.ActivityResult(Activity.RESULT_OK, PhoneUtil.sms(PHONE_NUMBER))70 intending(toPackage(PACKAGE_ANDROID_DIALER)).respondWith(result)71 onView(withId(R.id.rv_info)).perform(RecyclerViewActions72 .actionOnItemAtPosition<ViewHolder>(0, object : ViewAction {73 override fun getConstraints(): Matcher<View>? {74 return null75 }76 override fun getDescription(): String {77 return "Click on specific button"78 }79 override fun perform(uiController: UiController, view: View) {80 val v = view.findViewById(R.id.iv_right_icon)81 v.performClick()82 }83 }))84 }85 @Test86 fun calling_test() {87 val result = Instrumentation.ActivityResult(Activity.RESULT_OK, PhoneUtil.call(PHONE_NUMBER))88 intending(toPackage(PACKAGE_ANDROID_DIALER)).respondWith(result)89 onView(withId(R.id.rv_info)).perform(RecyclerViewActions90 .actionOnItemAtPosition<ViewHolder>(0, object : ViewAction {91 override fun getConstraints(): Matcher<View>? {92 return null93 }94 override fun getDescription(): String {95 return "Click on specific button"96 }97 override fun perform(uiController: UiController, view: View) {98 val v = view.findViewById(R.id.iv_left_icon)99 v.performClick()100 }101 }))102 }103 private fun startActivity(): ContactDetailActivity {104 return mContactDetailRule.launchActivity(105 ContactDetailActivity.navigateTest(mContactListRule.activity, contactListResponse()))106 }107 private fun contactListResponse(): ContactListResponse {108 val obj = ContactListResponse()109 obj.id = 1110 obj.firstName = "adit"111 obj.lastName = "gumay"...
MainActivityTest.kt
Source:MainActivityTest.kt
...16package com.example.android.pictureinpicture17import android.content.pm.ActivityInfo18import android.support.test.InstrumentationRegistry19import android.support.test.espresso.Espresso.onView20import android.support.test.espresso.UiController21import android.support.test.espresso.ViewAction22import android.support.test.espresso.action.ViewActions.click23import android.support.test.espresso.assertion.ViewAssertions.matches24import android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom25import android.support.test.espresso.matcher.ViewMatchers.isDisplayed26import android.support.test.espresso.matcher.ViewMatchers.withId27import android.support.test.rule.ActivityTestRule28import android.support.test.runner.AndroidJUnit429import android.view.View30import com.example.android.pictureinpicture.widget.MovieView31import org.hamcrest.Description32import org.hamcrest.Matcher33import org.hamcrest.Matchers.not34import org.hamcrest.TypeSafeMatcher35import org.hamcrest.core.AllOf.allOf36import org.junit.Assert.assertNotNull37import org.junit.Assert.assertThat38import org.junit.Assert.assertTrue39import org.junit.Rule40import org.junit.Test41import org.junit.runner.RunWith42@RunWith(AndroidJUnit4::class)43class MainActivityTest {44 @Rule @JvmField45 val rule = ActivityTestRule(MainActivity::class.java)46 @Test47 fun movie_playingOnPip() {48 // The movie should be playing on start49 onView(withId(R.id.movie))50 .check(matches(allOf(isDisplayed(), isPlaying())))51 .perform(showControls())52 // Click on the button to enter Picture-in-Picture mode53 onView(withId(R.id.minimize)).perform(click())54 InstrumentationRegistry.getInstrumentation().waitForIdleSync()55 // The Activity is paused. We cannot use Espresso to test paused activities.56 rule.runOnUiThread {57 // We are now in Picture-in-Picture mode58 assertTrue(rule.activity.isInPictureInPictureMode)59 val view = rule.activity.findViewById<MovieView>(R.id.movie)60 assertNotNull(view)61 // The video should still be playing62 assertTrue(view.isPlaying)63 }64 }65 @Test66 fun movie_pauseAndResume() {67 // The movie should be playing on start68 onView(withId(R.id.movie))69 .check(matches(allOf(isDisplayed(), isPlaying())))70 .perform(showControls())71 // Pause72 onView(withId(R.id.toggle)).perform(click())73 onView(withId(R.id.movie)).check(matches(not(isPlaying())))74 // Resume75 onView(withId(R.id.toggle)).perform(click())76 onView(withId(R.id.movie)).check(matches(isPlaying()))77 }78 @Test79 fun fullscreen_enabledOnLandscape() {80 rule.runOnUiThread { rule.activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE }81 InstrumentationRegistry.getInstrumentation().waitForIdleSync()82 rule.runOnUiThread {83 val decorView = rule.activity.window.decorView84 assertThat(decorView.systemUiVisibility,85 hasFlag(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN))86 }87 }88 @Test89 fun fullscreen_disabledOnPortrait() {90 rule.runOnUiThread {91 rule.activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT92 }93 InstrumentationRegistry.getInstrumentation().waitForIdleSync()94 rule.runOnUiThread {95 val decorView = rule.activity.window.decorView96 assertThat(decorView.systemUiVisibility,97 not(hasFlag(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)))98 }99 }100 private fun isPlaying(): Matcher<View> {101 return object : TypeSafeMatcher<View>() {102 override fun matchesSafely(view: View): Boolean {103 return (view as MovieView).isPlaying104 }105 override fun describeTo(description: Description) {106 description.appendText("MovieView is playing")107 }108 }109 }110 private fun showControls(): ViewAction {111 return object : ViewAction {112 override fun getConstraints(): Matcher<View> {113 return isAssignableFrom(MovieView::class.java)114 }115 override fun getDescription(): String {116 return "Show controls of MovieView"117 }118 override fun perform(uiController: UiController, view: View) {119 uiController.loopMainThreadUntilIdle()120 (view as MovieView).showControls()121 uiController.loopMainThreadUntilIdle()122 }123 }124 }125 private fun hasFlag(flag: Int): Matcher<in Int> {126 return object : TypeSafeMatcher<Int>() {127 override fun matchesSafely(i: Int?): Boolean {128 return i?.and(flag) == flag129 }130 override fun describeTo(description: Description) {131 description.appendText("Flag integer contains " + flag)132 }...
ViewActions.kt
Source:ViewActions.kt
1package ru.ltst.u2020mvp.util2import android.support.test.espresso.PerformException3import android.support.test.espresso.UiController4import android.support.test.espresso.ViewAction5import android.support.test.espresso.action.GeneralLocation6import android.support.test.espresso.action.GeneralSwipeAction7import android.support.test.espresso.action.Press8import android.support.test.espresso.action.Swipe9import android.support.test.espresso.matcher.ViewMatchers.isRoot10import android.support.test.espresso.matcher.ViewMatchers.withId11import android.support.test.espresso.util.HumanReadables12import android.support.test.espresso.util.TreeIterables13import android.view.View14import org.hamcrest.CoreMatchers.any15import org.hamcrest.Matcher16import java.util.concurrent.TimeoutException17object ViewActions {18 /**19 * Perform action of waiting for a specific view id.20 *21 *22 * E.g.:23 * onView(isRoot()).perform(waitId(R.id.dialogEditor, Sampling.SECONDS_15));24 * @param viewId25 * *26 * @param millis27 * *28 * @return29 */30 fun waitId(viewId: Int, millis: Long): ViewAction {31 return object : ViewAction {32 override fun getConstraints(): Matcher<View> {33 return isRoot()34 }35 override fun getDescription(): String {36 return "wait for a specific view with id <$viewId> during $millis millis."37 }38 override fun perform(uiController: UiController, view: View) {39 uiController.loopMainThreadUntilIdle()40 val startTime = System.currentTimeMillis()41 val endTime = startTime + millis42 val viewMatcher = withId(viewId)43 do {44 for (child in TreeIterables.breadthFirstViewTraversal(view)) {45 // found view with required ID46 if (viewMatcher.matches(child)) {47 return48 }49 }50 uiController.loopMainThreadForAtLeast(50)51 } while (System.currentTimeMillis() < endTime)52 // timeout happens53 throw PerformException.Builder().withActionDescription(this.description).withViewDescription(HumanReadables.describe(view)).withCause(TimeoutException()).build()54 }55 }56 }57 /**58 * Perform action of waiting for a specific time. Useful when you need59 * to wait for animations to end and Espresso fails at waiting.60 *61 *62 * E.g.:63 * onView(isRoot()).perform(waitAtLeast(Sampling.SECONDS_15));64 * @param millis65 * *66 * @return67 */68 fun waitAtLeast(millis: Long): ViewAction {69 return object : ViewAction {70 override fun getConstraints(): Matcher<View> {71 return any(View::class.java)72 }73 override fun getDescription(): String {74 return "wait for at least $millis millis."75 }76 override fun perform(uiController: UiController, view: View) {77 uiController.loopMainThreadUntilIdle()78 uiController.loopMainThreadForAtLeast(millis)79 }80 }81 }82 fun swipeTop(): ViewAction {83 return GeneralSwipeAction(Swipe.FAST, GeneralLocation.BOTTOM_CENTER, GeneralLocation.TOP_CENTER, Press.FINGER)84 }85 fun swipeBottom(): ViewAction {86 return GeneralSwipeAction(Swipe.FAST, GeneralLocation.TOP_CENTER, GeneralLocation.BOTTOM_CENTER, Press.FINGER)87 }88 /**89 * Perform action of waiting until UI thread is free.90 *91 *92 * E.g.:93 * onView(isRoot()).perform(waitUntilIdle());94 * @return95 */96 fun waitUntilIdle(): ViewAction {97 return object : ViewAction {98 override fun getConstraints(): Matcher<View> {99 return any(View::class.java)100 }101 override fun getDescription(): String {102 return "wait until UI thread is free"103 }104 override fun perform(uiController: UiController, view: View) {105 uiController.loopMainThreadUntilIdle()106 }107 }108 }109 fun withCustomConstraints(action: ViewAction, constraints: Matcher<View>): ViewAction {110 return object : ViewAction {111 override fun getDescription(): String {112 return action.description113 }114 override fun getConstraints(): Matcher<View> {115 return constraints;116 }117 override fun perform(uiController: UiController, view: View) {118 action.perform(uiController, view)119 }120 }121 }122}...
RecyclerViewUtils.kt
Source:RecyclerViewUtils.kt
1package eu.caraus.appsflastfm.testUtils.views2import android.support.test.espresso.contrib.RecyclerViewActions.scrollToPosition3import android.support.v7.widget.RecyclerView4import android.support.test.espresso.UiController5import android.support.test.espresso.matcher.ViewMatchers6import android.support.test.espresso.ViewAction7import android.support.test.espresso.util.HumanReadables8import android.support.test.espresso.PerformException9import android.support.annotation.IdRes10import android.support.test.espresso.Espresso11import android.support.test.espresso.assertion.ViewAssertions12import android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom13import android.support.test.espresso.matcher.ViewMatchers.isDisplayed14import android.view.View15import org.hamcrest.*16import org.hamcrest.Matchers.allOf17object RecyclerViewUtils {18 fun <VH : RecyclerView.ViewHolder> actionOnItemViewAtPosition( position:Int, @IdRes viewId:Int, viewAction:ViewAction):ViewAction {19 return ActionOnItemViewAtPositionViewAction<VH>(position, viewId, viewAction)20 }21 fun recyclerViewItemsCount(@IdRes RecyclerViewId: Int): Int {22 var COUNT = 023 val matcher = object : TypeSafeMatcher<View>() {24 override fun matchesSafely(item: View): Boolean {25 COUNT = (item as RecyclerView).adapter.itemCount26 return true27 }28 override fun describeTo(description: Description) {29 }30 }31 Espresso.onView(CoreMatchers.allOf(ViewMatchers.withId(RecyclerViewId), isDisplayed())).check(ViewAssertions.matches(matcher))32 val result = COUNT33 COUNT = 034 return result35 }36 class ActionOnItemViewAtPositionViewAction<VH : RecyclerView.ViewHolder>37 ( private val position:Int,38 @param:IdRes private val viewId:Int,39 private val viewAction:ViewAction )40 : ViewAction {41 override fun getConstraints(): Matcher<View> {42 return allOf( isAssignableFrom(RecyclerView::class.java), isDisplayed() )43 }44 override fun getDescription():String {45 return ("actionOnItemAtPosition performing ViewAction: " + this.viewAction.description + " on item at position: " + this.position)46 }47 override fun perform(uiController:UiController, view:View) {48 val recyclerView = view as RecyclerView49 ScrollToPositionViewAction(this.position).perform( uiController, view)50 uiController.loopMainThreadUntilIdle()51 val targetView = recyclerView.getChildAt(this.position).findViewById<View>(this.viewId)52 if (targetView == null)53 {54 throw PerformException.Builder().withActionDescription(this.toString())55 .withViewDescription(56 HumanReadables.describe(view))57 .withCause(IllegalStateException(58 "No view with id "59 + this.viewId60 + " found at position: "61 + this.position))62 .build()63 }64 else65 {66 this.viewAction.perform(uiController, targetView)67 }68 }69 }70 class ScrollToPositionViewAction (71 private val position : Int )72 : ViewAction {73 override fun getConstraints():Matcher<View> {74 return allOf( isAssignableFrom(RecyclerView::class.java), isDisplayed() )75 }76 override fun getDescription():String {77 return "scroll RecyclerView to position: " + this.position78 }79 override fun perform(uiController:UiController, view:View) {80 val recyclerView = view as RecyclerView81 recyclerView.scrollToPosition(this.position)82 }83 }84 fun withRecyclerView( recyclerViewId : Int) : RecyclerViewMatcher {85 return RecyclerViewMatcher(recyclerViewId)86 }87}...
RecyclerViewItemInteractions.kt
Source:RecyclerViewItemInteractions.kt
1package br.com.concretesolutions.kappuccino.custom.recyclerView2import android.support.annotation.IdRes3import android.support.test.espresso.NoMatchingViewException4import android.support.test.espresso.PerformException.Builder5import android.support.test.espresso.UiController6import android.support.test.espresso.ViewAction7import android.support.test.espresso.ViewAssertion8import android.support.test.espresso.action.ViewActions9import android.support.test.espresso.matcher.ViewMatchers10import android.support.test.espresso.util.HumanReadables11import android.support.v7.widget.RecyclerView12import android.view.View13import org.hamcrest.Matcher14import org.hamcrest.Matchers15internal object RecyclerViewItemInteractions {16 internal const val rootViewId = -117 internal fun actionOnItemViewAtPosition(position: Int, @IdRes viewId: Int, viewAction: ViewAction): ViewAction {18 return ActionOnItemViewAtPositionViewAction(position, viewId, viewAction)19 }20 internal fun assertionOnItemViewAtPosition(position: Int, @IdRes viewId: Int = rootViewId, viewAssertion: ViewAssertion): ViewAssertion {21 return AssertionOnItemViewAtPosition(position, viewId, viewAssertion)22 }23 private class ActionOnItemViewAtPositionViewAction (24 private val position: Int,25 @IdRes private val viewId: Int,26 private val viewAction: ViewAction) : ViewAction {27 override fun getConstraints(): Matcher<View> = createConstraints()28 override fun getDescription(): String {29 return "actionOnItemAtPosition performing ViewAction: ${viewAction.description} on item at position: $position"30 }31 override fun perform(uiController: UiController, view: View) {32 val recyclerView = view as RecyclerView33 ScrollToPositionViewAction(position).perform(uiController, view)34 uiController.loopMainThreadUntilIdle()35 val targetView = recyclerView.getChildAt(position).apply {36 if (viewId == rootViewId) {37 rootView38 } else {39 findViewById<View>(viewId)40 }41 }42 if (targetView == null) {43 throw Builder()44 .withActionDescription(this.toString())45 .withViewDescription(HumanReadables.describe(view))46 .withCause(IllegalStateException("No view with id $viewId found at position: $position"))47 .build()48 } else {49 viewAction.perform(uiController, targetView)50 }51 }52 }53 private class AssertionOnItemViewAtPosition(private val position: Int,54 @IdRes private val viewId: Int,55 private val viewAssertion: ViewAssertion) : ViewAssertion {56 override fun check(view: View?, noViewFoundException: NoMatchingViewException?) {57 val recyclerView = view as RecyclerView58 actionOnItemViewAtPosition(position, viewId, ViewActions.scrollTo())59 val childAt = recyclerView.getChildAt(position)60 val targetView = childAt.findViewById<View>(viewId)61 ?: throw Builder().withCause(noViewFoundException).build()62 viewAssertion.check(targetView, noViewFoundException)63 }64 }65 private class ScrollToPositionViewAction(private val position: Int) : ViewAction {66 override fun getConstraints(): Matcher<View> = createConstraints()67 override fun getDescription(): String {68 return "scroll RecyclerView to position: $position"69 }70 override fun perform(uiController: UiController, view: View) {71 val recyclerView = view as RecyclerView72 recyclerView.scrollToPosition(position)73 }74 }75 private fun createConstraints() = Matchers.allOf(76 ViewMatchers.isAssignableFrom(RecyclerView::class.java), ViewMatchers.isDisplayed())77}...
CenterSwipeAction.kt
Source:CenterSwipeAction.kt
1package ebanx.com.ego.utils2import android.support.test.espresso.PerformException3import android.support.test.espresso.UiController4import android.support.test.espresso.ViewAction5import android.support.test.espresso.action.CoordinatesProvider6import android.support.test.espresso.action.PrecisionDescriber7import android.support.test.espresso.action.Swiper8import android.support.test.espresso.matcher.ViewMatchers9import android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility10import android.support.test.espresso.util.HumanReadables11import android.view.View12import android.view.ViewConfiguration13import org.hamcrest.Matcher14class CenterSwipeAction(private val swiper: Swiper,15 private val startCoordProvide: CoordinatesProvider,16 private val endCoordProvide: CoordinatesProvider,17 private val precDesc: PrecisionDescriber) : ViewAction {18 override fun getConstraints(): Matcher<View> {19 return withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)20 }21 override fun getDescription(): String {22 return "swipe from middle of screen"23 }24 @Suppress("CatchRuntimeException")25 override fun perform(uiController: UiController, view: View) {26 val startCoord = startCoordProvide.calculateCoordinates(view)27 val finalCoord = endCoordProvide.calculateCoordinates(view)28 val precision = precDesc.describePrecision()29 // you could try this for several times until Swiper.Status is achieved or try count is reached30 try {31 swiper.sendSwipe(uiController, startCoord, finalCoord, precision)32 } catch (re: RuntimeException) {33 throw PerformException.Builder()34 .withActionDescription(this.description)35 .withViewDescription(HumanReadables.describe(view))36 .withCause(re)37 .build()38 }39 // ensures that the swipe has been run....
CustomViewActions.kt
Source:CustomViewActions.kt
1import android.support.test.espresso.Espresso.onView2import android.support.test.espresso.UiController3import android.support.test.espresso.ViewAction4import android.support.test.espresso.matcher.ViewMatchers.*5import android.support.v7.widget.RecyclerView6import android.view.View7import org.hamcrest.Matcher8import org.hamcrest.Matchers.allOf9object CustomViewActions {10 fun getTotalItemsFromRecyclerAdapter(resId: Int): Int {11 val counts = IntArray(1) { -1 }12 onView(withId(resId)).perform(object : ViewAction {13 override fun getDescription(): String {14 return "Getting recyclerView adapter's item count"15 }16 override fun getConstraints(): Matcher<View> {17 return isAssignableFrom(RecyclerView::class.java)18 }19 override fun perform(uiController: UiController?, view: View?) {20 if (view is RecyclerView) {21 val count = view.adapter?.itemCount22 if (count != null)23 counts[0] = count24 }25 }26 })27 return counts[0]28 }29 fun recyclerScrollTo(position: Int): ViewAction {30 return object : ViewAction {31 override fun getConstraints(): Matcher<View> {32 //just checking whether thew view is recyclerView and is displayed33 return allOf<View>(isAssignableFrom(RecyclerView::class.java), isDisplayed())34 }35 override fun getDescription(): String {36 return "recyclerScrollTo:"37 }38 override fun perform(uiController: UiController, view: View) {39 val recyclerView = view as RecyclerView40 recyclerView.scrollToPosition(position)41 }42 }43 }44}...
ViewAction.kt
Source:ViewAction.kt
1package net.radityalabs.contactapp.test2import android.support.test.espresso.UiController3import android.view.View4import android.widget.TextView5import org.hamcrest.Matcher6import android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom7import android.support.test.espresso.matcher.ViewMatchers.isDisplayed8import org.hamcrest.CoreMatchers.allOf9/**10 * Created by radityagumay on 4/19/17.11 */12object ViewAction {13 fun setTextInTextView(value: String): android.support.test.espresso.ViewAction {14 return object : android.support.test.espresso.ViewAction {15 override fun getConstraints(): Matcher<View> {16 return allOf(isDisplayed(), isAssignableFrom(TextView::class.java!!))17 }18 override fun perform(uiController: UiController, view: View) {19 (view as TextView).text = value20 }21 override fun getDescription(): String {22 return "replace text"23 }24 }25 }26}...
UiController
Using AI Code Generation
1import android.support.test.espresso.UiController;2import android.support.test.espresso.ViewAction;3import android.support.test.espresso.matcher.ViewMatchers;4import android.support.test.rule.ActivityTestRule;5import android.support.test.runner.AndroidJUnit4;6import android.view.View;7import org.hamcrest.Matcher;8import org.junit.Rule;9import org.junit.Test;10import org.junit.runner.RunWith;11import static android.support.test.espresso.Espresso.onView;12import static android.support.test.espresso.action.ViewActions.click;13import static android.support.test.espresso.action.ViewActions.typeText;14import static android.support.test.espresso.assertion.ViewAssertions.matches;15import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;16import static android.support.test.espresso.matcher.ViewMatchers.withId;17import static android.support.test.espresso.matcher.ViewMatchers.withText;18import static org.hamcrest.Matchers.allOf;19@RunWith(AndroidJUnit4.class)20public class MainActivityTest {21public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);22public void mainActivityTest() {23ViewInteraction appCompatEditText = onView(24allOf(withId(R.id.etName),25childAtPosition(26allOf(withId(R.id.content_main),27childAtPosition(28withId(android.R.id.content),29isDisplayed()));30appCompatEditText.perform(click());31appCompatEditText.perform(replaceText("Hello World"), closeSoftKeyboard());32ViewInteraction appCompatButton = onView(33allOf(withId(R.id.btnSubmit), withText("Submit"),34childAtPosition(35allOf(withId(R.id.content_main),36childAtPosition(37withId(android.R.id.content),38isDisplayed()));39appCompatButton.perform(click());40onView(withId(R.id.tvText)).check(matches(withText("Hello World")));41}42private static ViewAction replaceText(final String text) {43return new ViewAction() {44public Matcher<View> getConstraints() {45return ViewMatchers.isAssignableFrom(TextView.class);46}47public String getDescription() {48return "replace text";49}50public void perform(UiController uiController, View view) {51((TextView) view).setText(text);52}53};54}55}56public class MainActivity extends AppCompatActivity {57private EditText etName;58private Button btnSubmit;59private TextView tvText;60protected void onCreate(Bundle savedInstanceState) {61super.onCreate(savedInstanceState);62setContentView(R.layout.activity_main);63etName = (EditText) findViewById(R.id.etName);64btnSubmit = (Button) findViewById(R.id.btnSubmit);65tvText = (TextView) findViewById(R.id.tvText);
UiController
Using AI Code Generation
1import android.support.test.espresso.UiController;2import android.support.test.espresso.ViewAction;3import android.support.test.espresso.matcher.ViewMatchers;4import android.view.View;5import android.widget.TextView;6import org.hamcrest.Matcher;7public class TextViewAction implements ViewAction {8public Matcher<View> getConstraints() {9return ViewMatchers.isAssignableFrom(TextView.class);10}11public String getDescription() {12return "replace text";13}14public void perform(UiController uiController, View view) {15((TextView) view).setText("new text");16}17}18import android.support.test.uiautomator.UiController;19import android.support.test.uiautomator.UiObject;20import android.support.test.uiautomator.UiObjectNotFoundException;21import android.support.test.uiautomator.UiSelector;22import android.view.View;23import android.widget.TextView;24import org.hamcrest.Matcher;25public class TextViewAction implements ViewAction {26public Matcher<View> getConstraints() {27return ViewMatchers.isAssignableFrom(TextView.class);28}29public String getDescription() {30return "replace text";31}32public void perform(UiController uiController, View view) {33UiObject uiObject = new UiObject(new UiSelector().text("old text"));34try {35uiObject.setText("new text");36} catch (UiObjectNotFoundException e) {37e.printStackTrace();38}39}40}41onView(withId(R.id.textView)).check(matches(withText("new text")));42onView(withId(R.id.textView)).perform(new TextViewAction());43public class MainActivityTest {44public void testTextView() {45onView(withId(R.id.textView)).perform(new TextViewAction());46onView(withId(R.id.textView)).check(matches(withText("new text")));47}48}
UiController
Using AI Code Generation
1import android.support.test.espresso.UiController;2import android.support.test.espresso.ViewAction;3import android.view.View;4import org.hamcrest.Matcher;5public class CustomViewAction {6public static ViewAction setTextInTextView(final String value) {7return new ViewAction() {8public Matcher<View> getConstraints() {9return null;10}11public String getDescription() {12return "Set value in the text view";13}14public void perform(UiController uiController, View view) {15((TextView)view).setText(value);16}17};18}19}20onView(withId(R.id.editText)).perform(CustomViewAction.setTextInTextView("Amit"));
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!!