Best Appium-espresso-driver code snippet using android.support.test.espresso.matcher.ViewMatchers.withResourceName
Builders.kt
Source:Builders.kt
...116 * Matches the view with given resource name117 *118 * @param name Resource name to match119 */120 fun withResourceName(name: String) {121 viewMatchers.add(ViewMatchers.withResourceName(name))122 }123 /**124 * Matches the view by resource name with given matcher125 *126 * @param matcher Matcher for resource name127 */128 fun withResourceName(matcher: Matcher<String>) {129 viewMatchers.add(ViewMatchers.withResourceName(matcher))130 }131 /**132 * Matches the view with given content description133 *134 * @param description Content description to match135 */136 fun withContentDescription(description: String) {137 viewMatchers.add(ViewMatchers.withContentDescription(description))138 }139 /**140 * Matches the view which has parent with given matcher141 *142 * @param function ViewBuilder which will result in parent matcher143 */...
PageExtensions.kt
Source:PageExtensions.kt
...45fun BasePage.onViewWithClassName(arg0: Matcher<String>): ViewInteraction = Espresso.onView(ViewMatchers.withClassName(arg0))46fun BasePage.onViewWithContentDescription(arg0: Int): ViewInteraction = Espresso.onView(ViewMatchers.withContentDescription(arg0))47fun BasePage.onViewWithContentDescription(arg0: Matcher<out CharSequence>): ViewInteraction = Espresso.onView(ViewMatchers.withContentDescription(arg0))48fun BasePage.onViewWithContentDescription(arg0: String): ViewInteraction = Espresso.onView(ViewMatchers.withContentDescription(arg0))49fun BasePage.onViewWithResourceName(arg0: String): ViewInteraction = Espresso.onView(ViewMatchers.withResourceName(arg0))50fun BasePage.onViewWithResourceName(arg0: Matcher<String>): ViewInteraction = Espresso.onView(ViewMatchers.withResourceName(arg0))51fun BasePage.onViewWithTagKey(arg0: Int, arg1: Matcher<Any>): ViewInteraction = Espresso.onView(ViewMatchers.withTagKey(arg0, arg1))52fun BasePage.onViewWithTagKey(arg0: Int): ViewInteraction = Espresso.onView(ViewMatchers.withTagKey(arg0))53fun BasePage.onViewWithTagValue(arg0: Matcher<Any>): ViewInteraction = Espresso.onView(ViewMatchers.withTagValue(arg0))54fun BasePage.onViewWithHint(arg0: Int): ViewInteraction = Espresso.onView(ViewMatchers.withHint(arg0))55fun BasePage.onViewWithHint(arg0: Matcher<String>): ViewInteraction = Espresso.onView(ViewMatchers.withHint(arg0))56fun BasePage.onViewWithHint(arg0: String): ViewInteraction = Espresso.onView(ViewMatchers.withHint(arg0))57fun BasePage.onViewWithEffectiveVisibility(arg0: ViewMatchers.Visibility): ViewInteraction = Espresso.onView(ViewMatchers.withEffectiveVisibility(arg0))58fun BasePage.onViewWithChild(arg0: Matcher<View>): ViewInteraction = Espresso.onView(ViewMatchers.withChild(arg0))59fun BasePage.onViewWithSpinnerText(arg0: String): ViewInteraction = Espresso.onView(ViewMatchers.withSpinnerText(arg0))60fun BasePage.onViewWithSpinnerText(arg0: Matcher<String>): ViewInteraction = Espresso.onView(ViewMatchers.withSpinnerText(arg0))61fun BasePage.onViewWithSpinnerText(arg0: Int): ViewInteraction = Espresso.onView(ViewMatchers.withSpinnerText(arg0))62fun BasePage.onViewWithInputType(arg0: Int): ViewInteraction = Espresso.onView(ViewMatchers.withInputType(arg0))63fun BasePage.waitForViewWithParent(arg0: Matcher<View>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withParent(arg0))64fun BasePage.waitForViewWithText(arg0: String): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withText(arg0))65fun BasePage.waitForViewWithText(arg0: Matcher<String>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withText(arg0))66fun BasePage.waitForViewWithText(arg0: Int): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withText(arg0))67fun BasePage.waitForViewWithId(arg0: Int): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withId(arg0))68fun BasePage.waitForViewWithId(arg0: Matcher<Int>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withId(arg0))69fun BasePage.waitForViewWithClassName(arg0: Matcher<String>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withClassName(arg0))70fun BasePage.waitForViewWithContentDescription(arg0: Int): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withContentDescription(arg0))71fun BasePage.waitForViewWithContentDescription(arg0: Matcher<out CharSequence>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withContentDescription(arg0))72fun BasePage.waitForViewWithContentDescription(arg0: String): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withContentDescription(arg0))73fun BasePage.waitForViewWithResourceName(arg0: String): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withResourceName(arg0))74fun BasePage.waitForViewWithResourceName(arg0: Matcher<String>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withResourceName(arg0))75fun BasePage.waitForViewWithTagKey(arg0: Int, arg1: Matcher<Any>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withTagKey(arg0, arg1))76fun BasePage.waitForViewWithTagKey(arg0: Int): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withTagKey(arg0))77fun BasePage.waitForViewWithTagValue(arg0: Matcher<Any>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withTagValue(arg0))78fun BasePage.waitForViewWithHint(arg0: Int): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withHint(arg0))79fun BasePage.waitForViewWithHint(arg0: Matcher<String>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withHint(arg0))80fun BasePage.waitForViewWithHint(arg0: String): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withHint(arg0))81fun BasePage.waitForViewWithEffectiveVisibility(arg0: ViewMatchers.Visibility): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withEffectiveVisibility(arg0))82fun BasePage.waitForViewWithChild(arg0: Matcher<View>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withChild(arg0))83fun BasePage.waitForViewWithSpinnerText(arg0: String): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withSpinnerText(arg0))84fun BasePage.waitForViewWithSpinnerText(arg0: Matcher<String>): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withSpinnerText(arg0))85fun BasePage.waitForViewWithSpinnerText(arg0: Int): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withSpinnerText(arg0))86fun BasePage.waitForViewWithInputType(arg0: Int): ViewInteraction = WaitForViewMatcher.waitForView(ViewMatchers.withInputType(arg0))87fun BasePage.getStringFromResource(stringResource: Int): String{88 val targetContext = InstrumentationRegistry.getTargetContext()...
CustomEspresso.kt
Source:CustomEspresso.kt
...17fun <T> iz(matcher: Matcher<T>): Matcher<T> = `is`(matcher)18fun <T> iz(value: T): Matcher<T> = iz(equalTo(value))19// Custom espresso matchers20// kotlin translation of https://gist.github.com/cpeppas/b5ffe6bd29b67d96416a21fun withResourceName(resourceName: String) = withResourceName(iz(resourceName))22fun withResourceName(resourceNameMatcher: Matcher<String>): Matcher<View> {23 return object : TypeSafeMatcher<View>() {24 override fun matchesSafely(view: View): Boolean {25 val id = view.id26 return id != View.NO_ID && id != 0 && view.resources != null &&27 resourceNameMatcher.matches(view.resources.getResourceName(id))28 }29 override fun describeTo(desc: Description) {30 desc.appendText("with resource name: ")31 resourceNameMatcher.describeTo(desc)32 }33 }34}35fun withAdaptedData(dataMatcher: Matcher<Any>): Matcher<View> {36 return object : TypeSafeMatcher<View>() {...
EspressoUiAction.kt
Source:EspressoUiAction.kt
...12@DslTagMarker13class EspressoUiAction(init: EspressoUiAction.() -> Unit, val action: (List<Matcher<View>>) -> ViewInteraction) : Function0<ViewInteraction> {14 val matchersList = ArrayList<Matcher<View>>()15 var id: Int by SideEffectDelegator { matchersList.add(ViewMatchers.withId(it)) }16 var resName: String by SideEffectDelegator { matchersList.add(ViewMatchers.withResourceName(it)) }17 var text: String by SideEffectDelegator { matchersList.add(ViewMatchers.withText(it)) }18 var assignableFrom: Class<out View> by SideEffectDelegator { matchersList.add(ViewMatchers.isAssignableFrom(it)) }19 var textStartsWith: String by SideEffectDelegator { matchersList.add(ViewMatchers.withText(startsWith(it))) }20 var imageDrawableId: Int by SideEffectDelegator { matchersList.add(withImageDrawable(it)) }21 var compoundDrawableId: Int by SideEffectDelegator { matchersList.add(withCompoundDrawable(it)) }22 var backgroundResId: Int by SideEffectDelegator { matchersList.add(withBackground(it)) }23 var withContentDescriptionId: Int by SideEffectDelegator { matchersList.add(ViewMatchers.withContentDescription(it))}24 var withContentDescriptionString: String by SideEffectDelegator { matchersList.add(ViewMatchers.withContentDescription(it)) }25 init {26 init()27 }28 override fun invoke(): ViewInteraction {29 return action(matchersList)30 }...
EditTextActviityTest.kt
Source:EditTextActviityTest.kt
...3import android.support.test.espresso.Espresso.onView4import android.support.test.espresso.action.ViewActions.*5import android.support.test.espresso.assertion.ViewAssertions.matches6import android.support.test.espresso.intent.Intents7import android.support.test.espresso.matcher.ViewMatchers.withResourceName8import android.support.test.espresso.matcher.ViewMatchers.withText9import android.support.test.rule.ActivityTestRule10import android.support.test.runner.AndroidJUnit411import com.sampleNote.work.view.EditTextActivity12import org.junit.After13import org.junit.Before14import org.junit.Rule15import org.junit.Test16import org.junit.runner.RunWith17/**18 * Created by Abhishek Pathak on 03/10/2021.19 */20@RunWith(AndroidJUnit4::class)21class EditTextActviityTest {22 // Created a rule for Note List Activity23 @get:Rule24 public val activityRule: ActivityTestRule<EditTextActivity> =25 ActivityTestRule(EditTextActivity::class.java, true, true)26 @Before27 @Throws(Exception::class)28 fun setUp() {29 //launching the activity that is required to test30 activityRule.launchActivity(Intent())31 // initializing init for intents32 Intents.init()33 }34 @Test35 fun changeText_sameActivity() {36 // Type text and then press the button.37 onView(withResourceName("edittext"))38 .perform(typeText(MessageChanged), closeSoftKeyboard())39 onView(withResourceName("clickBtn")).perform(click())40 // Check that the text was changed.41 onView(withResourceName("TextLabel"))42 .check(matches(withText(MessageChanged)))43 }44 @After45 @Throws(Exception::class)46 fun tearDown() {47 Intents.release()48 }49 companion object {50 const val MessageChanged = "2nd Message"51 }52}...
NoteListActivity.kt
Source:NoteListActivity.kt
...3import android.support.test.espresso.Espresso.onView4import android.support.test.espresso.action.ViewActions.click5import android.support.test.espresso.contrib.RecyclerViewActions6import android.support.test.espresso.intent.Intents7import android.support.test.espresso.matcher.ViewMatchers.withResourceName8import android.support.test.rule.ActivityTestRule9import android.support.test.runner.AndroidJUnit410import android.support.v7.widget.RecyclerView11import com.sampleNote.work.view.NoteListActivity12import org.junit.After13import org.junit.Before14import org.junit.Rule15import org.junit.Test16import org.junit.runner.RunWith17/**18 * Created by Abhishek Pathak on 03/10/2021.19 */20@RunWith(AndroidJUnit4::class)21class NoteListActivityTest {22 // Created a rule for Note List Activity23 @get:Rule24 public val activityRule: ActivityTestRule<NoteListActivity> =25 ActivityTestRule(NoteListActivity::class.java, true, true)26 @Before27 @Throws(Exception::class)28 fun setUp() {29 //launching the activity that is required to test30 activityRule.launchActivity(Intent())31 // initializing init for intents32 Intents.init()33 }34 /*35 Test to verify our recyclerview is inflating data fine or not also item click is working fine or not36 */37 @Test38 fun verifyListItemClick() {39 onView(withResourceName("notes_recycler_view")).perform(40 RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(1, click())41 )42 }43 /*44 Verifying a click operation on Fab button45 */46 @Test47 fun verifyFabButtonClick() {48 onView(withResourceName("fab")).perform(click())49 }50 @After51 @Throws(Exception::class)52 fun tearDown() {53 Intents.release()54 }55}...
DialogTest.kt
Source:DialogTest.kt
...16 @Test17 fun viewShouldBeVisible() {18 onView(withId(R.id.buttonShowErrorDialog))19 .perform(click())20 onView(withResourceName("alertTitle"))21 .inRoot(isDialog())22 .check(matches(withText("Error")))23 .check(matches(isDisplayed()))24 onView(withResourceName("message"))25 .inRoot(isDialog())26 .check(matches(withText("Some error message...")))27 .check(matches(isDisplayed()))28 onView(withResourceName("button1"))29 .inRoot(isDialog())30 .check(matches(withText(android.R.string.ok)))31 .check(matches(isDisplayed()))32 }33}...
ConfirmDialog.kt
Source:ConfirmDialog.kt
2import android.support.test.espresso.Espresso.*3import android.support.test.espresso.action.ViewActions.*4import android.support.test.espresso.matcher.ViewMatchers.*5class ConfirmDialog {6 val message = withResourceName("message")7 val yesButton = withResourceName("button1")8 val noButton = withResourceName("button2")9 fun tapConfirmNoButton() {10 onView(noButton).perform(click())11 }12 fun tapConfirmYesButton() {13 onView(yesButton).perform(click())14 }15}...
withResourceName
Using AI Code Generation
1public static Matcher<View> withResourceName ( final Matcher < String > resourceNameMatcher ) { return new BoundedMatcher < View , View > ( View . class ) { @Override public boolean matchesSafely ( View view ) { int id = view . getId ( ) ; return id != NO_ID && resourceNameMatcher . matches ( view . getResources ( ) . getResourceEntryName ( id ) ) ; } @Override public void describeTo ( Description description ) { description . appendText ( "with resource name: " ) ; resourceNameMatcher . describeTo ( description ) ; } } ; }2public static Matcher < View > withResourceName ( final String resourceName ) { return withResourceName ( is ( resourceName ) ) ; }3public static Matcher < View > withResourceName ( int resourceId ) { return withResourceName ( is ( getResourceName ( resourceId ) ) ) ; }4public static Matcher < View > withResourceName ( Matcher < String > resourceNameMatcher ) { return new BoundedMatcher < View , View > ( View . class ) { @Override public boolean matchesSafely ( View view ) { int id = view . getId ( ) ; return id != NO_ID && resourceNameMatcher . matches ( view . getResources ( ) . getResourceEntryName ( id ) ) ; } @Override public void describeTo ( Description description ) { description . appendText ( "with resource name: " ) ; resourceNameMatcher . describeTo ( description ) ; } } ; }5public static Matcher < View > withResourceName ( String resourceName ) { return withResourceName ( is ( resourceName ) ) ; }6public static Matcher < View > withResourceName ( int resourceId ) { return withResourceName ( is ( getResourceName ( resourceId ) ) ) ; }7public static Matcher < View > withResourceName ( Matcher < String > resourceNameMatcher ) { return new BoundedMatcher < View , View > ( View . class ) { @Override public boolean matches
withResourceName
Using AI Code Generation
1onView(withResourceName(“com.example.app:id/username”)).perform(typeText(“username”));2onView(withParent(withId(R.id.parent))).perform(click());3onView(withParentIndex(1)).perform(click());4onView(withParentIndex(1)).perform(click());5onView(withParentIndex(1)).perform(click());6onView(withText(“text”)).perform(click());7onView(withText(“text”)).perform(click());8onView(withText(“text”)).perform(click());9onView(withText(“text”)).perform(click());10onView(withText(“text”)).perform(click());
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!!