Best Appium-espresso-driver code snippet using android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA
exampleTest.kt
Source:exampleTest.kt
...69 val scenario = launchFragmentInContainer<example>()70 onView(allOf(withId(R.id.testo_frammento), withText("Hello Fragment!!"))).check(matches(isDisplayed()))71 //verifica che la TextView con un certo ID ha come contenuto un certo testo72 onView(allOf(isAssignableFrom(TextView::class.java), withId(R.id.testo_frammento))).check(matches(withText("Hello Fragment!!")))73 onView(allOf(isAssignableFrom(TextView::class.java), withId(R.id.testo_frammento), isDescendantOfA(withClassName(`is`("android.widget.FrameLayout"))))).check(matches(withText("Hello Fragment!!")))74 //riprova: il seguente fallisce perchè la classe non è corretta75 //onView(allOf(isAssignableFrom(Button::class.java), withId(R.id.testo_frammento), isDescendantOfA(withClassName(`is`("android.widget.FrameLayout"))))).check(matches(withText("Hello Fragment!!")))76 //oppure il genitore non è del tipo giusto77 //onView(allOf(isAssignableFrom(TextView::class.java), withId(R.id.testo_frammento), isDescendantOfA(withClassName(`is`("android.widget.ConstraintLayout"))))).check(matches(withText("Hello Fragment!!")))78 }79 //verifica animazioni fragment80}
ViewMatchersTest.kt
Source:ViewMatchersTest.kt
...22import android.support.test.espresso.matcher.ViewMatchers.hasImeAction23import android.support.test.espresso.matcher.ViewMatchers.hasSibling24import android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom25import android.support.test.espresso.matcher.ViewMatchers.isChecked26import android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA27import android.support.test.espresso.matcher.ViewMatchers.isDisplayed28import android.support.test.espresso.matcher.ViewMatchers.isEnabled29import android.support.test.espresso.matcher.ViewMatchers.isFocusable30import android.support.test.espresso.matcher.ViewMatchers.isSelected31import android.support.test.espresso.matcher.ViewMatchers.supportsInputMethods32import android.support.test.espresso.matcher.ViewMatchers.withChild33import android.support.test.espresso.matcher.ViewMatchers.withClassName34import android.support.test.espresso.matcher.ViewMatchers.withContentDescription35import android.support.test.espresso.matcher.ViewMatchers.withHint36import android.support.test.espresso.matcher.ViewMatchers.withId37import android.support.test.espresso.matcher.ViewMatchers.withParent38import android.support.test.espresso.matcher.ViewMatchers.withText39import org.hamcrest.CoreMatchers.allOf40import org.hamcrest.CoreMatchers.`is`41import org.hamcrest.CoreMatchers.not42/**43 * Lists all ViewMatchers. ViewMatchers here are without functional load.44 * This is done for demonstration purposes.45 */46@RunWith(AndroidJUnit4::class)47class ViewMatchersTest {48 @Test49 fun userProperties() {50 onView(withId(R.id.fab_add_task))51 onView(withText("All TO-DOs"))52 onView(withContentDescription(R.string.menu_filter))53 onView(hasContentDescription())54 onView(withHint(R.string.name_hint))55 }56 @Test57 fun uiProperties() {58 onView(isDisplayed())59 onView(isEnabled())60 onView(isChecked())61 onView(isSelected())62 }63 @Test64 fun objectMatcher() {65 onView(not<View>(isChecked()))66 onView(allOf<View>(withText("item 1"), isChecked()))67 }68 @Test69 fun hierarchy() {70 onView(withParent(withId(R.id.todo_item)))71 onView(withChild(withText("item 2")))72 onView(isDescendantOfA(withId(R.id.todo_item)))73 onView(hasDescendant(isChecked()))74 .check(matches(isDisplayed()))75 .check(matches(isFocusable()))76 onView(hasSibling(withContentDescription(R.string.menu_filter)))77 }78 @Test79 fun input() {80 onView(supportsInputMethods())81 onView(hasImeAction(EditorInfo.IME_ACTION_SEND))82 }83 @Test84 fun classMatchers() {85 onView(isAssignableFrom(CheckBox::class.java))86 onView(withClassName(`is`(FloatingActionButton::class.java.canonicalName)))...
RoomsScreenTest.kt
Source:RoomsScreenTest.kt
...20import android.support.test.espresso.contrib.RecyclerViewActions.scrollTo21import android.support.test.espresso.core.deps.guava.base.Preconditions.checkArgument22import android.support.test.espresso.matcher.ViewMatchers.hasDescendant23import android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom24import android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA25import android.support.test.espresso.matcher.ViewMatchers.isDisplayed26import android.support.test.espresso.matcher.ViewMatchers.withId27import android.support.test.espresso.matcher.ViewMatchers.withText28import org.hamcrest.Matchers.allOf29/**30 * Tests for the rooms screen, the main screen which contains a grid of all rooms.31 */32@RunWith(AndroidJUnit4::class)33@LargeTest34class RoomsScreenTest {35 /**36 * [ActivityTestRule] is a JUnit [@Rule][Rule] to launch your activity under test.37 *38 *39 *40 * Rules are interceptors which are executed for each test method and are important building41 * blocks of Junit tests.42 */43 @Rule44 var mRoomsActivityTestRule = ActivityTestRule(RoomsActivity::class.java)45 /**46 * A custom [Matcher] which matches an item in a [RecyclerView] by its text.47 *48 *49 *50 * View constraints:51 *52 * * View must be a child of a [RecyclerView]53 *54 *55 * @param itemText the text to match56 * @return Matcher that matches text in the given view57 */58 private fun withItemText(itemText: String): Matcher<View> {59 checkArgument(!TextUtils.isEmpty(itemText), "itemText cannot be null or empty")60 return object : TypeSafeMatcher<View>() {61 public override fun matchesSafely(item: View): Boolean {62 return allOf(63 isDescendantOfA(isAssignableFrom(RecyclerView::class.java)),64 withText(itemText)).matches(item)65 }66 override fun describeTo(description: Description) {67 description.appendText("is isDescendantOfA RV with text " + itemText)68 }69 }70 }71 @Test72 @Throws(Exception::class)73 fun clickAddRoomButton_opensAddRoomUi() {74 // Click on the add room button75 onView(withId(R.id.fab_add_rooms)).perform(click())76 // Check if the add room screen is displayed77 onView(withId(R.id.add_room_title)).check(matches(isDisplayed()))78 }79 @Test80 @Throws(Exception::class)81 fun addRoomToRoomsList() {...
SubredditsScreenTest.kt
Source:SubredditsScreenTest.kt
...17import android.support.test.espresso.contrib.RecyclerViewActions.scrollTo18import android.support.test.espresso.core.deps.guava.base.Preconditions.checkArgument19import android.support.test.espresso.matcher.ViewMatchers.hasDescendant20import android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom21import android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA22import android.support.test.espresso.matcher.ViewMatchers.isDisplayed23import android.support.test.espresso.matcher.ViewMatchers.withId24import android.support.test.espresso.matcher.ViewMatchers.withText25import android.support.test.filters.LargeTest26import org.hamcrest.Matchers.allOf27/**28 * Tests for the subreddits screen, screen that containt all of your subreddits29 */30@RunWith(AndroidJUnit4::class)31@LargeTest32class SubredditsScreenTest {33 /**34 * A custom [Matcher] which matches an item in a [RecyclerView] by its text.35 *36 *37 *38 *39 * View constraints:40 *41 * * View must be a child of a [RecyclerView]42 *43 * @param itemText the text to match44 * *45 * @return Matcher that matches text in the given view46 */47 private fun withItemText(itemText: String): Matcher<View> {48 checkArgument(!TextUtils.isEmpty(itemText), "itemText cannot be null or empty")49 return object : TypeSafeMatcher<View>() {50 public override fun matchesSafely(item: View): Boolean {51 return allOf(52 isDescendantOfA(isAssignableFrom(RecyclerView::class.java)),53 withText(itemText)).matches(item)54 }55 override fun describeTo(description: Description) {56 description.appendText("is isDescendantOfA RV with text " + itemText)57 }58 }59 }60 /**61 * [ActivityTestRule] is a JUnit [@Rule][Rule] to launch your activity under test.62 *63 *64 *65 *66 * Rules are interceptors which are executed for each test method and are important building67 * blocks of Junit tests.68 */69 @Rule70 var mSubredditsActivityTestRule: ActivityTestRule<SubredditsActivity> = ActivityTestRule(SubredditsActivity::class.java)...
NestedScrollViewViewAction.kt
Source:NestedScrollViewViewAction.kt
...5import android.support.test.espresso.ViewAction6import android.support.test.espresso.action.ViewActions7import android.support.test.espresso.matcher.ViewMatchers8import android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom9import android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA10import android.support.test.espresso.matcher.ViewMatchers.isDisplayingAtLeast11import android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility12import android.support.test.espresso.util.HumanReadables13import android.support.v4.widget.NestedScrollView14import android.view.View15import org.hamcrest.Matchers.allOf16import org.hamcrest.Matchers.anyOf17import timber.log.Timber18/** [ViewAction] that scrolls the [NestedScrollView] to any desired view */19class NestedScrollViewViewAction : ViewAction {20 companion object {21 private const val DISPLAY_PERCENTAGE = 9022 23 /**24 * Factory method that performs all assertions before the [ViewAction] and then returns it25 *26 * @return the [NestedScrollViewViewAction] instance27 */28 @JvmStatic29 fun scrollTo() = ViewActions.actionWithAssertions(NestedScrollViewViewAction())30 }31 override fun getConstraints() = allOf<View>(32 withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE),33 isDescendantOfA(anyOf<View>(34 isAssignableFrom(NestedScrollView::class.java), isAssignableFrom(NestedScrollView::class.java))35 )36 )37 override fun perform(uiController: UiController, view: View) {38 if (isDisplayingAtLeast(DISPLAY_PERCENTAGE).matches(view)) {39 Timber.i("View is already displayed. Returning.")40 return41 }42 val rect = Rect()43 view.getDrawingRect(rect)44 if (!view.requestRectangleOnScreen(rect, true)) {45 Timber.w("Scrolling to view was requested, but none of the parents scrolled.")46 }47 uiController.loopMainThreadUntilIdle()...
MapsActivityTest.kt
Source:MapsActivityTest.kt
...32 IdlingRegistry.getInstance().register(idlingResource)33 onView(allOf(withId(R.id.pubLogo), isDisplayed())).perform(click())34 onView(allOf(withId(R.id.pubDescription), isDisplayed()))35 onView(withId(R.id.viewPager)).perform(swipeDown())36 onView(allOf(withId(R.id.pubDescription), isDescendantOfA(nthChildOf(withId(R.id.viewPager), 0)))).check(matches(not(isDisplayed())))37 onView(allOf(withId(R.id.pubLogo), isDisplayed())).perform(click())38 onView(allOf(withId(R.id.pubDescription), isDisplayed()))39 Espresso.pressBack()40 onView(allOf(withId(R.id.pubDescription), isDescendantOfA(nthChildOf(withId(R.id.viewPager), 0)))).check(matches(not(isDisplayed())))41 Espresso.pressBack()42 onView(allOf(withId(R.id.pubLogo), isDescendantOfA(nthChildOf(withId(R.id.viewPager), 0)))).check(matches(not(isDisplayed())))43 }44}...
MapsActivityTest2.kt
Source:MapsActivityTest2.kt
1package ru.xmn.russiancraftbeer.screens.map.ui2import android.support.test.espresso.Espresso3import android.support.test.espresso.Espresso.onView4import android.support.test.espresso.IdlingRegistry5import android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA6import android.support.test.espresso.matcher.ViewMatchers.withId7import android.support.test.filters.LargeTest8import android.support.test.rule.ActivityTestRule9import android.support.test.runner.AndroidJUnit410import com.zhuinden.espressohelper.checkIsDisplayed11import com.zhuinden.espressohelper.checkIsNotDisplayed12import com.zhuinden.espressohelper.performClick13import com.zhuinden.espressohelper.performSwipeDown14import org.hamcrest.CoreMatchers.allOf15import org.junit.Rule16import org.junit.Test17import org.junit.runner.RunWith18import ru.xmn.russiancraftbeer.R.id.*19@RunWith(AndroidJUnit4::class)20@LargeTest21class MapsActivityTest2 {22 @Rule23 @JvmField24 val mActivityRule = ActivityTestRule<MapsActivity>(25 MapsActivity::class.java)26 @Test27 fun pager_is_visible() {28 val idlingResource = ElapsedTimeIdlingResource(6000)29 IdlingRegistry.getInstance().register(idlingResource)30 pubLogo.descendantOf(viewPager).performClick()31 pubLogo.descendantOf(viewPager).checkIsDisplayed()32 viewPager.performSwipeDown()33 pubDescription.descendantOf(viewPager).checkIsNotDisplayed()34 pubLogo.descendantOf(viewPager).performClick()35 Espresso.pressBack()36 pubDescription.descendantOf(viewPager).checkIsNotDisplayed()37 Espresso.pressBack()38 pubLogo.descendantOf(viewPager).checkIsNotDisplayed()39 }40}41fun Int.descendantOf(parentId: Int, position: Int = 0) =42 onView(descendantOfMatcher(parentId, position))!!43fun Int.descendantOfMatcher(parentId: Int, position: Int = 0) =44 allOf(withId(this), isDescendantOfA(nthChildOf(withId(parentId), position)))!!...
SnackbarElement.kt
Source:SnackbarElement.kt
...27) {28 val message = ViewElement(29 Matchers.allOf(30 ViewMatchers.withId(R.id.snackbar_text),31 ViewMatchers.isDescendantOfA(matcher)32 )33 )34 val button = ViewElement(35 Matchers.allOf(36 ViewMatchers.withId(R.id.snackbar_action),37 ViewMatchers.isDescendantOfA(matcher)38 )39 )40 fun swipeOut() = EspressoActions.swipe(RIGHT_TO_LEFT)41}...
isDescendantOfA
Using AI Code Generation
1import static android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA;2import static android.support.test.espresso.matcher.ViewMatchers.withId;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 android.support.test.espresso.matcher.ViewMatchers.withId;7import static android.support.test.espresso.matcher.ViewMatchers.withText;8import static android.support.test.espresso.matcher.ViewMatchers.withId;9import static android.support.test.espresso.matcher.ViewMatchers.withText;10import static android.support.test.espresso.matcher.ViewMatchers.withId;11import static android.support.test.espresso.matcher.ViewMatchers.withText;12import static android.support.test.espresso.matcher.ViewMatchers.withId;13import static android.support.test.espresso.matcher.ViewMatchers.withText;14import static android.support.test.espresso.matcher.ViewMatchers.withId;15import static android.support.test.espresso.matcher.ViewMatchers.withText;16import static android.support.test.espresso.matcher.ViewMatchers.withId;17import static android.support.test.espresso.matcher.ViewMatchers.withText;18import static android.support.test.espresso.matcher.ViewMatchers.with
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!!