Best Appium-espresso-driver code snippet using android.support.test.espresso.ViewAssertion
RecyclerViewItemInteractions.kt
Source:RecyclerViewItemInteractions.kt
...3import 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 }...
ChatRobot.kt
Source:ChatRobot.kt
...14 * limitations under the License.15 */16package org.codepond.imdemo.chat17import android.support.test.espresso.Espresso.*18import android.support.test.espresso.ViewAssertion19import android.support.test.espresso.action.ViewActions.*20import android.support.test.espresso.assertion.ViewAssertions.*21import android.support.test.espresso.matcher.ViewMatchers.*22import android.support.v7.widget.RecyclerView23import android.view.View24import android.widget.RelativeLayout25import org.codepond.imdemo.R26import org.hamcrest.Matcher27import org.hamcrest.Matchers.*28import org.junit.Assert.assertEquals29fun isListItem(): Matcher<View> {30 return isDescendantOfA(isAssignableFrom(RecyclerView::class.java))31}32fun isAlignParent(alignParent: Int): ViewAssertion {33 return ViewAssertion { view, noViewFoundException ->34 if (noViewFoundException != null) {35 throw noViewFoundException36 }37 assertThat("View must be a child of RelativeLayout", view.parent, instanceOf<Any>(RelativeLayout::class.java))38 val params = view.layoutParams as RelativeLayout.LayoutParams39 val rules = params.rules40 assertEquals(RelativeLayout.TRUE, rules[alignParent])41 }42}43class ChatRobot {44 inner class Action {45 fun clickSend(): Result {46 onView(withId(R.id.button_send)).perform(click())47 return Result()...
ScrollViewTest.kt
Source:ScrollViewTest.kt
1package ru.spb.speech2import android.support.test.espresso.Espresso.onView3import android.support.test.espresso.action.ViewActions.click4import android.support.test.espresso.action.ViewActions.scrollTo5import android.support.test.espresso.assertion.ViewAssertions.matches6import android.support.test.espresso.intent.rule.IntentsTestRule7import android.support.test.espresso.matcher.ViewMatchers.*8import android.support.test.runner.AndroidJUnit49import org.junit.Rule10import org.junit.Test11import org.junit.runner.RunWith12import android.support.test.espresso.util.HumanReadables13import android.support.test.espresso.matcher.ViewMatchers.isDisplayed14import android.support.test.espresso.ViewAssertion15import org.junit.After16import org.junit.Before17@RunWith(AndroidJUnit4::class)18class ScrollViewTest : BaseInstrumentedTest() {19 @Rule20 @JvmField21 var mIntentsTestRule = IntentsTestRule<TrainingStatisticsActivity>(TrainingStatisticsActivity::class.java)22 lateinit var helper: TestHelper23 @Before24 fun enableDebugMode() {25 helper = TestHelper(mIntentsTestRule.activity)26 helper.setTrainingPresentationMod(true) // вклÑÑение ÑеÑÑовой пÑезенÑаÑии27 }28 @After29 fun disableDebugMode() {30 helper.setTrainingPresentationMod(false) // вÑклÑÑение ÑеÑÑовой пÑезенÑаÑии31 }32 @Test33 fun test(){34 onView(withText(mIntentsTestRule.activity.getString(R.string.share))).check(isNotDisplayed())35 onView(withId(R.id.share1))36 .perform(scrollTo(), click())37 onView(withText(mIntentsTestRule.activity.getString(R.string.share))).check(matches(isDisplayed()))38 }39 fun isNotDisplayed(): ViewAssertion {40 return ViewAssertion { view, _ ->41 if (view != null && isDisplayed().matches(view)) {42 throw AssertionError("View is present in the hierarchy and Displayed: " + HumanReadables.describe(view))43 }44 }45 }46}...
CustomMatchers.kt
Source:CustomMatchers.kt
1package com.kkk.mvp_koin2import android.support.test.espresso.NoMatchingViewException3import android.support.test.espresso.ViewAssertion4import android.support.test.espresso.matcher.BoundedMatcher5import android.support.test.espresso.matcher.ViewMatchers6import android.support.v7.widget.RecyclerView7import android.view.View8import org.hamcrest.CoreMatchers9import org.hamcrest.Description10import org.hamcrest.Matcher11class CustomMatchers {12 companion object {13 fun withItemCount(count: Int): Matcher<View> {14 return object : BoundedMatcher<View, RecyclerView>(RecyclerView::class.java) {15 override fun describeTo(description: Description?) {16 description?.appendText("RecyclerView with item count: $count")17 }18 override fun matchesSafely(item: RecyclerView?): Boolean {19 return item?.adapter?.itemCount == count20 }21 }22 }23 }24}25class CustomAssertions {26 companion object {27 fun hasItemCount(count: Int): ViewAssertion {28 return RecyclerViewItemCountAssertion(count)29 }30 }31 private class RecyclerViewItemCountAssertion(private val count: Int) : ViewAssertion {32 override fun check(view: View, noViewFoundException: NoMatchingViewException?) {33 if (noViewFoundException != null) {34 throw noViewFoundException35 }36 if (view !is RecyclerView) {37 throw IllegalStateException("The asserted view is not RecyclerView")38 }39 if (view.adapter == null) {40 throw IllegalStateException("No adapter is assigned to RecyclerView")41 }42 ViewMatchers.assertThat("RecyclerView item count", view.adapter?.itemCount, CoreMatchers.equalTo(count))43 }44 }45}46private class RecyclerViewItemCountAssertion(private val count: Int) : ViewAssertion {47 override fun check(view: View, noViewFoundException: NoMatchingViewException?) {48 }49}...
extensions.kt
Source:extensions.kt
1package nl.jovmit2import android.support.annotation.StringRes3import android.support.test.espresso.Espresso.onView4import android.support.test.espresso.ViewAction5import android.support.test.espresso.ViewAssertion6import android.support.test.espresso.ViewInteraction7import android.support.test.espresso.assertion.ViewAssertions.matches8import android.support.test.espresso.matcher.ViewMatchers9import android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom10import android.support.test.espresso.matcher.ViewMatchers.withHint11import android.support.test.espresso.matcher.ViewMatchers.withId12import android.support.test.espresso.matcher.ViewMatchers.withParent13import android.support.test.espresso.matcher.ViewMatchers.withText14import android.support.v7.widget.Toolbar15import org.hamcrest.Matchers.allOf16val isDisplayed: ViewAssertion = matches(ViewMatchers.isDisplayed())17fun toolbarWithTitle(@StringRes title: Int): ViewInteraction =18 onView(allOf(withText(title), withParent(isAssignableFrom(Toolbar::class.java))))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...
ViewPagerItemCountAssertion.kt
Source:ViewPagerItemCountAssertion.kt
1package de.metzgore.beansplan.util2import android.support.annotation.IdRes3import android.support.test.espresso.Espresso4import android.support.test.espresso.NoMatchingViewException5import android.support.test.espresso.ViewAssertion6import android.support.test.espresso.matcher.ViewMatchers7import android.support.v4.view.PagerAdapter8import android.support.v4.view.ViewPager9import android.view.View10import com.schibsted.spain.barista.internal.matcher.DisplayedMatchers11import org.hamcrest.CoreMatchers12class ViewPagerItemCountAssertion {13 companion object {14 fun assertViewPagerViewItemCount(@IdRes viewPagerId: Int, expectedItemCount: Int) {15 Espresso.onView(DisplayedMatchers.displayedWithId(viewPagerId)).check(ViewPagerItemCountAssertion(expectedItemCount))16 }17 }18 private class ViewPagerItemCountAssertion(private val count: Int) : ViewAssertion {19 override fun check(view: View, noViewFoundException: NoMatchingViewException?) {20 if (noViewFoundException != null) {21 throw noViewFoundException22 }23 if (view !is ViewPager) {24 throw IllegalStateException("The asserted view is not ViewPager")25 }26 if (view.adapter == null) {27 throw IllegalStateException("No adapter is assigned to ViewPager")28 }29 val pagerAdapter = view.adapter as PagerAdapter30 ViewMatchers.assertThat("RecyclerView item count", pagerAdapter.count, CoreMatchers.equalTo(count))31 }32 }...
CustomAssertion.kt
Source:CustomAssertion.kt
1package poc.com.cricbuzzpoc2import android.view.View3import android.support.test.espresso.ViewAssertion4import android.support.v7.widget.RecyclerView5import android.support.test.espresso.NoMatchingViewException6import android.support.test.espresso.matcher.ViewMatchers7import org.hamcrest.CoreMatchers8class CustomAssertion {9 companion object {10 fun hasItemCount(count: Int): ViewAssertion {11 return RecyclerViewItemCountAssertion(count)12 }13 }14 private class RecyclerViewItemCountAssertion(private val count: Int) : ViewAssertion {15 override fun check(view: View, noViewFoundException: NoMatchingViewException?) {16 if (noViewFoundException != null) {17 throw noViewFoundException18 }19 if (view !is RecyclerView) {20 throw IllegalStateException("The asserted view is not RecyclerView")21 }22 if (view.adapter == null) {23 throw IllegalStateException("No adapter is assigned to RecyclerView")24 }25 ViewMatchers.assertThat("RecyclerView item count", view.adapter.itemCount, CoreMatchers.equalTo(count))26 }27 }28}...
CustomAssertions.kt
Source:CustomAssertions.kt
1package sample.onursaygili.commentator2import android.support.test.espresso.NoMatchingViewException3import android.support.test.espresso.ViewAssertion4import android.support.test.espresso.matcher.ViewMatchers5import android.support.v7.widget.RecyclerView6import android.view.View7import org.hamcrest.CoreMatchers8class CustomAssertions {9 companion object {10 fun hasItemCount(count: Int): ViewAssertion {11 return RecyclerViewItemCountAssertion(count)12 }13 }14 private class RecyclerViewItemCountAssertion(private val count: Int) : ViewAssertion {15 override fun check(view: View, noViewFoundException: NoMatchingViewException?) {16 if (noViewFoundException != null) {17 throw noViewFoundException18 }19 if (view !is RecyclerView) {20 throw IllegalStateException("The asserted view is not RecyclerView")21 }22 if (view.adapter == null) {23 throw IllegalStateException("No adapter is assigned to RecyclerView")24 }25 ViewMatchers.assertThat("RecyclerView item count", view.adapter.itemCount, CoreMatchers.equalTo(count))26 }27 }28}...
ViewAssertion
Using AI Code Generation
1import android.support.test.espresso.Espresso;2import android.support.test.espresso.ViewAssertion;3import android.support.test.espresso.assertion.ViewAssertions;4import android.support.test.espresso.matcher.ViewMatchers;5import android.support.test.rule.ActivityTestRule;6import android.support.test.runner.AndroidJUnit4;7import android.view.View;8import android.widget.TextView;9import org.hamcrest.Matcher;10import org.junit.Rule;11import org.junit.Test;12import org.junit.runner.RunWith;13import static android.support.test.espresso.Espresso.onView;14import static android.support.test.espresso.action.ViewActions.click;15import static android.support.test.espresso.action.ViewActions.typeText;16import static android.support.test.espresso.assertion.ViewAssertions.matches;17import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;18import static android.support.test.espresso.matcher.ViewMatchers.withId;19import static android.support.test.espresso.matcher.ViewMatchers.withText;20@RunWith(AndroidJUnit4.class)21public class MainActivityTest {22 public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class);23 public void checkTextView() {24 onView(ViewMatchers.withId(R.id.textView)).check(matches(withText("Hello World!")));25 }26}27onView(withId(R.id.textView)).check(matches(withText("")));28onView(withId(R.id.textView)).check(matches(withText(not(""))));29onView(withId(R.id.textView)).check(matches(withText(containsString("Hello"))));30onView(withId(R.id.textView)).check(matches(withText(startsWith("Hello"))));31onView(withId(R.id.textView)).check(matches(withText(endsWith("World!"))));
ViewAssertion
Using AI Code Generation
1onView(withId(R.id.textview)).check(matches(isDisplayed()));2onView(withId(R.id.textview)).check(matches(not(isDisplayed())));3onView(withId(R.id.checkbox)).check(matches(isChecked()));4onView(withId(R.id.checkbox)).check(matches(not(isChecked())));5onView(withId(R.id.spinner)).check(matches(isSelected()));6onView(withId(R.id.spinner)).check(matches(not(isSelected())));7onView(withId(R.id.edittext)).check(matches(isEnabled()));8onView(withId(R.id.edittext)).check(matches(not(isEnabled())));9onView(withId(R.id.edittext)).check(matches(isFocusable()));10onView(withId(R.id.edittext)).check(matches(not(isFocusable())));11onView(withId(R.id.button)).check(matches(isClickable()));12onView(withId(R.id.button)).check(matches(not(isClickable())));
ViewAssertion
Using AI Code Generation
1ViewInteraction textView = onView( allOf( withId( R.id.textView ), withText( "Hello World!" ), isDisplayed() ) );2textView.check( matches( withText( "Hello World!" ) ) );3ViewInteraction textView = onView( allOf( withId( R.id.textView ), withText( "Hello World!" ), isDisplayed() ) );4textView.check( matches( withText( "Hello World!" ) ) );5ViewInteraction textView = onView( allOf( withId( R.id.textView ), withText( "Hello World!" ), isDisplayed() ) );6textView.check( matches( withText( "Hello World!" ) ) );7ViewInteraction textView = onView( allOf( withId( R.id.textView ), withText( "Hello World!" ), isDisplayed() ) );8textView.check( matches( withText( "Hello World!" ) ) );9ViewInteraction textView = onView( allOf( withId( R.id.textView ), withText( "Hello World!" ), isDisplayed() ) );10textView.check( matches( withText( "Hello World!" ) ) );11ViewInteraction textView = onView( allOf( withId( R.id.textView ), withText( "Hello World!" ), isDisplayed() ) );12textView.check( matches( withText( "Hello World!" ) ) );13ViewInteraction textView = onView( allOf( withId( R.id.textView ), withText( "Hello World!" ), isDisplayed() ) );14textView.check( matches( withText( "Hello World!" ) ) );15ViewInteraction textView = onView( allOf( withId( R.id.textView ), withText( "Hello World!" ), isDisplayed() ) );16textView.check( matches( withText
ViewAssertion
Using AI Code Generation
1import static android.support.test.espresso.Espresso.onView;2import static android.support.test.espresso.assertion.ViewAssertions.matches;3import static android.support.test.espresso.matcher.ViewMatchers.withText;4import static android.support.test.espresso.matcher.ViewMatchers.withId;5import static org.hamcrest.Matchers.allOf;6import static org.hamcrest.Matchers.containsString;7import android.support.test.espresso.assertion.ViewAssertion;8import android.support.test.espresso.matcher.ViewMatchers;9import android.support.test.rule.ActivityTestRule;10import android.support.test.runner.AndroidJUnit4;11import android.view.View;12import android.widget.TextView;13import org.junit.Rule;14import org.junit.Test;15import org.junit.runner.RunWith;16@RunWith(AndroidJUnit4.class)17public class MainActivityTest {18public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(19MainActivity.class);20public void ensureTextChangesWork() {21onView(withId(R.id.editTextUserInput))22.perform(typeText("HELLO"), closeSoftKeyboard());23onView(withId(R.id.changeTextBt)).perform(click());24onView(withId(R.id.textToBeChanged))25.check(matches(withText("HELLO")));26onView(withId(R.id.textToBeChanged))27.check(matches(withText(containsString("HELLO"))));28onView(withId(R.id.textToBeChanged))29check(new ViewAssertion() {30public void check(View view, NoMatchingViewException noViewFoundException) {31if (!(view instanceof TextView)) {32throw new IllegalStateException("View is not a TextView!");33}34TextView textView = (TextView) view;35String text = textView.getText().toString();36if (!text.equals("HELLO")) {37throw new IllegalStateException("Text is not HELLO!");38}39}40}
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!!