How to use action class of androidx.test.espresso package

Best Appium-espresso-driver code snippet using androidx.test.espresso.action

EspressoActions.kt

Source:EspressoActions.kt Github

copy

Full Screen

...4import android.view.View5import androidx.test.espresso.PerformException6import androidx.test.espresso.UiController7import androidx.test.espresso.ViewAction8import androidx.test.espresso.action.CoordinatesProvider9import androidx.test.espresso.action.GeneralClickAction10import androidx.test.espresso.action.GeneralSwipeAction11import androidx.test.espresso.action.PrecisionDescriber12import androidx.test.espresso.action.Press13import androidx.test.espresso.action.Swipe14import androidx.test.espresso.action.SwipeDirection15import androidx.test.espresso.action.Swiper16import androidx.test.espresso.action.Tap17import androidx.test.espresso.action.ViewActions18import androidx.test.espresso.action.ViewActions.actionWithAssertions19import androidx.test.espresso.util.HumanReadables20import com.avito.android.test.UITestConfig21import com.avito.android.test.element.field.actions.TypeText22import com.avito.android.test.espresso.action.ActionOnClickableElement23import com.avito.android.test.espresso.action.ActionOnEnabledElement24import com.avito.android.test.espresso.action.ActionOnLongClickableElement25import com.avito.android.test.espresso.action.WaitForIdleAction26import com.avito.android.test.espresso.action.click.inProcessClickAction27import com.avito.android.test.espresso.action.click.inProcessLongClickAction28import com.avito.android.test.espresso.action.scroll.ScrollToIfPossibleAction29import org.hamcrest.Matcher30import org.hamcrest.Matchers31// TODO: make internal32object EspressoActions {33 fun typeText(stringToBeTyped: String): ViewAction =34 actionWithAssertions(TypeText(stringToBeTyped))35 /**36 * Enables scrolling to the given view.37 * Less strict version of ScrollToAction38 * If view not a descendant of a ScrollView - nothing happens39 */40 fun scrollIfPossible(): ViewAction = actionWithAssertions(ScrollToIfPossibleAction())41 /**42 * Hack: waits for main thread to be idle43 */44 fun waitForIdle(): ViewAction = actionWithAssertions(WaitForIdleAction())45 /**46 * Expose espresso API47 */48 fun swipe(49 direction: SwipeDirection,50 speed: Swiper = Swipe.FAST,51 precision: PrecisionDescriber = Press.FINGER52 ): ViewAction {53 val providers = direction.toCoordinateProvider()54 return actionWithAssertions(55 GeneralSwipeAction(56 speed,57 providers.first,58 providers.second,59 precision60 )61 )62 }63 fun click(64 type: UITestConfig.ClickType = UITestConfig.clicksType,65 coordinatesProvider: CoordinatesProvider = VisibleCenterGlobalCoordinatesProvider(),66 visualize: Boolean = UITestConfig.visualizeClicks67 ): ViewAction {68 fun safeAction(action: ViewAction) = ActionOnEnabledElement(69 ActionOnClickableElement(action)70 )71 val clickAction = when (type) {72 is UITestConfig.ClickType.EspressoClick -> when (type.rollbackPolicy) {73 is UITestConfig.ClickType.EspressoClick.ClickRollbackPolicy.DoNothing -> defaultEspressoClickAction(74 coordinatesProvider75 )76 is UITestConfig.ClickType.EspressoClick.ClickRollbackPolicy.TryOneMoreClick -> {77 val rollbackAction = safeAction(defaultEspressoClickAction(coordinatesProvider))78 return defaultEspressoClickAction(coordinatesProvider, rollbackAction)79 }80 is UITestConfig.ClickType.EspressoClick.ClickRollbackPolicy.Fail -> defaultEspressoClickAction(81 coordinatesProvider,82 object : ViewAction {83 override fun getDescription(): String =84 "fake fail action after click interpreted as long click"85 override fun getConstraints(): Matcher<View> = Matchers.any(View::class.java)86 override fun perform(uiController: UiController?, view: View?) {87 throw PerformException.Builder()88 .withActionDescription("click interpreted as long click")89 // TODO Replace with relevant description90 .withViewDescription(HumanReadables.describe(view))91 .build()92 }93 }94 )95 }96 is UITestConfig.ClickType.InProcessClick -> inProcessClickAction(97 coordinatesProvider = coordinatesProvider,98 visualizeClicks = visualize99 )100 }101 return safeAction(clickAction)102 }103 fun longClick(104 type: UITestConfig.ClickType = UITestConfig.clicksType,105 coordinatesProvider: CoordinatesProvider = VisibleCenterGlobalCoordinatesProvider(),106 visualize: Boolean = UITestConfig.visualizeClicks107 ): ViewAction {108 fun safeAction(action: ViewAction) = ActionOnEnabledElement(109 ActionOnLongClickableElement(action)110 )111 return safeAction(112 when (type) {113 is UITestConfig.ClickType.EspressoClick -> ViewActions.longClick()114 is UITestConfig.ClickType.InProcessClick -> inProcessLongClickAction(coordinatesProvider, visualize)115 }116 )117 }118 /**119 * Same as [ViewActions.click] but with usage of given coordinates provider120 */121 private fun defaultEspressoClickAction(122 coordinatesProvider: CoordinatesProvider,123 rollbackAction: ViewAction? = null124 ): ViewAction =125 actionWithAssertions(126 GeneralClickAction(127 Tap.SINGLE,128 coordinatesProvider,129 Press.FINGER,130 InputDevice.SOURCE_UNKNOWN,131 MotionEvent.BUTTON_PRIMARY,132 rollbackAction133 )134 )135}...

