How to use ViewMatchers class of android.support.test.espresso.matcher package

Best Appium-espresso-driver code snippet using android.support.test.espresso.matcher.ViewMatchers

FinalFullTest.kt

Source:FinalFullTest.kt Github

copy

Full Screen

2import android.graphics.Typeface3import android.support.test.espresso.Espresso4import android.support.test.espresso.action.ViewActions5import android.support.test.espresso.assertion.ViewAssertions.matches6import android.support.test.espresso.matcher.ViewMatchers7import android.support.test.espresso.matcher.ViewMatchers.*8import android.support.test.filters.LargeTest9import android.support.test.rule.ActivityTestRule10import android.support.test.rule.GrantPermissionRule11import android.support.test.runner.AndroidJUnit412import android.view.View13import android.widget.GridView14import android.widget.ListView15import android.widget.TextView16import com.google.firebase.Timestamp17import com.google.firebase.auth.FirebaseAuth18import com.google.firebase.auth.UserProfileChangeRequest19import com.google.firebase.firestore.FirebaseFirestore20import org.hamcrest.Description21import org.hamcrest.Matcher22import org.hamcrest.Matchers23import org.hamcrest.Matchers.*24import org.hamcrest.TypeSafeMatcher25import org.junit.Before26import org.junit.Rule27import org.junit.Test28import org.junit.runner.RunWith29import java.time.LocalDateTime30import java.time.format.DateTimeFormatter31@LargeTest32@RunWith(AndroidJUnit4::class)33class FinalFullTest {34 //THIS IS A COMPLETE TEST OF THE APP, UNFORTUNATELY IT REQUIRES THAT THE EspressoEmailFinal1@gmail.com account is deleted.35 //I WASN'T ABLE TO DELETE THE ACCOUNT ON EACH START WITH CODE SO IT NEEDS TO BE DONE MANUALLY ON FIREBASE36 //OTHER THAN THAT, TEST IS REPRODUCIBLE37 @Rule38 @JvmField39 var mActivityTestRule = ActivityTestRule(SignUpActivity::class.java)40 @Rule41 @JvmField42 var mRuntimePermissionRule: GrantPermissionRule = GrantPermissionRule43 .grant(android.Manifest.permission.ACCESS_FINE_LOCATION)44 fun withBoldStyle(): Matcher<View> {45 return object: TypeSafeMatcher<View>() {46 override fun describeTo(description: Description) {47 description.appendText("Text should be bold")48 }49 override fun matchesSafely(item: View): Boolean {50 return (item as TextView).typeface == Typeface.DEFAULT_BOLD51 }52 }53 }54 private fun withGridSize(size: Int): Matcher<View> {55 return object : TypeSafeMatcher<View>() {56 public override fun matchesSafely(view: View): Boolean {57 return (view as GridView).count >= size58 }59 override fun describeTo(description: Description) {60 description.appendText("ListView should have at least $size items")61 }62 }63 }64 private fun withListSize(size: Int): Matcher<View> {65 return object : TypeSafeMatcher<View>() {66 public override fun matchesSafely(view: View): Boolean {67 return (view as ListView).count >= size68 }69 override fun describeTo(description: Description) {70 description.appendText("ListView should have at least $size items")71 }72 }73 }74 private fun createUserDocument(username: String): HashMap<String, Any> {75 val userData = HashMap<String, Any>()76 userData["GOLD"] = 077 userData["CollectedCoinz"] = listOf<HashMap<String, Any>>()78 userData["CollectedID"] = listOf<String>()79 userData["LastDate"] = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"))80 userData["LastTimestamp"] = Timestamp.now().seconds81 userData["CoinzExchanged"] = 082 userData["CoinzReceived"] = 083 userData["Username"] = username84 userData["Rerolled"] = false85 userData["TransferHistory"] = listOf<HashMap<String, Any>>()86 userData["AllCollectedToday"] = false87 val amount = (3..6).shuffled().first()88 val currency = arrayListOf("QUID", "PENY", "DOLR", "SHIL").shuffled().first()89 val reward = arrayListOf(100, 150, 200, 300)[amount - 3]90 val quests: MutableList<HashMap<String, Any>> = arrayListOf()91 val quest = HashMap<String, Any>()92 quest["Amount"] = amount93 quest["Currency"] = currency94 quest["Reward"] = reward95 quest["CompletionStage"] = 096 quests.add(quest)97 userData["Quests"] = quests98 userData["Wager"] = HashMap<String, Any>()99 userData["WageredToday"] = false100 return userData101 }102 @Before103 fun beforeTest() {104 val espressoUser2 = createUserDocument("EspressoUsernameFinal2")105 espressoUser2["CoinzExchanged"] = 22106 espressoUser2["GOLD"] = 123.4107 val collectedCoinz1: MutableList<HashMap<String, Any>> = arrayListOf()108 for (i in (0..5)) {109 val currencies = listOf("QUID", "PENY", "DOLR", "SHIL", "QUID", "PENY")110 val coin = HashMap<String, Any>()111 coin["Currency"] = currencies[i]112 coin["Value"] = (i+0.25)*2113 coin["Time"] = Timestamp.now().seconds114 collectedCoinz1.add(coin)115 }116 espressoUser2["CollectedCoinz"] = collectedCoinz1117 FirebaseAuth.getInstance().createUserWithEmailAndPassword("espressoemailfinal2@gmail.com", "EspressoPasswordFinal2")118 .addOnSuccessListener {119 val profileUpdates = UserProfileChangeRequest.Builder()120 .setDisplayName("EspressoUsernameFinal2")121 .build()122 FirebaseAuth.getInstance().currentUser?.updateProfile(profileUpdates)123 }124 FirebaseFirestore.getInstance().collection("Coinz").document("espressoemailfinal2@gmail.com")125 .set(espressoUser2)126 val espressoUser3 = createUserDocument("EspressoUsernameFinal3")127 espressoUser3["CoinzReceived"] = 23128 espressoUser3["GOLD"] = 567.8129 val transfer = HashMap<String, Any>()130 transfer["Amount"] = 999.9131 transfer["From"] = "Admin"132 val transfers = arrayListOf(transfer)133 espressoUser3["TransferHistory"] = transfers134 FirebaseAuth.getInstance().createUserWithEmailAndPassword("espressoemailfinal3@gmail.com", "EspressoPasswordFinal3")135 .addOnSuccessListener {136 val profileUpdates = UserProfileChangeRequest.Builder()137 .setDisplayName("EspressoUsernameFinal3")138 .build()139 FirebaseAuth.getInstance().currentUser?.updateProfile(profileUpdates)140 }141 FirebaseFirestore.getInstance().collection("Coinz").document("espressoemailfinal3@gmail.com")142 .set(espressoUser3)143 }144 @Test145 fun espressoFinalTest() {146 Espresso.onView(ViewMatchers.withId(R.id.signup_username))147 .perform(ViewActions.replaceText("EspressoUsernameFinal1"))148 Espresso.onView(ViewMatchers.withId(R.id.signup_password))149 .perform(ViewActions.replaceText("EspressoPasswordFinal1"))150 Espresso.onView(ViewMatchers.withId(R.id.signup_email))151 .perform(ViewActions.replaceText("EspressoEmailFinal1@gmail.com"))152 Espresso.onView(ViewMatchers.withId(R.id.email_sign_up_button))153 .perform(ViewActions.click())154 Thread.sleep(15000)155 Espresso.onView(ViewMatchers.withId(R.id.main_OpenMenu))156 .perform(ViewActions.click())157 Espresso.onView(ViewMatchers.withId(R.id.main_SignOut))158 .perform(ViewActions.click())159 Thread.sleep(500)160 Espresso.onView(ViewMatchers.withId(R.id.go_to_login))161 .perform(ViewActions.click())162 Espresso.onView(ViewMatchers.withId(R.id.login_email))163 .perform(ViewActions.replaceText("EspressoEmailFinal1@gmail.com"))164 Espresso.onView(ViewMatchers.withId(R.id.login_password))165 .perform(ViewActions.replaceText("EspressoPasswordFinal1"))166 Espresso.onView(ViewMatchers.withId(R.id.email_sign_in_button))167 .perform(ViewActions.click())168 Thread.sleep(5000)169 Espresso.onView(ViewMatchers.withId(R.id.main_OpenMenu))170 .perform(ViewActions.click())171 Espresso.onView(ViewMatchers.withId(R.id.main_SignOut))172 .perform(ViewActions.click())173 Thread.sleep(500)174 //Register and login test done.175 //Starting test of all activities, exchanging, transfering, setting up wager, and rerolling quest176 Espresso.onView(ViewMatchers.withId(R.id.login_email))177 .perform(ViewActions.replaceText("EspressoEmailFinal2@gmail.com"))178 Espresso.onView(ViewMatchers.withId(R.id.login_password))179 .perform(ViewActions.replaceText("EspressoPasswordFinal2"))180 Espresso.onView(ViewMatchers.withId(R.id.email_sign_in_button))181 .perform(ViewActions.click())182 Thread.sleep(5000)183 Espresso.onView(ViewMatchers.withId(R.id.main_OpenMenu))184 .perform(ViewActions.click())185 Espresso.onView(ViewMatchers.withId(R.id.main_Ranking))186 .perform(ViewActions.click())187 Thread.sleep(1000)188 Espresso.onView(ViewMatchers.withId(R.id.ranking_PlayerRanking))189 .check(matches(withText("Player Ranking")))190 Espresso.pressBack()191 Espresso.onView(ViewMatchers.withId(R.id.main_OpenMenu))192 .perform(ViewActions.click())193 Espresso.onView(ViewMatchers.withId(R.id.main_Bank))194 .perform(ViewActions.click())195 Espresso.onView(withId(R.id.bank_GOLDvalue))196 .check(matches(not(withText("0.0"))))197 Espresso.onView(ViewMatchers.withId(R.id.bank_GOLDvalue))198 .check(matches(withText("123.4")))199 Thread.sleep(500)200 Espresso.onData(anything())201 .inAdapterView(withId(R.id.bank_coinzGridView))202 .atPosition(0)203 .onChildView(withId(R.id.coinExchange))204 .perform(ViewActions.click())205 Thread.sleep(2000)206 Espresso.onView(ViewMatchers.withId(R.id.bank_GOLDvalue))207 .check(matches(not(withText("123.4"))))208 Espresso.onData(anything())209 .inAdapterView(withId(R.id.bank_coinzGridView))210 .atPosition(0)211 .onChildView(withId(R.id.coinExchange))212 .perform(ViewActions.click())213 Thread.sleep(500)214 Espresso.onData(anything())215 .inAdapterView(withId(R.id.bank_coinzGridView))216 .atPosition(0)217 .onChildView(withId(R.id.coinExchange))218 .perform(ViewActions.click())219 Thread.sleep(500)220 Espresso.onView(withId(R.id.bank_exchangedTextView))221 .check(matches(withText(containsString("back tomorrow to exchange"))))222 Espresso.onView(withId(R.id.bank_coinzGridView))223 .check(matches(withGridSize(3)))224 Espresso.onView(withId(R.id.bank_transferButton))225 .perform(ViewActions.click())226 Espresso.onView(withId(R.id.bank_transferEmail))227 .perform(ViewActions.replaceText("EspressoEmailFinal2@gmail.com"))228 Espresso.onData(anything())229 .inAdapterView(withId(R.id.bank_coinzGridView))230 .atPosition(0)231 .onChildView(withId(R.id.coinExchange))232 .perform(ViewActions.click())233 Espresso.onView(ViewMatchers.withId(R.id.bank_transferEmail))234 .check(matches(hasErrorText("You cannot transfer Coinz to yourself")))235 Espresso.onView(withId(R.id.bank_transferEmail))236 .perform(ViewActions.replaceText("JustTesting"))237 Espresso.onData(anything())238 .inAdapterView(withId(R.id.bank_coinzGridView))239 .atPosition(0)240 .onChildView(withId(R.id.coinExchange))241 .perform(ViewActions.click())242 Espresso.onView(ViewMatchers.withId(R.id.bank_transferEmail))243 .check(matches(hasErrorText("This email address is invalid")))244 Espresso.onView(withId(R.id.bank_transferEmail))245 .perform(ViewActions.replaceText("EspressoEmailFinal3@gmail.com"))246 Espresso.onData(anything())247 .inAdapterView(withId(R.id.bank_coinzGridView))248 .atPosition(0)249 .onChildView(withId(R.id.coinExchange))250 .perform(ViewActions.click())251 Thread.sleep(500)252 Espresso.onData(anything())253 .inAdapterView(withId(R.id.bank_coinzGridView))254 .atPosition(0)255 .onChildView(withId(R.id.coinExchange))256 .perform(ViewActions.click())257 Thread.sleep(500)258 Espresso.onData(anything())259 .inAdapterView(withId(R.id.bank_coinzGridView))260 .atPosition(0)261 .onChildView(withId(R.id.coinExchange))262 .perform(ViewActions.click())263 Thread.sleep(5000)264 Espresso.onView(withId(R.id.bank_coinzGridView))265 .check(matches(withGridSize(1)))266 Espresso.pressBack()267 Espresso.onView(ViewMatchers.withId(R.id.main_OpenMenu))268 .perform(ViewActions.click())269 Espresso.onView(ViewMatchers.withId(R.id.main_Race))270 .perform(ViewActions.click())271 Espresso.onView(ViewMatchers.withId(R.id.race_TimeSpinner))272 .perform(ViewActions.click())273 Espresso.onData(Matchers.hasToString(startsWith("30")))274 .perform(ViewActions.click())275 Espresso.onView(ViewMatchers.withId(R.id.race_WagerButton))276 .perform(ViewActions.click())277 Thread.sleep(2000)278 Espresso.onView(ViewMatchers.withId(R.id.main_WagerTextView))279 .check(matches(isDisplayed()))280 Espresso.onView(ViewMatchers.withId(R.id.main_WagerTextView))281 .check(matches(withText(startsWith("Time Left:"))))282 Espresso.onView(ViewMatchers.withId(R.id.main_OpenMenu))283 .perform(ViewActions.click())284 Espresso.onView(ViewMatchers.withId(R.id.main_Transfer))285 .perform(ViewActions.click())286 Espresso.onView(ViewMatchers.withId(R.id.transferHistory_TransferHistoryTextView))287 .check(matches(withText("Transfer History")))288 Espresso.pressBack()289 Espresso.onView(ViewMatchers.withId(R.id.main_OpenMenu))290 .perform(ViewActions.click())291 Espresso.onView(ViewMatchers.withId(R.id.main_Quest))292 .perform(ViewActions.click())293 Espresso.onData(anything())294 .inAdapterView(withId(R.id.quest_listView))295 .atPosition(0)296 .onChildView(withId(R.id.quest_rerollButton))297 .perform(ViewActions.click())298 Thread.sleep(500)299 Espresso.onData(anything())300 .inAdapterView(withId(R.id.quest_listView))301 .atPosition(0)302 .onChildView(withId(R.id.quest_rerollButton))303 .check(matches(Matchers.not(isEnabled())))304 Espresso.pressBack()305 Espresso.onView(ViewMatchers.withId(R.id.main_OpenMenu))306 .perform(ViewActions.click())307 Espresso.onView(ViewMatchers.withId(R.id.main_SignOut))308 .perform(ViewActions.click())309 Thread.sleep(500)310 Espresso.onView(ViewMatchers.withId(R.id.login_email))311 .perform(ViewActions.replaceText("EspressoEmailFinal2@gmail.com"))312 Espresso.onView(ViewMatchers.withId(R.id.login_password))313 .perform(ViewActions.replaceText("EspressoPasswordFinal2"))314 Espresso.onView(ViewMatchers.withId(R.id.email_sign_in_button))315 .perform(ViewActions.click())316 Thread.sleep(5000)317 Espresso.onView(ViewMatchers.withId(R.id.main_WagerTextView))318 .check(matches(isDisplayed()))319 Espresso.onView(ViewMatchers.withId(R.id.main_OpenMenu))320 .perform(ViewActions.click())321 Espresso.onView(ViewMatchers.withId(R.id.main_SignOut))322 .perform(ViewActions.click())323 Thread.sleep(500)324 Espresso.onView(ViewMatchers.withId(R.id.login_email))325 .perform(ViewActions.replaceText("EspressoEmailFinal3@gmail.com"))326 Espresso.onView(ViewMatchers.withId(R.id.login_password))327 .perform(ViewActions.replaceText("EspressoPasswordFinal3"))328 Espresso.onView(ViewMatchers.withId(R.id.email_sign_in_button))329 .perform(ViewActions.click())330 Thread.sleep(5000)331 Espresso.onView(ViewMatchers.withId(R.id.main_OpenMenu))332 .perform(ViewActions.click())333 Espresso.onView(ViewMatchers.withId(R.id.main_Transfer))334 .perform(ViewActions.click())335 Thread.sleep(500)336 Espresso.onView(ViewMatchers.withId(R.id.transferHistory_listView))337 .check(matches(withListSize(2)))338 Espresso.onData(anything())339 .inAdapterView(withId(R.id.transferHistory_listView))340 .atPosition(1)341 .onChildView(withId(R.id.transferHistory_DeleteButton))342 .perform(ViewActions.click())343 Thread.sleep(250)344 Espresso.onView(ViewMatchers.withId(R.id.transferHistory_listView))345 .check(matches(withListSize(1)))346 Espresso.onData(anything())347 .inAdapterView(withId(R.id.transferHistory_listView))348 .atPosition(0)349 .onChildView(withId(R.id.transferHistory_DeleteAllButton))350 .perform(ViewActions.click())351 Thread.sleep(250)352 Espresso.onView(ViewMatchers.withId(R.id.transferHistory_listView))353 .check(matches(withListSize(0)))354 Espresso.pressBack()355 Espresso.onView(withId(R.id.main_OpenMenu))356 .perform(ViewActions.click())357 Espresso.onView(withId(R.id.main_Bank))358 .perform(ViewActions.click())359 Espresso.onView(withId(R.id.bank_GOLDvalue))360 .check(matches(not(withText("567.8"))))361 Espresso.onView(withId(R.id.bank_GOLDvalue))362 .check(matches(not(withText("0.0"))))363 Espresso.pressBack()364 Espresso.onView(withId(R.id.main_OpenMenu))365 .perform(ViewActions.click())366 Espresso.onView(withId(R.id.main_SignOut))...

Full Screen

Full Screen

MainActivityTest2.kt

Source:MainActivityTest2.kt Github

copy

Full Screen

...6package powerball.apps.jacs.powerball7import android.support.test.InstrumentationRegistry8import android.support.test.espresso.Espresso9import android.support.test.espresso.action.ViewActions10import android.support.test.espresso.matcher.ViewMatchers11import android.support.test.filters.LargeTest12import android.support.test.rule.ActivityTestRule13import android.support.test.runner.AndroidJUnit414import android.view.View15import android.view.ViewGroup16import org.hamcrest.Description17import org.hamcrest.Matcher18import org.hamcrest.Matchers19import org.hamcrest.TypeSafeMatcher20import org.junit.Rule21import org.junit.Test22import org.junit.runner.RunWith23@LargeTest24@RunWith(AndroidJUnit4::class)25class MainActivityTest2 {26 @Rule27 var mActivityTestRule = ActivityTestRule(MainActivity::class.java)28 @Test29 fun mainActivityTest2() {30 val viewPager = Espresso.onView(31 Matchers.allOf(ViewMatchers.withId(R.id.pager1),32 childAtPosition(33 Matchers.allOf(ViewMatchers.withId(R.id.activity_main),34 childAtPosition(35 ViewMatchers.withId(R.id.content_frame),36 0)),37 1),38 ViewMatchers.isDisplayed()))39 viewPager.perform(ViewActions.swipeLeft())40 val viewPager2 = Espresso.onView(41 Matchers.allOf(ViewMatchers.withId(R.id.pager1),42 childAtPosition(43 Matchers.allOf(ViewMatchers.withId(R.id.activity_main),44 childAtPosition(45 ViewMatchers.withId(R.id.content_frame),46 0)),47 1),48 ViewMatchers.isDisplayed()))49 viewPager2.perform(ViewActions.swipeRight())50 Espresso.openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getInstrumentation().targetContext)51 val appCompatTextView = Espresso.onView(52 Matchers.allOf(ViewMatchers.withId(R.id.title), ViewMatchers.withText("Settings"),53 childAtPosition(54 childAtPosition(55 ViewMatchers.withId(R.id.content),56 0),57 0),58 ViewMatchers.isDisplayed()))59 appCompatTextView.perform(ViewActions.click())60 val floatingActionButton = Espresso.onView(61 Matchers.allOf(ViewMatchers.withId(R.id.fab),62 childAtPosition(63 childAtPosition(64 ViewMatchers.withId(R.id.drawer_layout),65 0),66 2),67 ViewMatchers.isDisplayed()))68 floatingActionButton.perform(ViewActions.click())69 val snackbarLayout = Espresso.onView(70 Matchers.allOf(childAtPosition(71 childAtPosition(72 ViewMatchers.withId(R.id.drawer_layout),73 0),74 3),75 ViewMatchers.isDisplayed()))76 snackbarLayout.perform(ViewActions.click())77 val appCompatImageButton = Espresso.onView(78 Matchers.allOf(ViewMatchers.withContentDescription("Open navigation drawer"),79 childAtPosition(80 Matchers.allOf(ViewMatchers.withId(R.id.toolbar),81 childAtPosition(82 ViewMatchers.withClassName(Matchers.`is`("android.support.design.widget.AppBarLayout")),83 0)),84 1),85 ViewMatchers.isDisplayed()))86 appCompatImageButton.perform(ViewActions.click())87 val navigationMenuItemView = Espresso.onView(88 Matchers.allOf(childAtPosition(89 Matchers.allOf(ViewMatchers.withId(R.id.design_navigation_view),90 childAtPosition(91 ViewMatchers.withId(R.id.nav_view),92 0)),93 1),94 ViewMatchers.isDisplayed()))95 navigationMenuItemView.perform(ViewActions.click())96 val appCompatImageButton2 = Espresso.onView(97 Matchers.allOf(ViewMatchers.withContentDescription("Open navigation drawer"),98 childAtPosition(99 Matchers.allOf(ViewMatchers.withId(R.id.toolbar),100 childAtPosition(101 ViewMatchers.withClassName(Matchers.`is`("android.support.design.widget.AppBarLayout")),102 0)),103 1),104 ViewMatchers.isDisplayed()))105 appCompatImageButton2.perform(ViewActions.click())106 val navigationMenuItemView2 = Espresso.onView(107 Matchers.allOf(childAtPosition(108 Matchers.allOf(ViewMatchers.withId(R.id.design_navigation_view),109 childAtPosition(110 ViewMatchers.withId(R.id.nav_view),111 0)),112 3),113 ViewMatchers.isDisplayed()))114 navigationMenuItemView2.perform(ViewActions.click())115 val appCompatImageButton3 = Espresso.onView(116 Matchers.allOf(ViewMatchers.withContentDescription("Open navigation drawer"),117 childAtPosition(118 Matchers.allOf(ViewMatchers.withId(R.id.toolbar),119 childAtPosition(120 ViewMatchers.withClassName(Matchers.`is`("android.support.design.widget.AppBarLayout")),121 0)),122 1),123 ViewMatchers.isDisplayed()))124 appCompatImageButton3.perform(ViewActions.click())125 val navigationMenuItemView3 = Espresso.onView(126 Matchers.allOf(childAtPosition(127 Matchers.allOf(ViewMatchers.withId(R.id.design_navigation_view),128 childAtPosition(129 ViewMatchers.withId(R.id.nav_view),130 0)),131 2),132 ViewMatchers.isDisplayed()))133 navigationMenuItemView3.perform(ViewActions.click())134 val appCompatImageButton1 = Espresso.onView(135 Matchers.allOf(ViewMatchers.withContentDescription("Open navigation drawer"),136 childAtPosition(137 Matchers.allOf(ViewMatchers.withId(R.id.toolbar),138 childAtPosition(139 ViewMatchers.withClassName(Matchers.`is`("android.support.design.widget.AppBarLayout")),140 0)),141 1),142 ViewMatchers.isDisplayed()))143 appCompatImageButton1.perform(ViewActions.click())144 Espresso.pressBack()145 }146 companion object {147 private fun childAtPosition(148 parentMatcher: Matcher<View>, position: Int): Matcher<View> {149 return object : TypeSafeMatcher<View>() {150 override fun describeTo(description: Description) {151 description.appendText("Child at position $position in parent ")152 parentMatcher.describeTo(description)153 }154 public override fun matchesSafely(view: View): Boolean {155 val parent = view.parent156 return (parent is ViewGroup && parentMatcher.matches(parent)...

Full Screen

Full Screen

BaseRobot.kt

Source:BaseRobot.kt Github

copy

Full Screen

...6import android.support.test.espresso.ViewInteraction7import android.support.test.espresso.action.ViewActions8import android.support.test.espresso.assertion.ViewAssertions9import android.support.test.espresso.contrib.RecyclerViewActions10import android.support.test.espresso.matcher.ViewMatchers11import android.support.test.uiautomator.UiDevice12import android.support.v7.widget.RecyclerView13import android.view.View14import android.view.ViewGroup15import com.robotsandpencils.kotlindaggerexperiement.R16import org.hamcrest.*17abstract class BaseRobot {18 fun childAtPosition(parentMatcher: Matcher<View>, position: Int): Matcher<View> {19 return object : TypeSafeMatcher<View>() {20 override fun describeTo(description: Description) {21 description.appendText("Child at position $position in parent ")22 parentMatcher.describeTo(description)23 }24 public override fun matchesSafely(view: View): Boolean {25 val parent = view.parent26 return (parent is ViewGroup && parentMatcher.matches(parent)27 && view == parent.getChildAt(position))28 }29 }30 }31 fun assertText(@IdRes id: Int, message: String): ViewInteraction =32 Espresso.onView(CoreMatchers.allOf(ViewMatchers.withId(id), ViewMatchers.isDisplayed()))33 .check(ViewAssertions.matches(ViewMatchers.withText(message)))34 fun componentInList(component: Matcher<View>, list: Matcher<View>, index: Int): ViewInteraction {35 return Espresso.onView(Matchers.allOf(component, ViewMatchers.isDescendantOfA(childAtPosition(list, index))))36 }37 fun componentInTagged(component: Matcher<View>, tagValue: Matcher<Any>): ViewInteraction {38 return Espresso.onView(Matchers.allOf(component, ViewMatchers.isDescendantOfA(ViewMatchers.withTagValue(tagValue))))39 }40 fun componentInTagged(component: Matcher<View>, key: Int, tagValue: Matcher<Any>): ViewInteraction {41 return Espresso.onView(Matchers.allOf(component, ViewMatchers.isDescendantOfA(ViewMatchers.withTagKey(key, tagValue))))42 }43 fun scrollTo(list: Matcher<View>, item: Matcher<View>) {44 Espresso.onView(list).perform(RecyclerViewActions.scrollTo<RecyclerView.ViewHolder>(item))45 }46 fun scrollTo(item: Matcher<View>) {47 scrollTo(ViewMatchers.withId(R.id.list), item)48 }49 fun scrollToPosition(list: Matcher<View>, position: Int) {50 Espresso.onView(list).perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(position))51 }52 fun scrollToPosition(position: Int) {53 scrollToPosition(ViewMatchers.withId(R.id.list), position)54 }55 fun getTextByTag(@IdRes id: Int, tag: String) = componentInTagged(ViewMatchers.withId(id), R.id.tag_test, Matchers.equalTo(tag))56 fun assertDialogText(title: Matcher<String>, message: Matcher<String>) {57 Espresso.onView(ViewMatchers.withId(R.id.md_title)).check(ViewAssertions.matches(ViewMatchers.withText(title)))58 Espresso.onView(ViewMatchers.withId(R.id.md_content)).check(ViewAssertions.matches(ViewMatchers.withText(message)))59 }60 fun assertDialogText(title: Int, message: Matcher<String>) {61 Espresso.onView(ViewMatchers.withId(R.id.md_title)).check(ViewAssertions.matches(ViewMatchers.withText(title)))62 Espresso.onView(ViewMatchers.withId(R.id.md_content)).check(ViewAssertions.matches(ViewMatchers.withText(message)))63 }64 fun assertDialogText(title: Int, message: Int) {65 Espresso.onView(ViewMatchers.withId(R.id.md_title)).check(ViewAssertions.matches(ViewMatchers.withText(title)))66 Espresso.onView(ViewMatchers.withId(R.id.md_content)).check(ViewAssertions.matches(ViewMatchers.withText(message)))67 }68 fun assertNeutralText(text: Matcher<String>) {69 Espresso.onView(ViewMatchers.withId(R.id.md_buttonDefaultNeutral)).check(ViewAssertions.matches(ViewMatchers.withText(text)))70 }71 fun assertNeutralNotVisible() {72 Espresso.onView(ViewMatchers.withId(R.id.md_buttonDefaultNeutral)).check(ViewAssertions.matches(Matchers.not(ViewMatchers.isDisplayed())))73 }74 fun clickDialogPositive() {75 clickButton(R.id.md_buttonDefaultPositive)76 }77 fun clickDialogNeutral() {78 clickButton(R.id.md_buttonDefaultNeutral)79 }80 fun clickDialogNegative() {81 clickButton(R.id.md_buttonDefaultNegative)82 }83 fun clickPromptCheckbox() {84 clickButton(R.id.md_promptCheckbox)85 }86 fun clickButton(@IdRes id: Int) {87 Espresso.onView(ViewMatchers.withId(id)).perform(ViewActions.click())88 }89 fun sleep(ms: Long) {90 SystemClock.sleep(ms)91 }92 fun pressBack() {93 Espresso.closeSoftKeyboard()94 val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())95 mDevice.pressBack()96 }97}...

Full Screen

Full Screen

ViewMatchersTest.kt

Source:ViewMatchersTest.kt Github

copy

Full Screen

...16import android.support.test.espresso.matcher.PreferenceMatchers.withTitle17import android.support.test.espresso.matcher.RootMatchers.isDialog18import android.support.test.espresso.matcher.RootMatchers.isPlatformPopup19import android.support.test.espresso.matcher.RootMatchers.isTouchable20import android.support.test.espresso.matcher.ViewMatchers.hasContentDescription21import android.support.test.espresso.matcher.ViewMatchers.hasDescendant22import 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())...

Full Screen

Full Screen

TestSkenario.kt

Source:TestSkenario.kt Github

copy

Full Screen

2import android.support.test.espresso.Espresso3import android.support.test.espresso.action.ViewActions4import android.support.test.espresso.assertion.ViewAssertions5import android.support.test.espresso.contrib.RecyclerViewActions6import android.support.test.espresso.matcher.ViewMatchers7import android.support.test.rule.ActivityTestRule8import android.support.test.runner.AndroidJUnit49import android.support.v7.widget.RecyclerView10import android.view.View11import android.view.ViewGroup12import com.iav.kade.activity.HomeActivity13import com.iav.kade.R.id.add_to_favorite14import com.iav.kade.R.id.fav15import org.hamcrest.Description16import org.hamcrest.Matcher17import org.hamcrest.Matchers18import org.hamcrest.TypeSafeMatcher19import org.junit.Rule20import org.junit.Test21import org.junit.runner.RunWith22@RunWith(AndroidJUnit4::class)23class TestSkenario {24 @Rule25 @JvmField26 var activityRule = ActivityTestRule(HomeActivity::class.java)27 @Test28 fun behaviorTest(){29 Thread.sleep(3000)30 val recyclerView = Espresso.onView(31 Matchers.allOf(ViewMatchers.withId(R.id.rv),32 ViewMatchers.isDisplayed()))33 recyclerView.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))34 recyclerView35 .perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(8))36 .perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(7, ViewActions.click()))37 Thread.sleep(3000)38 Espresso.onView(ViewMatchers.withId(add_to_favorite)).perform(ViewActions.click())39 Espresso.pressBack()40 Espresso.onView(ViewMatchers.withId(fav)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()))41 .perform(ViewActions.click())42 recyclerView.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))43 recyclerView44 .perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(0, ViewActions.click()))45 Espresso.onView(ViewMatchers.withId(add_to_favorite)).perform(ViewActions.click())46 Espresso.pressBack()47 Thread.sleep(1000)48 val linearLayout3 = Espresso.onView(49 Matchers.allOf(childAtPosition(50 childAtPosition(51 ViewMatchers.withId(R.id.rv),52 0),53 0),54 ViewMatchers.isDisplayed()))55 linearLayout3.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))56 }57 private fun childAtPosition(58 parentMatcher: Matcher<View>, position: Int): Matcher<View> {59 return object : TypeSafeMatcher<View>() {60 override fun describeTo(description: Description) {61 description.appendText("Child at position $position in parent ")62 parentMatcher.describeTo(description)63 }64 public override fun matchesSafely(view: View): Boolean {65 val parent = view.parent66 return (parent is ViewGroup && parentMatcher.matches(parent)67 && view == parent.getChildAt(position))68 }69 }...

Full Screen

Full Screen

LoginActivityTest.kt

Source:LoginActivityTest.kt Github

copy

Full Screen

1package com.example.szymon.coinz2import android.support.test.espresso.Espresso3import android.support.test.espresso.action.ViewActions4import android.support.test.espresso.assertion.ViewAssertions.matches5import android.support.test.espresso.matcher.ViewMatchers6import android.support.test.espresso.matcher.ViewMatchers.hasErrorText7import android.support.test.filters.LargeTest8import android.support.test.rule.ActivityTestRule9import android.support.test.rule.GrantPermissionRule10import android.support.test.runner.AndroidJUnit411import org.junit.Rule12import org.junit.Test13import org.junit.runner.RunWith14@LargeTest15@RunWith(AndroidJUnit4::class)16class LoginActivityTest {17 @Rule18 @JvmField19 var mActivityTestRule = ActivityTestRule(LoginActivity::class.java)20 @Rule21 @JvmField22 var mRuntimePermissionRule = GrantPermissionRule23 .grant(android.Manifest.permission.ACCESS_FINE_LOCATION)24 @Test25 fun loginActivityTestNoCredentials() {26 Espresso.onView(ViewMatchers.withId(R.id.email_sign_in_button))27 .perform(ViewActions.click())28 Espresso.onView(ViewMatchers.withId(R.id.login_email))29 .check(matches(hasErrorText("This field is required")))30 }31 @Test32 fun loginActivityTestNoPassword() {33 Espresso.onView(ViewMatchers.withId(R.id.login_email))34 .perform(ViewActions.replaceText("EspressoEmail@gmail.com"))35 Espresso.onView(ViewMatchers.withId(R.id.email_sign_in_button))36 .perform(ViewActions.click())37 Espresso.onView(ViewMatchers.withId(R.id.login_password))38 .check(matches(hasErrorText("This password is too short")))39 }40 @Test41 fun loginActivityInvalidEmail() {42 Espresso.onView(ViewMatchers.withId(R.id.login_email))43 .perform(ViewActions.replaceText("EspressoEmailgmail.com"))44 Espresso.onView(ViewMatchers.withId(R.id.email_sign_in_button))45 .perform(ViewActions.click())46 Espresso.onView(ViewMatchers.withId(R.id.login_email))47 .check(matches(hasErrorText("This email address is invalid")))48 }49 @Test50 fun loginActivityValidLogin() {51 Espresso.onView(ViewMatchers.withId(R.id.login_email))52 .perform(ViewActions.replaceText("EspressoEmail@gmail.com"))53 Espresso.onView(ViewMatchers.withId(R.id.login_password))54 .perform(ViewActions.replaceText("EspressoPassword"))55 Espresso.onView(ViewMatchers.withId(R.id.email_sign_in_button))56 .perform(ViewActions.click())57 Thread.sleep(5000)58 }59}...

Full Screen

Full Screen

ExampleInstrumentedTest.kt

Source:ExampleInstrumentedTest.kt Github

copy

Full Screen

...3import android.content.Context4import android.content.Intent5import android.support.test.InstrumentationRegistry6import android.support.test.espresso.ViewAction7import android.support.test.espresso.matcher.ViewMatchers8import android.support.test.rule.ActivityTestRule9import android.support.test.runner.AndroidJUnit410import org.junit.Rule11import org.junit.Test12import org.junit.runner.RunWith13import example.com.shivangigotawalatest.Activity.MainActivity14import android.support.test.espresso.Espresso.onView15import android.support.test.espresso.action.ViewActions.click16import android.support.test.espresso.assertion.ViewAssertions.matches17import android.support.test.espresso.matcher.ViewMatchers.hasDescendant18import android.support.test.espresso.matcher.ViewMatchers.isDisplayed19import android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility20import android.support.test.espresso.matcher.ViewMatchers.withId21import android.support.test.espresso.matcher.ViewMatchers.withText22import org.hamcrest.core.AllOf.allOf23import org.junit.Assert.*24/**25 * Instrumented test, which will execute on an Android device.26 *27 * @see [Testing documentation](http://d.android.com/tools/testing)28 */29@RunWith(AndroidJUnit4::class)30class ExampleInstrumentedTest {31 @Rule32 var mActivityRule = ActivityTestRule(MainActivity::class.java)33 @Test34 fun testViews() {35 onView(withId(R.id.tv_name)).check(matches(isDisplayed()))...

Full Screen

Full Screen

SearchActivityInstrumentedTest.kt

Source:SearchActivityInstrumentedTest.kt Github

copy

Full Screen

...3import org.junit.Test4import org.junit.runner.RunWith5import android.support.test.espresso.Espresso.onView6import android.support.test.espresso.assertion.ViewAssertions.matches7import android.support.test.espresso.matcher.ViewMatchers8import android.support.test.espresso.matcher.ViewMatchers.withId9import android.support.test.espresso.matcher.ViewMatchers.withText10import android.support.test.rule.ActivityTestRule11import com.example.sky.helper.TestUtils12import com.example.sky.search.SearchActivity13import org.junit.Rule14import android.support.test.espresso.matcher.ViewMatchers.withParent15import android.widget.TextView16import junit.framework.Assert.assertEquals17import org.hamcrest.CoreMatchers.allOf18import org.hamcrest.CoreMatchers.instanceOf19@RunWith(AndroidJUnit4::class)20class SearchActivityInstrumentedTest {21 @Suppress("unused") // actually used by Espresso22 @get:Rule23 val activityRule = ActivityTestRule<SearchActivity>(SearchActivity::class.java)24 @Test25 fun toolbarShouldBeShown() {26 onView(withText("Edinburgh - London")).check(matches(ViewMatchers.isDisplayed()))27 onView(withText("19 Feb - 20 Feb, 1 adult, Economy")).check(matches(ViewMatchers.isDisplayed()))28 }29}...

Full Screen

Full Screen

ViewMatchers

Using AI Code Generation

copy

Full Screen

1onView(withText(“Hello World!”)).perform(click());2onView(withId(R.id.button)).perform(click());3onView(withId(R.id.textView)).check(matches(withText(“Hello World!”)));4onView(withText(“Hello World!”)).perform(click());5onView(withId(R.id.button)).perform(click());6onView(withId(R.id.textView)).check(matches(withText(“Hello World!”)));7onView(withText(“Hello World!”)).perform(click());8onView(withId(R.id.button)).perform(click());9onView(withId(R.id.textView)).check(matches(withText(“Hello World!”)));10onView(withText(“Hello World!”)).perform(click());11onView(withId(R.id.button)).perform(click());12onView(withId(R.id.textView)).check(matches(withText(“Hello World!”)));

Full Screen

Full Screen

ViewMatchers

Using AI Code Generation

copy

Full Screen

1ViewInteraction appCompatEditText = onView(2allOf(withId(R.id.editText), isDisplayed()));3appCompatEditText.perform(replaceText("Hello"), closeSoftKeyboard());4ViewInteraction appCompatButton = onView(5allOf(withId(R.id.button), withText("Click"), isDisplayed()));6appCompatButton.perform(click());7ViewInteraction textView = onView(8allOf(withId(R.id.textView), withText("Hello"), isDisplayed()));9textView.check(matches(withText("Hello")));10}11}12In the above code, we have used the Espresso library to test the UI of the application. Espresso tests are written in Java and are executed on the device or emulator. Espresso test code is very easy to write and maintain. It is also very fast. Espresso tests are written as JUnit tests, which means they can be run with other JUnit tests in the same test suite. Here we have used the onView() method of the Espresso class to find the view in the current view hierarchy. It is used to find the view by its resource id. It also has the method to find the view by the text of the view. It also has the method to find the view by the content description of the view. The onView() method returns the ViewInteraction object. The ViewInteraction object is used to perform the action on the view. The ViewInteraction object has the method to perform the click action on the view. It also has the method to perform the long click action on the view. It also has the method to perform the double click action on the view. It also has the method to perform the swipe action on the view. It also has the method to perform the scroll action on the view. It also has the method to perform the type action on the view. It also has the method to perform the replace text action on the view. It also has the method to perform the clear text action on the view. It also has the method to perform the close soft keyboard action on the view. It also has the method

Full Screen

Full Screen

ViewMatchers

Using AI Code Generation

copy

Full Screen

1dependencies {2}31. withId()4ViewInteraction view = onView(withId(R.id.button1));52. withText()6ViewInteraction view = onView(withText("Button 1"));73. withContentDescription()8ViewInteraction view = onView(withContentDescription("Button 1"));94. withClassName()10ViewInteraction view = onView(withClassName(Matchers.equalTo(Button.class.getName())));115. withTagValue()12ViewInteraction view = onView(withTagValue(Matchers.is("Button 1")));136. isAssignableFrom()14ViewInteraction view = onView(isAssignableFrom(Button.class));157. withParent()

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful