Best Appium-espresso-driver code snippet using android.support.test.espresso.Espresso.onView
NavigationScreenTest.kt
Source:NavigationScreenTest.kt
1package com.socialpub.rahul.ui.onboarding2import android.view.View3import android.view.ViewGroup4import androidx.test.espresso.Espresso.onView5import androidx.test.espresso.action.ViewActions.click6import androidx.test.espresso.assertion.ViewAssertions.matches7import androidx.test.espresso.contrib.DrawerActions8import androidx.test.espresso.matcher.ViewMatchers.*9import androidx.test.filters.LargeTest10import androidx.test.rule.ActivityTestRule11import androidx.test.runner.AndroidJUnit412import com.socialpub.rahul.R13import org.hamcrest.Description14import org.hamcrest.Matcher15import org.hamcrest.Matchers.`is`16import org.hamcrest.Matchers.allOf17import org.hamcrest.TypeSafeMatcher18import org.hamcrest.core.IsInstanceOf19import org.junit.Rule20import org.junit.Test21import org.junit.runner.RunWith22@LargeTest23@RunWith(AndroidJUnit4::class)24class NavigationScreenTest {25 @Rule26 @JvmField27 var mActivityTestRule = ActivityTestRule(OnboardingActivity::class.java)28 @Test29 fun navigationScreenTest() {30 // Added a sleep statement to match the app's execution delay.31 // The recommended way to handle such scenarios is to use Espresso idling resources:32 // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html33 Thread.sleep(7000)34 onView(withId(R.id.drawer_container_home))35 .perform(DrawerActions.open());36 // Added a sleep statement to match the app's execution delay.37 // The recommended way to handle such scenarios is to use Espresso idling resources:38 // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html39 Thread.sleep(1500)40 val imageView = onView(41 allOf(42 withId(R.id.image_side_nav_home_useravatar),43 childAtPosition(44 childAtPosition(45 withId(R.id.navigation_header_container),46 047 ),48 049 ),50 isDisplayed()51 )52 )53 imageView.check(matches(isDisplayed()))54 onView(withId(R.id.drawer_container_home))55 .perform(DrawerActions.open());56 Thread.sleep(1500)57 val navigationMenuItemView = onView(58 allOf(59 childAtPosition(60 allOf(61 withId(R.id.design_navigation_view),62 childAtPosition(63 withId(R.id.side_drawer_home),64 065 )66 ),67 168 ),69 isDisplayed()70 )71 )72 navigationMenuItemView.perform(click())73 // Added a sleep statement to match the app's execution delay.74 // The recommended way to handle such scenarios is to use Espresso idling resources:75 // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html76 Thread.sleep(1000)77 val textView = onView(78 allOf(79 withText("Notifications"),80 childAtPosition(81 childAtPosition(82 IsInstanceOf.instanceOf(androidx.appcompat.widget.LinearLayoutCompat::class.java),83 084 ),85 086 ),87 isDisplayed()88 )89 )90 textView.check(matches(withText("Notifications")))91 val materialButton = onView(92 allOf(93 withId(R.id.btn_close),94 childAtPosition(95 childAtPosition(96 withClassName(`is`("androidx.appcompat.widget.LinearLayoutCompat")),97 098 ),99 1100 ),101 isDisplayed()102 )103 )104 materialButton.perform(click())105 onView(withId(R.id.drawer_container_home))106 .perform(DrawerActions.open());107 Thread.sleep(1500)108 val navigationMenuItemView2 = onView(109 allOf(110 childAtPosition(111 allOf(112 withId(R.id.design_navigation_view),113 childAtPosition(114 withId(R.id.side_drawer_home),115 0116 )117 ),118 2119 ),120 isDisplayed()121 )122 )123 navigationMenuItemView2.perform(click())124 // Added a sleep statement to match the app's execution delay.125 // The recommended way to handle such scenarios is to use Espresso idling resources:126 // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html127 Thread.sleep(1000)128 val button = onView(129 allOf(130 withId(R.id.btn_multi_delete),131 childAtPosition(132 allOf(133 withId(R.id.container_multiselect),134 childAtPosition(135 IsInstanceOf.instanceOf(androidx.appcompat.widget.LinearLayoutCompat::class.java),136 2137 )138 ),139 0140 ),141 isDisplayed()142 )143 )144 button.check(matches(isDisplayed()))145 val appCompatImageButton = onView(146 allOf(147 withContentDescription("Navigate up"),148 childAtPosition(149 allOf(150 withId(R.id.toolbar_fav),151 childAtPosition(152 withClassName(`is`("androidx.appcompat.widget.LinearLayoutCompat")),153 0154 )155 ),156 1157 ),158 isDisplayed()159 )160 )161 appCompatImageButton.perform(click())162 // Added a sleep statement to match the app's execution delay.163 // The recommended way to handle such scenarios is to use Espresso idling resources:164 // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html165 onView(withId(R.id.drawer_container_home))166 .perform(DrawerActions.open());167 Thread.sleep(1500)168 val navigationMenuItemView3 = onView(169 allOf(170 childAtPosition(171 allOf(172 withId(R.id.design_navigation_view),173 childAtPosition(174 withId(R.id.side_drawer_home),175 0176 )177 ),178 3179 ),180 isDisplayed()181 )182 )183 navigationMenuItemView3.perform(click())184 // Added a sleep statement to match the app's execution delay.185 // The recommended way to handle such scenarios is to use Espresso idling resources:186 // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html187 Thread.sleep(1000)188 val button2 = onView(189 allOf(190 withId(R.id.btn_following),191 childAtPosition(192 childAtPosition(193 IsInstanceOf.instanceOf(androidx.appcompat.widget.LinearLayoutCompat::class.java),194 1195 ),196 0197 ),198 isDisplayed()199 )200 )201 Thread.sleep(1000)202 button2.check(matches(isDisplayed()))203 val button3 = onView(204 allOf(205 withId(R.id.btn_followers),206 childAtPosition(207 childAtPosition(208 IsInstanceOf.instanceOf(androidx.appcompat.widget.LinearLayoutCompat::class.java),209 1210 ),211 1212 ),213 isDisplayed()214 )215 )216 button3.check(matches(isDisplayed()))217 val appCompatImageButton2 = onView(218 allOf(219 withContentDescription("Navigate up"),220 childAtPosition(221 allOf(222 withId(R.id.toolbar_followers),223 childAtPosition(224 withClassName(`is`("androidx.appcompat.widget.LinearLayoutCompat")),225 0226 )227 ),228 1229 ),230 isDisplayed()231 )232 )233 appCompatImageButton2.perform(click())234 // Added a sleep statement to match the app's execution delay.235 // The recommended way to handle such scenarios is to use Espresso idling resources:236 // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html237 onView(withId(R.id.drawer_container_home))238 .perform(DrawerActions.open());239 Thread.sleep(1500)240 val navigationMenuItemView4 = onView(241 allOf(242 childAtPosition(243 allOf(244 withId(R.id.design_navigation_view),245 childAtPosition(246 withId(R.id.side_drawer_home),247 0248 )249 ),250 5251 ),252 isDisplayed()253 )254 )255 navigationMenuItemView4.perform(click())256 // Added a sleep statement to match the app's execution delay.257 // The recommended way to handle such scenarios is to use Espresso idling resources:258 // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html259 Thread.sleep(1000)260 val switch_ = onView(261 allOf(262 withId(R.id.switch_location_search),263 childAtPosition(264 childAtPosition(265 IsInstanceOf.instanceOf(androidx.appcompat.widget.LinearLayoutCompat::class.java),266 1267 ),268 1269 ),270 isDisplayed()271 )272 )273 switch_.check(matches(isDisplayed()))274 val switch_2 = onView(275 allOf(276 withId(R.id.switch_email_search),277 childAtPosition(278 childAtPosition(279 IsInstanceOf.instanceOf(androidx.appcompat.widget.LinearLayoutCompat::class.java),280 2281 ),282 1283 ),284 isDisplayed()285 )286 )287 switch_2.check(matches(isDisplayed()))288 val appCompatImageButton3 = onView(289 allOf(290 withContentDescription("Navigate up"),291 childAtPosition(292 allOf(293 withId(R.id.toolbar_settings),294 childAtPosition(295 withClassName(`is`("androidx.appcompat.widget.LinearLayoutCompat")),296 0297 )298 ),299 1300 ),301 isDisplayed()302 )303 )304 appCompatImageButton3.perform(click())305 // Added a sleep statement to match the app's execution delay.306 // The recommended way to handle such scenarios is to use Espresso idling resources:307 // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html308 onView(withId(R.id.drawer_container_home))309 .perform(DrawerActions.open());310 Thread.sleep(1500)311 val navigationMenuItemView5 = onView(312 allOf(313 childAtPosition(314 allOf(315 withId(R.id.design_navigation_view),316 childAtPosition(317 withId(R.id.side_drawer_home),318 0319 )320 ),321 4322 ),323 isDisplayed()324 )325 )326 navigationMenuItemView5.perform(click())327 // Added a sleep statement to match the app's execution delay.328 // The recommended way to handle such scenarios is to use Espresso idling resources:329 // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html330 Thread.sleep(1000)331 val button4 = onView(332 allOf(333 withId(R.id.btn_update_profile),334 childAtPosition(335 childAtPosition(336 withId(android.R.id.content),337 0338 ),339 1340 ),341 isDisplayed()342 )343 )344 button4.check(matches(isDisplayed()))345 val appCompatImageButton4 = onView(346 allOf(347 withContentDescription("Navigate up"),348 childAtPosition(349 allOf(350 withId(R.id.toolbar_profile),351 childAtPosition(352 withClassName(`is`("androidx.appcompat.widget.LinearLayoutCompat")),353 0354 )355 ),356 1357 ),358 isDisplayed()359 )360 )361 appCompatImageButton4.perform(click())362 // Added a sleep statement to match the app's execution delay.363 // The recommended way to handle such scenarios is to use Espresso idling resources:364 // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html365 onView(withId(R.id.drawer_container_home))366 .perform(DrawerActions.open());367 Thread.sleep(1500)368 val navigationMenuItemView6 = onView(369 allOf(370 childAtPosition(371 allOf(372 withId(R.id.design_navigation_view),373 childAtPosition(374 withId(R.id.side_drawer_home),375 0376 )377 ),378 6379 ),380 isDisplayed()381 )382 )...
UIRegisterTest.kt
Source:UIRegisterTest.kt
1package com.lennertbontinck.carmeetsandroidapp.ui2import android.os.SystemClock3import android.support.test.espresso.Espresso.onView4import android.support.test.espresso.action.ViewActions5import android.support.test.espresso.action.ViewActions.click6import android.support.test.espresso.assertion.ViewAssertions.matches7import android.support.test.espresso.matcher.RootMatchers8import android.support.test.espresso.matcher.ViewMatchers.*9import android.support.test.rule.ActivityTestRule10import com.lennertbontinck.carmeetsandroidapp.R11import com.lennertbontinck.carmeetsandroidapp.activities.MainActivity12import com.lennertbontinck.carmeetsandroidapp.utils.PreferenceUtil13import org.hamcrest.CoreMatchers14import org.junit.After15import org.junit.Before16import org.junit.Rule17import org.junit.Test18import org.junit.runner.RunWith19import org.junit.runners.JUnit420import java.util.*21/**22 * UI tests voor registreer verloop.23 */24@RunWith(JUnit4::class)25class UIRegisterTest {26 private var username = "androidtestuser"27 private var tokenUsername = "admin"28 private var email = "androidtestuser@lennertbontinck.com"29 private var tokenEmail = "info@lennertbontinck.com"30 private var password = "Password123"31 @Rule32 @JvmField33 var mActivityTestRule = ActivityTestRule(MainActivity::class.java, false, false)34 @Before35 fun beforeTests() {36 //verwijder shared preferences37 PreferenceUtil.deletePreferences()38 mActivityTestRule.launchActivity(null)39 //wacht even voor het laden van de data40 //server zou actief moeten zijn vooraleer testen uitgevoerd worden om onnodig lange wachttijden te vermijden41 SystemClock.sleep(1000)42 val uuid = UUID.randomUUID()43 username = uuid.toString()44 email = uuid.toString() + "@lennertbontinck.com"45 //klik account46 onView(withId(R.id.nav_account)).perform(click())47 //kijk login fragment zichtbaar48 onView(withId(R.id.fragment_login)).check(matches(isDisplayed()))49 //klik op register50 onView(withId(R.id.button_login_register)).perform(click())51 //kijk register fragment zichtbaar52 onView(withId(R.id.fragment_register)).check(matches(isDisplayed()))53 }54 @After55 fun afterTests() {56 PreferenceUtil.deletePreferences()57 }58 @Test59 fun register_allFieldsEmpty_showToastEmptyFields() {60 //druk direct op registreer61 onView(withId(R.id.button_register_confirm)).perform(click())62 //kijk empty fields toast63 onView(withText(R.string.warning_empty_fields)).inRoot(64 RootMatchers.withDecorView(65 CoreMatchers.not(66 CoreMatchers.`is`(67 mActivityTestRule.activity.window.decorView68 )69 )70 )71 ).check(matches(isDisplayed()))72 }73 @Test74 fun register_someFieldsEmpty_showToastEmptyFields() {75 //vul email in76 onView(withId(R.id.text_register_email)).perform(ViewActions.typeText(email))77 android.support.test.espresso.Espresso.closeSoftKeyboard()78 //vul username in79 onView(withId(R.id.text_register_username)).perform(ViewActions.typeText(username))80 android.support.test.espresso.Espresso.closeSoftKeyboard()81 //druk op registreer82 onView(withId(R.id.button_register_confirm)).perform(click())83 //kijk empty fields toast84 onView(withText(R.string.warning_empty_fields)).inRoot(85 RootMatchers.withDecorView(86 CoreMatchers.not(87 CoreMatchers.`is`(88 mActivityTestRule.activity.window.decorView89 )90 )91 )92 ).check(matches(isDisplayed()))93 }94 @Test95 fun register_passwordsDoNotMatch_showToastPasswordsNotEqual() {96 //vul email in97 onView(withId(R.id.text_register_email)).perform(ViewActions.typeText(email))98 android.support.test.espresso.Espresso.closeSoftKeyboard()99 //vul username in100 onView(withId(R.id.text_register_username)).perform(ViewActions.typeText(username))101 android.support.test.espresso.Espresso.closeSoftKeyboard()102 //vul ww in103 onView(withId(R.id.text_register_password)).perform(ViewActions.typeText(password))104 android.support.test.espresso.Espresso.closeSoftKeyboard()105 //vul confirm ww in106 onView(withId(R.id.text_register_confirm_password)).perform(ViewActions.typeText(password + "notEqual"))107 android.support.test.espresso.Espresso.closeSoftKeyboard()108 //druk op registreer109 onView(withId(R.id.button_register_confirm)).perform(click())110 //kijk password not equal toast111 onView(withText(R.string.warning_passwords_not_equal)).inRoot(112 RootMatchers.withDecorView(113 CoreMatchers.not(114 CoreMatchers.`is`(115 mActivityTestRule.activity.window.decorView116 )117 )118 )119 ).check(matches(isDisplayed()))120 }121 @Test122 fun register_existingUsername_doesNotRegister() {123 //vul email in124 onView(withId(R.id.text_register_email)).perform(ViewActions.typeText(email))125 android.support.test.espresso.Espresso.closeSoftKeyboard()126 //vul username in127 onView(withId(R.id.text_register_username)).perform(ViewActions.typeText(tokenUsername))128 android.support.test.espresso.Espresso.closeSoftKeyboard()129 //vul ww in130 onView(withId(R.id.text_register_password)).perform(ViewActions.typeText(password))131 android.support.test.espresso.Espresso.closeSoftKeyboard()132 //vul confirm ww in133 onView(withId(R.id.text_register_confirm_password)).perform(ViewActions.typeText(password))134 android.support.test.espresso.Espresso.closeSoftKeyboard()135 //druk op registreer136 onView(withId(R.id.button_register_confirm)).perform(click())137 //kijk register fragment zichtbaar (toast komt van server)138 onView(withId(R.id.fragment_register)).check(matches(isDisplayed()))139 }140 @Test141 fun register_existingEmail_doesNotRegister() {142 //vul email in143 onView(withId(R.id.text_register_email)).perform(ViewActions.typeText(tokenEmail))144 android.support.test.espresso.Espresso.closeSoftKeyboard()145 //vul username in146 onView(withId(R.id.text_register_username)).perform(ViewActions.typeText(username))147 android.support.test.espresso.Espresso.closeSoftKeyboard()148 //vul ww in149 onView(withId(R.id.text_register_password)).perform(ViewActions.typeText(password))150 android.support.test.espresso.Espresso.closeSoftKeyboard()151 //vul confirm ww in152 onView(withId(R.id.text_register_confirm_password)).perform(ViewActions.typeText(password))153 android.support.test.espresso.Espresso.closeSoftKeyboard()154 //druk op registreer155 onView(withId(R.id.button_register_confirm)).perform(click())156 //kijk register fragment zichtbaar (toast komt van server)157 onView(withId(R.id.fragment_register)).check(matches(isDisplayed()))158 }159 @Test160 fun register_correctInfo_registerAndCorrectUsername() {161 //vul email in162 onView(withId(R.id.text_register_email)).perform(ViewActions.typeText(email))163 android.support.test.espresso.Espresso.closeSoftKeyboard()164 //vul username in165 onView(withId(R.id.text_register_username)).perform(ViewActions.typeText(username))166 android.support.test.espresso.Espresso.closeSoftKeyboard()167 //vul ww in168 onView(withId(R.id.text_register_password)).perform(ViewActions.typeText(password))169 android.support.test.espresso.Espresso.closeSoftKeyboard()170 //vul confirm ww in171 onView(withId(R.id.text_register_confirm_password)).perform(ViewActions.typeText(password))172 android.support.test.espresso.Espresso.closeSoftKeyboard()173 //druk op registreer174 onView(withId(R.id.button_register_confirm)).perform(click())175 //wacht even voor server176 SystemClock.sleep(1000)177 //username op account pagina178 onView(withId(R.id.text_account_username)).check(matches(withText(username)))179 }180}...
MainActivityInstrumentationTest.kt
Source:MainActivityInstrumentationTest.kt
...21 var activityRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java)22 @get:Rule var permissionRule = GrantPermissionRule.grant(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)23 @Test24 fun fabMenu_items_should_not_be_displayed_in_start(){25 Espresso.onView(withId(R.id.menu)).check(ViewAssertions.matches(isDisplayed()))26 Espresso.onView(withId(R.id.menu_fb)).check(ViewAssertions.matches(not(isDisplayed())))27 Espresso.onView(withId(R.id.menu_getData)).check(ViewAssertions.matches(not(isDisplayed())))28 }29 @Test30 fun fabMenu_open_and_close(){31 Espresso.onView(allOf(withParent(withId(R.id.menu)), withClassName(endsWith("ImageView")), isDisplayed()))32 .perform(click())33 Espresso.onView(withId(R.id.menu_fb)).check(ViewAssertions.matches(isDisplayed()))34 Espresso.onView(withId(R.id.menu_getData)).check(ViewAssertions.matches(isDisplayed()))35 Espresso.onView(withId(R.id.menu)).check(ViewAssertions.matches(isDisplayed()))36 Espresso.onView(allOf(withParent(withId(R.id.menu)), withClassName(endsWith("ImageView")), isDisplayed()))37 .perform(click())38 Espresso.onView(withId(R.id.menu_fb)).check(ViewAssertions.matches(not(isDisplayed())))39 Espresso.onView(withId(R.id.menu_getData)).check(ViewAssertions.matches(not(isDisplayed())))40 Espresso.onView(withId(R.id.menu)).check(ViewAssertions.matches(isDisplayed()))41 }42 @Test43 fun logo_fragment_should_be_visible_in_start(){44 Espresso.onView(withId(R.id.main_page_fragment)).check(ViewAssertions.matches(isDisplayed()))45 Espresso.onView(withId(R.id.logo_image)).check(ViewAssertions.matches(isDisplayed()))46 Espresso.onView(withId(R.id.logo_name)).check(ViewAssertions.matches(isDisplayed()))47 }48 @Test49 fun get_data_from_api_and_display_it(){50 Espresso.onView(allOf(withParent(withId(R.id.menu)), withClassName(endsWith("ImageView")), isDisplayed()))51 .perform(click())52 Espresso.onView(withId(R.id.menu_getData)).perform(click())53 Espresso.onView(withId(R.id.images)).check(ViewAssertions54 .matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))55 .check(ViewAssertions.matches(withText(containsString("Images:"))))56 Espresso.onView(withId(R.id.recipe_fragment)).check(ViewAssertions.matches(isDisplayed()))57 .check(ViewAssertions.matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))58 Espresso.onView(withId(R.id.name)).check(ViewAssertions.matches(isDisplayed()))59 .check(ViewAssertions.matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))60 Espresso.onView(withId(R.id.Ingredients)).check(ViewAssertions.matches(isDisplayed()))61 .check(ViewAssertions.matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))62 .check(ViewAssertions.matches(withText(containsString("Ingredients:"))))63 Espresso.onView(withId(R.id.Preparing)).check(ViewAssertions.matches(isDisplayed()))64 .check(ViewAssertions.matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))65 .check(ViewAssertions.matches(withText(containsString("Preparing:"))))66 Espresso.onView(withId(R.id.description))67 .check(ViewAssertions.matches(isDisplayed()))68 .check(ViewAssertions.matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))69 Espresso.onView(withId(R.id.ingredients))70 .check(ViewAssertions.matches(isDisplayed()))71 .check(ViewAssertions.matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))72 }73 @Test74 fun show_dialog_after_click_on_image_to_save(){75 Espresso.onView(allOf(withParent(withId(R.id.menu)), withClassName(endsWith("ImageView")), isDisplayed()))76 .perform(click())77 Espresso.onView(withId(R.id.menu_getData)).perform(click())78 Espresso.onView(withId(R.id.flexboxImages))79 .perform(scrollTo())80 Espresso.onView(81 withTagValue(`is`("Image0" as Object))82 ).perform(click())83 Espresso.onView(withText("Save image")).check(ViewAssertions.matches(isDisplayed()))84 }85 @Test86 fun show_toast_after_save_file_correct(){87 Espresso.onView(allOf(withParent(withId(R.id.menu)), withClassName(endsWith("ImageView")), isDisplayed()))88 .perform(click())89 Espresso.onView(withId(R.id.menu_getData)).perform(click())90 Espresso.onView(withId(R.id.flexboxImages))91 .perform(scrollTo())92 Espresso.onView(93 withTagValue(`is`("Image0" as Object))94 ).perform(click())95 Espresso.onView(withId(android.R.id.button1)).perform(click())96 Espresso.onView(withText("Image saved!")).inRoot(withDecorView(not(`is`(activityRule.activity.window.decorView)))).97 check(ViewAssertions.matches(isDisplayed()))98 }99 @Test100 fun snackbar_should_be_displayed_after_login(){101 activityRule.activity.setIsLog(true)102 activityRule.activity.setUser("test test")103 Espresso.onView(allOf(withParent(withId(R.id.menu)), withClassName(endsWith("ImageView")), isDisplayed()))104 .perform(click())105 Espresso.onView(withId(R.id.menu_getData)).perform(click())106 Espresso.onView(withText("Logged as test test")).check(ViewAssertions.matches(isDisplayed()))107 }108 @Test109 fun show_dialog_when_user_wants_logout(){110 activityRule.activity.setIsLog(true)111 activityRule.activity.setUser("test test")112 Espresso.onView(allOf(withParent(withId(R.id.menu)), withClassName(endsWith("ImageView")), isDisplayed()))113 .perform(click())114 Espresso.onView(withId(R.id.menu_fb)).perform(click())115 Espresso.onView(withText("Logged in as: test test")).inRoot(withDecorView(not(`is`(activityRule.activity.window.decorView)))).116 check(ViewAssertions.matches(isDisplayed()))117 }118 @Test119 fun show_toast_after_logout(){120 activityRule.activity.setIsLog(true)121 activityRule.activity.setUser("test test")122 Espresso.onView(allOf(withParent(withId(R.id.menu)), withClassName(endsWith("ImageView")), isDisplayed()))123 .perform(click())124 Espresso.onView(withId(R.id.menu_fb)).perform(click())125 Espresso.onView(withId(android.R.id.button1)).perform(click())126 Espresso.onView(withText("You have been logged out.")).inRoot(withDecorView(not(`is`(activityRule.activity.window.decorView)))).127 check(ViewAssertions.matches(isDisplayed()))128 }129}...
EspressoMainTests.kt
Source:EspressoMainTests.kt
...19 @JvmField20 val rule = ActivityTestRule(MainActivity::class.java)21 @Test22 fun clickComicsPartOfScreen(){23 Espresso.onView(withId(R.id.my_recycler_view)).check(matches(isDisplayed()))24 }25 @Test26 fun clickCharacterPartOfScreen(){27 Espresso.onView(withId(R.id.my_recycler_view)).check(matches(isDisplayed()))28 }29 @Test30 fun characterSearchBarWithSearchResult(){31 Espresso.onView(withId(R.id.search)).perform(click())32 Espresso.onView(withId(R.id.search_src_text)).perform(typeText("alex"))33 Espresso.onView(withId(R.id.search_src_text)).perform(pressImeActionButton())34 Espresso.onView(withId(R.id.my_recycler_view)).check(matches(isDisplayed()))35 }36 @Test37 fun characterSearchBarWithNoSearchResult(){38 Espresso.onView(withId(R.id.search)).perform(click())39 Espresso.onView(withId(R.id.search_src_text)).perform(typeText("nao achei >:("))40 Espresso.onView(withId(R.id.search_src_text)).perform(pressImeActionButton())41 Thread.sleep(10000)42 Espresso.onView(withId(R.id.my_recycler_view)).check(matches(withEffectiveVisibility(Visibility.GONE)))43 }44 @Test45 fun comicSearchBarWithNoSearchResult(){46 Espresso.onView(withId(R.id.search)).perform(click())47 Espresso.onView(withId(R.id.search_src_text)).perform(typeText("achei nada!"))48 Espresso.onView(withId(R.id.search_src_text)).perform(pressImeActionButton())49 Thread.sleep(10000)50 Espresso.onView(withId(R.id.my_recycler_view)).check(matches(withEffectiveVisibility(Visibility.GONE)))51 }52 @Test53 fun comicSearchBarWithSearchResult(){54 Espresso.onView(withId(R.id.search)).perform(click())55 Espresso.onView(withId(R.id.search_src_text)).perform(ViewActions.typeText("dead"))56 Espresso.onView(withId(R.id.search_src_text)).perform(pressImeActionButton())57 Espresso.onView(withId(R.id.my_recycler_view)).check(matches(isDisplayed()))58 }59 @Test60 fun characterClickOnResultinRecyclerview(){61 Thread.sleep(5000)62 Espresso.onView(withId(R.id.my_recycler_view)).perform(RecyclerViewActions.actionOnItemAtPosition<CharacterAdapter.ItemHolder>(0, click()))63 Espresso.onView(withId(R.id.character_image)).check(matches(isDisplayed()))64 }65 @Test66 fun comicClickOnResultinRecyclerview(){67 Thread.sleep(5000)68 Espresso.onView(withId(R.id.my_recycler_view)).perform(RecyclerViewActions.actionOnItemAtPosition<ComicAdapter.ItemHolder>(0, click()))69 Espresso.onView(withId(R.id.comic_image)).check(matches(isDisplayed()))70 }71 @Test72 fun scrollingComicsRecyclerList(){73 Thread.sleep(5000)74 Espresso.onView(withId(R.id.my_recycler_view))75 .perform(RecyclerViewActions.scrollToPosition<ComicAdapter.ItemHolder>(20))76 Thread.sleep(5000)77 Espresso.onView(withId(R.id.my_recycler_view))78 .perform(RecyclerViewActions.scrollToPosition<ComicAdapter.ItemHolder>(40))79 }80 @Test81 fun scrollingCharactersRecyclerList(){82 Thread.sleep(5000)83 Espresso.onView(withId(R.id.my_recycler_view))84 .perform(RecyclerViewActions.scrollToPosition<CharacterAdapter.ItemHolder>(20))85 Thread.sleep(5000)86 Espresso.onView(withId(R.id.my_recycler_view))87 .perform(RecyclerViewActions.scrollToPosition<CharacterAdapter.ItemHolder>(40))88 }89}...
MainActivityTest.kt
Source:MainActivityTest.kt
...21 var activityRule = ActivityTestRule(MainActivity::class.java)22 @Test23 fun testRecyclerViewBehaviour() {24 sleep_for_awhile()25 Espresso.onView(ViewMatchers.withId(main_list_view_last))26 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))27 sleep_for_awhile()28 Espresso.onView(ViewMatchers.withId(main_list_view_last)).perform(29 RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(3))30 Espresso.onView(ViewMatchers.withId(main_list_view_last)).perform(31 RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(3, click()))32 sleep_for_awhile(3000)33 pressBack()34 }35 @Test36 fun testAppBehaviour() {37 // Click Prev. Match38 Espresso.onView(ViewMatchers.withId(nav_last_match))39 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))40 Espresso.onView(ViewMatchers.withId(nav_last_match)).perform(click())41 sleep_for_awhile()42 // Cek Tab Next Match43 Espresso.onView(ViewMatchers.withId(nav_next_match))44 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))45 Espresso.onView(ViewMatchers.withId(nav_next_match)).perform(click())46 sleep_for_awhile()47 // Cek Tab Favorite48 Espresso.onView(ViewMatchers.withId(nav_favorite))49 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))50 Espresso.onView(ViewMatchers.withId(nav_favorite)).perform(click())51 sleep_for_awhile()52 pressBack()53 sleep_for_awhile()54 }55 @Test56 fun testAppFavorite() {57 // Cek Tab Next Match58 Espresso.onView(ViewMatchers.withId(nav_last_match))59 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))60 Espresso.onView(ViewMatchers.withId(nav_last_match)).perform(click())61 sleep_for_awhile(1000)62 // Display List Last Match63 Espresso.onView(ViewMatchers.withId(main_list_view_last)).perform(64 RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(4))65 Espresso.onView(ViewMatchers.withId(main_list_view_last)).perform(66 RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(4, click()))67 sleep_for_awhile()68 // Click Favorite Star69 Espresso.onView(ViewMatchers.withId(add_to_favorite))70 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))71 Espresso.onView(ViewMatchers.withId(add_to_favorite)).perform(ViewActions.click())72 sleep_for_awhile()73 // Back to main page74 Espresso.pressBack()75 sleep_for_awhile()76 // Cek / Display Tab Favorite77 Espresso.onView(ViewMatchers.withId(nav_favorite))78 .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))79 Espresso.onView(ViewMatchers.withId(nav_favorite)).perform(click())80 sleep_for_awhile()81 pressBack()82 }83 private fun sleep_for_awhile(timetosleep: Long = 2000) {84 try {85 Thread.sleep(timetosleep)86 } catch (e: InterruptedException) {87 e.printStackTrace()88 }89 }90}...
SignUpActivityTest1.kt
Source:SignUpActivityTest1.kt
...22 var mRuntimePermissionRule = GrantPermissionRule23 .grant(android.Manifest.permission.ACCESS_FINE_LOCATION)24 @Test25 fun signUpActivityTestNoCredentials() {26 Espresso.onView(ViewMatchers.withId(R.id.email_sign_up_button))27 .perform(ViewActions.click())28 Espresso.onView(ViewMatchers.withId(R.id.signup_username))29 .check(matches(hasErrorText("This username is too short")))30 }31 @Test32 fun signUpActivityTestOnlyUsername() {33 Espresso.onView(ViewMatchers.withId(R.id.signup_username))34 .perform(ViewActions.replaceText("EspressoUsername"))35 Espresso.onView(ViewMatchers.withId(R.id.email_sign_up_button))36 .perform(ViewActions.click())37 Espresso.onView(ViewMatchers.withId(R.id.signup_email))38 .check(matches(hasErrorText("This field is required")))39 }40 @Test41 fun signUpActivityTestNoPassword() {42 Espresso.onView(ViewMatchers.withId(R.id.signup_username))43 .perform(ViewActions.replaceText("EspressoUsername"))44 Espresso.onView(ViewMatchers.withId(R.id.signup_email))45 .perform(ViewActions.replaceText("EspressoEmail@gmail.com"))46 Espresso.onView(ViewMatchers.withId(R.id.email_sign_up_button))47 .perform(ViewActions.click())48 Espresso.onView(ViewMatchers.withId(R.id.signup_password))49 .check(matches(hasErrorText("This password is too short")))50 }51 @Test52 fun signUpActivityTestInvalidEmail() {53 Espresso.onView(ViewMatchers.withId(R.id.signup_username))54 .perform(ViewActions.replaceText("EspressoUsername"))55 Espresso.onView(ViewMatchers.withId(R.id.signup_password))56 .perform(ViewActions.replaceText("EspressoPassword"))57 Espresso.onView(ViewMatchers.withId(R.id.signup_email))58 .perform(ViewActions.replaceText("EspressoEmailgmail.com"))59 Espresso.onView(ViewMatchers.withId(R.id.email_sign_up_button))60 .perform(ViewActions.click())61 Espresso.onView(ViewMatchers.withId(R.id.signup_email))62 .check(matches(hasErrorText("This email address is invalid")))63 }64 @Test65 fun signUpActivityTestValidRegister() {66 Espresso.onView(ViewMatchers.withId(R.id.signup_username))67 .perform(ViewActions.replaceText("EspressoUsername"))68 Espresso.onView(ViewMatchers.withId(R.id.signup_password))69 .perform(ViewActions.replaceText("EspressoPassword"))70 Espresso.onView(ViewMatchers.withId(R.id.signup_email))71 .perform(ViewActions.replaceText("EspressoEmail@gmail.com"))72 Espresso.onView(ViewMatchers.withId(R.id.email_sign_up_button))73 .perform(ViewActions.click())74 Thread.sleep(15000)75 Espresso.onView(ViewMatchers.withId(R.id.main_OpenMenu))76 .perform(ViewActions.click())77 }78}...
LoginActivityTest.kt
Source:LoginActivityTest.kt
...22 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}...
MapsActivityTest.kt
Source:MapsActivityTest.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.action.ViewActions.click6import android.support.test.espresso.action.ViewActions.swipeDown7import android.support.test.espresso.assertion.ViewAssertions.matches8import android.support.test.espresso.matcher.ViewMatchers.*9import android.support.test.filters.LargeTest10import android.support.test.rule.ActivityTestRule11import android.support.test.runner.AndroidJUnit412import android.view.View13import android.view.ViewGroup14import org.hamcrest.CoreMatchers.allOf15import org.hamcrest.CoreMatchers.not16import org.hamcrest.Matcher17import org.hamcrest.TypeSafeMatcher18import org.junit.Rule19import org.junit.Test20import org.junit.runner.RunWith21import ru.xmn.russiancraftbeer.R22@RunWith(AndroidJUnit4::class)23@LargeTest24class MapsActivityTest {25 @Rule26 @JvmField27 val mActivityRule = ActivityTestRule<MapsActivity>(28 MapsActivity::class.java)29 @Test30 fun pager_is_visible() {31 val idlingResource = ElapsedTimeIdlingResource(6000)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}...
onView
Using AI Code Generation
1Espresso.onView( withText( "Hello World!" )).perform( click());2Espresso.onView( withText( "Hello World!" )).perform( click());3Espresso.onView( withText( "Hello World!" )).perform( click());4Espresso.onView( withText( "Hello World!" )).perform( click());5Espresso.onView( withText( "Hello World!" )).perform( click());6Espresso.onView( withText( "Hello World!" )).perform( click());7Espresso.onView( withText( "Hello World!" )).perform( click());8Espresso.onView( withText( "Hello World!" )).perform( click());9Espresso.onView( withText( "Hello World!" )).perform( click());10Espresso.onView( withText( "Hello World!" )).perform( click());11Espresso.onView( withText( "Hello World!" )).perform( click());12Espresso.onView( withText( "Hello World!" )).perform( click());13Espresso.onView( withText( "Hello World!" )).perform( click());14Espresso.onView( withText( "Hello World!" )).perform( click());15Espresso.onView( withText( "Hello World!" )).perform( click());
onView
Using AI Code Generation
1onView(withId(R.id.textView)).perform(typeText("Hello Espresso"));2onData(anything()).inAdapterView(withId(R.id.listView)).atPosition(0).perform(click());3onIdlingResource(new SimpleIdlingResource("simple"));4registerIdlingResources(new SimpleIdlingResource("simple"));5unregisterIdlingResources(new SimpleIdlingResource("simple"));6openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());7pressBack();8pressBackUnconditionally();9pressKey(KeyEvent.KEYCODE_BACK);10pressMenuKey();11pressMenuItemId(R.id.menu_item);12pressBack();13pressBackUnconditionally();14pressKey(KeyEvent.KEYCODE_BACK);15pressMenuKey();16pressMenuItemId(R.id.menu_item);17pressBack();18pressBackUnconditionally();19pressKey(KeyEvent.KEYCODE_BACK);
onView
Using AI Code Generation
1Espresso.onView(ViewMatchers.withId(R.id.button)).perform(ViewActions.click());2Espresso.onView(ViewMatchers.withId(R.id.textView)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));3package com.example.sandeeprana.espressoexample;4import android.support.test.espresso.Espresso;5import android.support.test.espresso.action.ViewActions;6import android.support.test.espresso.assertion.ViewAssertions;7import android.support.test.espresso.matcher.ViewMatchers;8import android.support.test.rule.ActivityTestRule;9import android.support.test.runner.AndroidJUnit4;10import org.junit.Rule;11import org.junit.Test;12import org.junit.runner.RunWith;13@RunWith(AndroidJUnit4.class)14public class MainActivityTest {15 public ActivityTestRule<MainActivity> mainActivityActivityTestRule = new ActivityTestRule<>(MainActivity.class);16 public void testClickButton() {17 Espresso.onView(ViewMatchers.withId(R.id.button)).perform(ViewActions.click());18 Espresso.onView(ViewMatchers.withId(R.id.textView)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));19 }20}
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!!