Full Screen

Full Screen

MovieInstrumentationTest.kt

Source:MovieInstrumentationTest.kt Github

copy

Full Screen

1package com.lingga.themoviedb2import androidx.recyclerview.widget.RecyclerView3import androidx.test.espresso.Espresso4import androidx.test.espresso.IdlingRegistry5import androidx.test.espresso.action.ViewActions6import androidx.test.espresso.assertion.ViewAssertions7import androidx.test.espresso.contrib.RecyclerViewActions8import androidx.test.espresso.matcher.ViewMatchers9import androidx.test.espresso.matcher.ViewMatchers.withId10import androidx.test.rule.ActivityTestRule11import com.lingga.themoviedb.utils.EspressoIdlingResource12import org.junit.After13import org.junit.Before14import org.junit.Rule15import org.junit.Test16class MovieInstrumentationTest {17 @Rule18 @JvmField19 var activityRule = ActivityTestRule(MainActivity::class.java)20 @Before21 fun setUp() {22 IdlingRegistry.getInstance().register(EspressoIdlingResource.idlingResource)23 }24 @After25 fun tearDown() {26 IdlingRegistry.getInstance().unregister(EspressoIdlingResource.idlingResource)27 }28 @Test29 fun loadMovie() {30 Espresso.onView(withId(R.id.recycler_view_movie))31 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))32 }33 @Test34 fun loadDetailMovie() {35 Espresso.onView(withId(R.id.movieFragment)).perform(ViewActions.click())36 Espresso.onView(withId(R.id.recycler_view_movie)).perform(37 RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(38 0,39 ViewActions.click()40 )41 )42 Espresso.onView(withId(R.id.favorite_button)).perform(ViewActions.click())43 Espresso.onView(withId(R.id.backdrop_poster))44 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))45 Espresso.onView(withId(R.id.poster))46 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))47 Espresso.onView(withId(R.id.title))48 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))49 Espresso.onView(withId(R.id.release_date))50 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))51 Espresso.onView(withId(R.id.overview))52 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))53 Espresso.onView(withId(R.id.ratingBar))54 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))55 }56 @Test57 fun tvShow() {58 Espresso.onView(withId(R.id.tvFragment)).perform(ViewActions.click())59 Espresso.onView(withId(R.id.recycler_view_tv))60 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))61 }62 @Test63 fun loadDetailTvShow() {64 Espresso.onView(withId(R.id.tvFragment)).perform(ViewActions.click())65 Espresso.onView(withId(R.id.recycler_view_tv)).perform(66 RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(67 0,68 ViewActions.click()69 )70 )71 Espresso.onView(withId(R.id.backdrop_poster))72 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))73 Espresso.onView(withId(R.id.poster))74 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))75 Espresso.onView(withId(R.id.title))76 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))77 Espresso.onView(withId(R.id.release_date))78 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))79 Espresso.onView(withId(R.id.overview))80 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))81 Espresso.onView(withId(R.id.ratingBar))82 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))83 }84 @Test85 fun loadFavorite() {86 Espresso.onView(withId(R.id.favoriteFragment)).perform(ViewActions.click())87 Espresso.onView(withId(R.id.recycler_view_favorite_movie))88 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))89 }90 @Test91 fun loadDetailFavorite() {92 Espresso.onView(withId(R.id.favoriteFragment)).perform(ViewActions.click())93 Espresso.onView(withId(R.id.recycler_view_favorite_movie)).perform(94 RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(95 0,96 ViewActions.click()97 )98 )99 Espresso.onView(withId(R.id.backdrop_poster))100 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))101 Espresso.onView(withId(R.id.poster))102 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))103 Espresso.onView(withId(R.id.title))104 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))105 Espresso.onView(withId(R.id.release_date))106 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))107 Espresso.onView(withId(R.id.overview))108 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))...

Full Screen

Full Screen

Events.kt

Source:Events.kt Github

copy

Full Screen

...7import androidx.recyclerview.widget.RecyclerView8import androidx.test.core.app.ApplicationProvider9import androidx.test.espresso.Espresso10import androidx.test.espresso.Espresso.onView11import androidx.test.espresso.action.ViewActions.click12import androidx.test.espresso.action.ViewActions.longClick13import androidx.test.espresso.action.ViewActions.pressImeActionButton14import androidx.test.espresso.action.ViewActions.replaceText15import androidx.test.espresso.action.ViewActions.scrollTo16import androidx.test.espresso.assertion.ViewAssertions.matches17import androidx.test.espresso.contrib.DrawerActions18import androidx.test.espresso.contrib.NavigationViewActions19import androidx.test.espresso.contrib.PickerActions20import androidx.test.espresso.contrib.RecyclerViewActions21import androidx.test.espresso.matcher.RootMatchers.isDialog22import androidx.test.espresso.matcher.ViewMatchers23import androidx.test.espresso.matcher.ViewMatchers.isDisplayed24import androidx.test.espresso.matcher.ViewMatchers.isRoot25import androidx.test.espresso.matcher.ViewMatchers.withContentDescription26import androidx.test.espresso.matcher.ViewMatchers.withId27import androidx.test.espresso.matcher.ViewMatchers.withText28import com.escodro.alkaa.R29import java.util.Calendar30/**31 * Handles all the test events.32 */33@Suppress("UndocumentedPublicFunction")34class Events {35 private val context: Context = ApplicationProvider.getApplicationContext()36 fun clickOnView(@IdRes viewId: Int) {37 onView(withId(viewId)).perform(click())38 }39 fun clickOnViewWithText(@StringRes resId: Int) {40 onView(withText(resId)).perform(click())41 }42 fun clickOnViewWithText(text: String) {43 onView(withText(text)).perform(click())44 }45 fun clickOnRecyclerItem(@IdRes recyclerView: Int) {46 onView(withId(recyclerView)).perform(47 RecyclerViewActions48 .actionOnItemAtPosition<RecyclerView.ViewHolder>(0, click())49 )50 }51 fun clickOnCloseChip(@StringRes viewId: Int) {52 onView(withId(viewId)).perform(ViewActions.closeChip())53 }54 fun longPressOnRecyclerItem(@IdRes recyclerView: Int) {55 onView(withId(recyclerView)).perform(56 RecyclerViewActions57 .actionOnItemAtPosition<RecyclerView.ViewHolder>(0, longClick())58 )59 }60 fun clickDialogOption(@StringRes stringResource: Int, index: Int) {61 val dialogOption = context.resources.getStringArray(stringResource)[index]62 onView(withText(dialogOption))63 .inRoot(isDialog())64 .check(matches(isDisplayed()))65 .perform(click())66 }67 fun clickOnChild(@IdRes viewGroupId: Int, index: Int) {68 onView(Matchers.getChildAt(withId(viewGroupId), index)).perform(click())69 }70 fun textOnView(@IdRes viewId: Int, text: String) {71 onView(withId(viewId)).perform(replaceText(text))72 }73 fun pressImeActionButton(@IdRes viewId: Int) {74 onView(withId(viewId)).perform(pressImeActionButton())75 }76 fun navigateUp() {77 onView(withContentDescription(R.string.abc_action_bar_up_description)).perform(click())78 }79 fun waitFor(delay: Long) {80 onView(isRoot()).perform(ViewActions.waitId(delay))81 }82 fun openDrawer(@IdRes drawerId: Int) {83 onView(withId(drawerId)).perform(DrawerActions.open())84 }85 fun clickOnNavigationViewItem(@IdRes viewId: Int, itemId: Int) {86 onView(withId(viewId)).perform(NavigationViewActions.navigateTo(itemId))87 }88 fun scrollTo(@IdRes viewId: Int) {89 onView(withId(viewId)).perform(scrollTo())90 }91 fun setDate(calendar: Calendar) {...

Full Screen

Full Screen

MigrationTest.kt

Source:MigrationTest.kt Github

copy

Full Screen

...5import androidx.recyclerview.widget.RecyclerView6import androidx.test.core.app.ActivityScenario7import androidx.test.core.app.launchActivity8import androidx.test.espresso.Espresso.onView9import androidx.test.espresso.action.ViewActions.click10import androidx.test.espresso.action.ViewActions.scrollTo11import androidx.test.espresso.assertion.ViewAssertions.matches12import androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition13import androidx.test.espresso.matcher.ViewMatchers.hasDescendant14import androidx.test.espresso.matcher.ViewMatchers.withId15import androidx.test.espresso.matcher.ViewMatchers.withText16import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner17import androidx.test.platform.app.InstrumentationRegistry18import org.fedorahosted.freeotp.R19import org.fedorahosted.freeotp.ui.MainActivity20import org.junit.After21import org.junit.Before22import org.junit.Test23import org.junit.runner.RunWith24@RunWith(AndroidJUnit4ClassRunner::class)25class MigrationTest {26 private var activityScenario: ActivityScenario<MainActivity>? = null27 @Before28 fun setup() {29 val context = InstrumentationRegistry.getInstrumentation()30 .targetContext31 val prefs: SharedPreferences = context.getSharedPreferences(32 "tokens", Context.MODE_PRIVATE)33 val globalPrefs = PreferenceManager.getDefaultSharedPreferences(context)34 prefs.edit()35 .putString("microsoft.com:token1totp", "{\"algo\":\"SHA1\",\"counter\":0,\"digits\":6,\"issuerExt\":\"microsoft.com\",\"label\":\"account1totp\",\"period\":30,\"secret\":[0,0,13,111,-99],\"type\":\"TOTP\"}")36 .putString("github.com:token2hotp", "{\"algo\":\"SHA256\",\"counter\":0,\"digits\":6,\"issuerExt\":\"github.com\",\"label\":\"account2hotp\",\"period\":30,\"secret\":[8,66,29,111,-99],\"type\":\"HOTP\"}")37 .putString("tokenOrder", "[\"microsoft.com:token1totp\",\"github.com:token2hotp\"]")38 .commit()39 globalPrefs.edit().putBoolean("tokenMigrated", false)40 .commit()41 activityScenario = launchActivity()42 }43 @Test44 fun testTokenContentAndOrder() {45 onView(withId(R.id.token_list))46 .perform(actionOnItemAtPosition<RecyclerView.ViewHolder>(0, scrollTo()))47 .check(matches(hasDescendant(withText("account1totp"))))48 .check(matches(hasDescendant(withText("microsoft.com"))))49 onView(withId(R.id.token_list))50 .perform(actionOnItemAtPosition<RecyclerView.ViewHolder>(1, scrollTo()))51 .check(matches(hasDescendant(withText("account2hotp"))))52 .check(matches(hasDescendant(withText("github.com"))))53 }54 @Test55 fun testTotpToken() {56 onView(withId(R.id.token_list))57 .perform(actionOnItemAtPosition<RecyclerView.ViewHolder>(0,58 RecyclerViewAction.clickChildViewWithId(R.id.menu)))59 onView(withText(R.string.edit))60 .perform(click())61 onView(withId(R.id.issuer))62 .check(matches(withText("microsoft.com")))63 onView(withId(R.id.label))64 .check(matches(withText("account1totp")))65 onView(withId(R.id.algorithm))66 .check(matches(withText("SHA1")))67 onView(withId(R.id.digits))68 .check(matches(withText("6")))69 onView(withId(R.id.secret))70 .check(matches(withText("AAAA2345")))71 }72 @Test73 fun testHotpToken() {74 onView(withId(R.id.token_list))75 .perform(actionOnItemAtPosition<RecyclerView.ViewHolder>(1,76 RecyclerViewAction.clickChildViewWithId(R.id.menu)))77 onView(withText(R.string.edit))78 .perform(click())79 onView(withId(R.id.issuer))80 .check(matches(withText("github.com")))81 onView(withId(R.id.label))82 .check(matches(withText("account2hotp")))83 onView(withId(R.id.algorithm))84 .check(matches(withText("SHA256")))85 onView(withId(R.id.digits))86 .check(matches(withText("6")))87 onView(withId(R.id.secret))88 .check(matches(withText("BBBB2345")))89 }...

Full Screen

Full Screen

DeleteCartTesting.kt

Source:DeleteCartTesting.kt Github

copy

Full Screen

1package com.birendra.lensdayshop2import androidx.test.espresso.Espresso3import androidx.test.espresso.Espresso.onView4import androidx.test.espresso.action.ViewActions5import androidx.test.espresso.action.ViewActions.click6import androidx.test.espresso.assertion.ViewAssertions.matches7import androidx.test.espresso.contrib.RecyclerViewActions8import androidx.test.espresso.matcher.ViewMatchers9import androidx.test.espresso.matcher.ViewMatchers.isDisplayed10import androidx.test.espresso.matcher.ViewMatchers.withText11import androidx.test.ext.junit.rules.ActivityScenarioRule12import androidx.test.filters.LargeTest13import com.birendra.lensdayshop.adapter.BookingAdapter14import com.birendra.lensdayshop.api.RetrofitService15import com.birendra.lensdayshop.repository.UserRepository16import kotlinx.coroutines.runBlocking17import org.junit.Rule18import org.junit.Test19import org.junit.runner.RunWith20import org.junit.runners.JUnit421@LargeTest22@RunWith(JUnit4::class)23class DeleteCartTesting {24 @get:Rule25 val testRule = ActivityScenarioRule(BottomActivity::class.java)26 @Test27 fun deleteCartUI()28 {29 runBlocking {30 RetrofitService.Online = true31 var userRepo = UserRepository()32 RetrofitService.token = "Bearer "+userRepo.loginUser("admins","admin").token33 }34 Espresso.onView(ViewMatchers.withId(R.id.navigation_dashboard))35 .perform(ViewActions.click())36 Thread.sleep(400)37 Espresso.onView(ViewMatchers.withId(R.id.recycler))38 .perform(RecyclerViewActions.actionOnItemAtPosition<BookingAdapter.BookingViewHolder>(0,CustomAction.clickItemWithId(R.id.cbCheck)))39 Thread.sleep(400)40 onView(withText("Delete"))41 .perform(click())42 Thread.sleep(2000)43 onView(withText("Continue Shopping"))44 .check(matches(isDisplayed()))45 }46}...

Full Screen

Full Screen

MainActivityTest.kt

Source:MainActivityTest.kt Github

copy

Full Screen

...3import androidx.test.core.app.ApplicationProvider4import androidx.test.espresso.Espresso.onData5import androidx.test.espresso.Espresso.onView6import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu7import androidx.test.espresso.action.ViewActions8import androidx.test.espresso.action.ViewActions.click9import androidx.test.espresso.matcher.ViewMatchers.withId10import androidx.test.espresso.matcher.ViewMatchers.withText11import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner12import org.junit.After13import org.junit.Before14import org.junit.Test15import org.junit.runner.RunWith16/**17 * Instrumented test, which will execute on an Android device.18 *19 * See [testing documentation](http://d.android.com/tools/testing).20 */21@RunWith(AndroidJUnit4ClassRunner::class)22class MainActivityTest {23 lateinit var activityScenario: ActivityScenario<MainActivity>24 @Before25 fun setUp() {26 activityScenario = ActivityScenario.launch(MainActivity::class.java)27 }28 @Test29 fun find_and_click_FAB() {30 onView(withId(R.id.fab)).perform(click())31 }32 @Test33 fun open_menu_click_settings() {34 openActionBarOverflowOrOptionsMenu(ApplicationProvider.getApplicationContext())35 onView(withText(R.string.action_settings)).perform(click())36 }37 @Test38 fun open_menu_click_others() {39 openActionBarOverflowOrOptionsMenu(ApplicationProvider.getApplicationContext())40 onView(withText(R.string.action_others)).perform(click())41 }42 @After43 fun tearDown() {44 activityScenario.close()45 }46}...

Full Screen

Full Screen

FilmListTest.kt

Source:FilmListTest.kt Github

copy

Full Screen

1package company.vk.lection092import androidx.test.espresso.Espresso.onView3import androidx.test.espresso.IdlingRegistry4import androidx.test.espresso.action.ViewActions.*5import androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition6import androidx.test.espresso.matcher.ViewMatchers.isRoot7import androidx.test.espresso.matcher.ViewMatchers.withId8import androidx.test.ext.junit.rules.activityScenarioRule9import androidx.test.platform.app.InstrumentationRegistry10import androidx.test.ext.junit.runners.AndroidJUnit411import company.vk.lection09.presentationlayer.MainActivity12import company.vk.lection09.presentationlayer.adapters.holders.FilmHolder13import org.junit.Test14import org.junit.runner.RunWith15import org.junit.Assert.*16import org.junit.Rule17/**18 * Instrumented test, which will execute on an Android device.19 *20 * See [testing documentation](http://d.android.com/tools/testing).21 */22@RunWith(AndroidJUnit4::class)23class FilmListTest {24 @get:Rule25 var activityScenarioRule = activityScenarioRule<MainActivity>()26 @Test27 fun check_click_10_position() {28 onView(withId(R.id.film_list)).perform(click())29// onView(isRoot()).perform(ViewWaitAction(R.id.recycler))30 val waitRecyclerIdling = ViewWaitIdling(withId(R.id.recycler))31 IdlingRegistry.getInstance().register(waitRecyclerIdling)32 onView(withId(R.id.recycler))33 .perform(actionOnItemAtPosition<FilmHolder>(10, click()))34 }35}...

Full Screen

Full Screen

HomeActivityMyTest.kt

Source:HomeActivityMyTest.kt Github

copy

Full Screen

1package com.abdul.myfirsttap2import androidx.test.espresso.Espresso.closeSoftKeyboard3import androidx.test.espresso.Espresso.onView4import androidx.test.espresso.ViewAction5import androidx.test.espresso.action.ViewActions.click6import androidx.test.espresso.action.ViewActions.typeText7import androidx.test.espresso.assertion.ViewAssertions.matches8import androidx.test.espresso.matcher.ViewMatchers.withId9import androidx.test.espresso.matcher.ViewMatchers.withText10import androidx.test.filters.LargeTest11import androidx.test.rule.ActivityTestRule12import androidx.test.runner.AndroidJUnit413import junit.framework.TestCase14import org.junit.Rule15import org.junit.Test16import org.junit.runner.RunWith17@LargeTest18@RunWith(AndroidJUnit4::class)19class HomeActivityMyTest : TestCase(){20 @Rule...

Full Screen

Full Screen

action

Using AI Code Generation

copy

Full Screen

1import androidx.test.espresso.Espresso.onView;2import androidx.test.espresso.action.ViewActions;3import androidx.test.espresso.assertion.ViewAssertions;4import androidx.test.espresso.matcher.ViewMatchers;5import androidx.test.ext.junit.runners.AndroidJUnit4;6import androidx.test.rule.ActivityTestRule;7import org.junit.Rule;8import org.junit.Test;9import org.junit.runner.RunWith;10import static androidx.test.espresso.matcher.ViewMatchers.withId;11@RunWith(AndroidJUnit4.class)12public class MainActivityTest {13= new ActivityTestRule<>(MainActivity.class);14public void testClickButton() {15onView(withId(R.id.button)).perform(ViewActions.click());16onView(withId(R.id.text_view)).check(ViewAssertions.matches(ViewMatchers.withText("Button clicked!")));17}18}19onView(withId(R.id.edit_text)).perform(ViewActions.replaceText("Hello World"));20In this tutorial, we have learned how to use the Espresso framework to perform UI tests in Android. We have also learned how to use the onView() method to find the view and perform action on it. We have also learned how to use the check() method

Full Screen

Full Screen

action

Using AI Code Generation

copy

Full Screen

1import androidx.test.espresso.Espresso.onView;2import androidx.test.espresso.action.ViewActions;3import androidx.test.espresso.matcher.ViewMatchers;4import androidx.test.rule.ActivityTestRule;5import androidx.test.runner.AndroidJUnit4;6import org.junit.Rule;7import org.junit.Test;8import org.junit.runner.RunWith;9@RunWith(AndroidJUnit4.class)10public class MainActivityTest {11new ActivityTestRule<>(MainActivity.class);12public void clickButton() {13onView(ViewMatchers.withId(R.id.button)).perform(ViewActions.click());14}15}16import androidx.test.espresso.Espresso.onView;17import androidx.test.espresso.action.ViewActions;18import androidx.test.espresso.matcher.ViewMatchers;19import androidx.test.rule.ActivityTestRule;20import androidx.test.runner.AndroidJUnit4;21import org.junit.Rule;22import org.junit.Test;23import org.junit.runner.RunWith;24@RunWith(AndroidJUnit4.class)25public class MainActivityTest {26new ActivityTestRule<>(MainActivity.class);27public void clickButton() {28onView(ViewMatchers.withId(R.id.button)).perform(ViewActions.click());29}30}31import androidx.test.espresso.Espresso.onView;32import androidx.test.espresso.action.ViewActions;33import androidx.test.espresso.matcher.ViewMatchers;34import androidx.test.rule.ActivityTestRule;35import androidx.test.runner.AndroidJUnit4;36import org.junit.Rule;37import org.junit.Test;38import org.junit.runner.RunWith;39@RunWith(AndroidJUnit4.class)40public class MainActivityTest {41new ActivityTestRule<>(MainActivity.class);42public void clickButton() {43onView(ViewMatchers.withId(R.id.button)).perform(ViewActions.click());44}45}46import androidx.test.espresso.Espresso.onView;47import androidx.test.espresso.action.ViewActions;48import androidx.test.espresso.matcher.ViewMatchers;49import androidx.test.rule.ActivityTestRule;50import androidx.test.runner.AndroidJUnit4;51import org.junit.Rule;52import org.junit.Test;53import org.junit.runner.RunWith;54@RunWith(AndroidJUnit4.class)55public class MainActivityTest {56new ActivityTestRule<>(MainActivity.class);57public void clickButton() {58onView(ViewMatchers.withId(R.id.button)).perform(ViewActions.click());59}60}

Full Screen

Full Screen

action

Using AI Code Generation

copy

Full Screen

1import androidx.test.espresso.action.ViewActions;2import androidx.test.espresso.matcher.ViewMatchers;3import androidx.test.rule.ActivityTestRule;4import androidx.test.runner.AndroidJUnit4;5import org.junit.Rule;6import org.junit.Test;7import org.junit.runner.RunWith;8import static androidx.test.espresso.Espresso.onView;9import static androidx.test.espresso.action.ViewActions.click;10import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;11import static androidx.test.espresso.action.ViewActions.typeText;12import static androidx.test.espresso.assertion.ViewAssertions.matches;13import static androidx.test.espresso.matcher.ViewMatchers.withId;14import static androidx.test.espresso.matcher.ViewMatchers.withText;15@RunWith(AndroidJUnit4.class)16public class MainActivityTest {17public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);18public void addition_isCorrect() {19onView(withId(R.id.editText)).perform(typeText("2"), closeSoftKeyboard());20onView(withId(R.id.editText2)).perform(typeText("3"), closeSoftKeyboard());21onView(withId(R.id.button)).perform(click());22onView(withId(R.id.textView)).check(matches(withText("5")));23}24}25You can see in the above code that we have added the following imports:26import androidx.test.espresso.action.ViewActions;27import androidx.test.espresso.matcher.ViewMatchers;28import androidx.test.rule.ActivityTestRule;29import androidx.test.runner.AndroidJUnit4;30import org.junit.Rule;31import org.junit.Test;32import org.junit.runner.RunWith;33import static androidx.test.espresso.Espresso.onView;34import static androidx.test.espresso.action.ViewActions.click;35import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;36import static androidx.test.espresso.action.ViewActions.typeText;37import static androidx.test.espresso.assertion.ViewAssertions.matches;38import static androidx.test.espresso.matcher.ViewMatchers.withId;39import static androidx.test.espresso.matcher.ViewMatchers.withText;40@RunWith(AndroidJUnit4.class)41public class MainActivityTest {42public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);43public void addition_isCorrect() {44onView(withId(R.id.editText)).perform(typeText("2"), close

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Appium-espresso-driver automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful