Best Appium-espresso-driver code snippet using android.support.test.espresso.Espresso
Dependencies.kt
Source:Dependencies.kt
1/**2 * This file contains all the dependencies we need in our project. Because we placed3 * them inside the buildSrc module we get auto code completion inside the rest of the project.4 * More information about this dependency management technique is available at:5 * https://handstandsam.com/2018/02/11/kotlin-buildsrc-for-better-gradle-dependency-management/6 *7 * @author Dennis Wehrle8 */9@Suppress("unused")10object Versions {11 // Android12 const val androidCompileSdk = 2713 const val androidMinSdk = 1614 const val androidTargetSdk = 2715 const val constraintLayout = "1.0.2"16 const val supportLibrary = "27.1.0"17 // App Libraries18 const val dagger = "2.15"19 const val glassfishAnnotation = "10.0-b28"20 const val glide = "4.6.1"21 const val gson = "2.8.1"22 const val javaxAnnotation = "1.0"23 const val javaxInject = "1"24 const val kotlin = "1.2.41"25 const val leakCanary = "1.5.4"26 const val okHttp = "3.8.1"27 const val retrofit = "2.4.0"28 const val room = "1.0.0"29 const val rxAndroid = "2.0.2"30 const val rxJava = "2.1.12"31 const val rxKotlin = "2.2.0"32 const val stetho = "1.5.0"33 const val timber = "4.6.1"34 const val pageIndicatorView = "0.2.0"35 const val moshi = "1.6.0"36 const val lifecycle = "1.1.1"37 const val lifecycleCompiler = "1.1.1"38 // Test Libraries39 const val androidSupportRules = "1.0.0"40 const val androidSupportRunner = "1.0.0"41 const val assertJ = "3.8.0"42 const val dexmakerMockito = "2.16.0"43 const val dexopener = "0.12.1"44 const val espresso = "3.0.1"45 const val jUnit = "4.12"46 const val mockitoKotlin = "1.5.0"47 const val robolectric = "3.4.2"48 const val runner = "0.5"49}50@Suppress("unused")51object AppDependencies {52 const val androidSupportAnnotations = "com.android.support:support-annotations:${Versions.supportLibrary}"53 const val androidSupportAppCompatV7 = "com.android.support:appcompat-v7:${Versions.supportLibrary}"54 const val androidSupportConstraintLayout = "com.android.support.constraint:constraint-layout:${Versions.constraintLayout}"55 const val androidSupportDesign = "com.android.support:design:${Versions.supportLibrary}"56 const val androidSupportRecyclerView = "com.android.support:recyclerview-v7:${Versions.supportLibrary}"57 const val androidSupportV13 = "com.android.support:support-v13:${Versions.supportLibrary}"58 const val androidSupportCardView = "com.android.support:cardview-v7:${Versions.supportLibrary}"59 const val dagger = "com.google.dagger:dagger:${Versions.dagger}"60 const val daggerCompiler = "com.google.dagger:dagger-compiler:${Versions.dagger}"61 const val daggerProcessor = "com.google.dagger:dagger-android-processor:${Versions.dagger}"62 const val daggerSupport = "com.google.dagger:dagger-android-support:${Versions.dagger}"63 const val glassfishAnnotation = "org.glassfish:javax.annotation:${Versions.glassfishAnnotation}"64 const val glide = "com.github.bumptech.glide:glide:${Versions.glide}"65 const val gson = "com.google.code.gson:gson:${Versions.gson}"66 const val javaxAnnotation = "javax.annotation:jsr250-api:${Versions.javaxAnnotation}"67 const val javaxInject = "javax.inject:javax.inject:${Versions.javaxInject}"68 const val kotlin = "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${Versions.kotlin}"69 const val leakCanaryDebug = "com.squareup.leakcanary:leakcanary-android:${Versions.leakCanary}"70 const val leakCanaryRelease = "com.squareup.leakcanary:leakcanary-android-no-op:${Versions.leakCanary}"71 const val okHttp = "com.squareup.okhttp3:okhttp:${Versions.okHttp}"72 const val okHttpLogger = "com.squareup.okhttp3:logging-interceptor:${Versions.okHttp}"73 const val pageindicatorview = "com.romandanylyk:pageindicatorview:${Versions.pageIndicatorView}@aar"74 const val retrofit = "com.squareup.retrofit2:retrofit:${Versions.retrofit}"75 const val retrofitAdapter = "com.squareup.retrofit2:adapter-rxjava2:${Versions.retrofit}"76 const val retrofitConverter = "com.squareup.retrofit2:converter-gson:${Versions.retrofit}"77 const val roomCompiler = "android.arch.persistence.room:compiler:${Versions.room}"78 const val roomRuntime = "android.arch.persistence.room:runtime:${Versions.room}"79 const val roomRxJava = "android.arch.persistence.room:rxjava2:${Versions.room}"80 const val rxAndroid = "io.reactivex.rxjava2:rxandroid:${Versions.rxAndroid}"81 const val rxJava = "io.reactivex.rxjava2:rxjava:${Versions.rxJava}"82 const val rxKotlin = "io.reactivex.rxjava2:rxkotlin:${Versions.rxKotlin}"83 const val stetho = "com.facebook.fresco:stetho:${Versions.stetho}"84 const val timber = "com.jakewharton.timber:timber:${Versions.timber}"85 const val moshi = "com.squareup.moshi:moshi-kotlin:${Versions.moshi}"86 const val lifecycle = "android.arch.lifecycle:extensions:${Versions.lifecycle}"87 const val lifecycleCompiler = "android.arch.lifecycle:compiler:${Versions.lifecycleCompiler}"88 const val moshiConverter = "com.squareup.retrofit2:converter-moshi:2.0.0-beta3"89}90@Suppress("unused")91object TestDependencies {92 const val androidRules = "com.android.support.test:rules:${Versions.runner}"93 const val androidRunner = "com.android.support.test:runner:${Versions.runner}"94 const val assertj = "org.assertj:assertj-core:${Versions.assertJ}"95 const val dexmakerMockito = "com.linkedin.dexmaker:dexmaker-mockito:${Versions.dexmakerMockito}"96 const val dexopener = "com.github.tmurakami:dexopener:${Versions.dexopener}"97 const val espressoContrib = "com.android.support.test.espresso:espresso-contrib:${Versions.espresso}"98 const val espressoCore = "com.android.support.test.espresso:espresso-core:${Versions.espresso}"99 const val espressoIntents = "com.android.support.test.espresso:espresso-intents:${Versions.espresso}"100 const val junit = "junit:junit:${Versions.jUnit}"101 const val kotlinJUnit = "org.jetbrains.kotlin:kotlin-test-junit:${Versions.kotlin}"102 const val mockitoKotlin = "com.nhaarman:mockito-kotlin:${Versions.mockitoKotlin}"103 const val robolectric = "org.robolectric:robolectric:${Versions.robolectric}"104 const val roomTesting = "android.arch.persistence.room:testing:${Versions.room}"105 const val supportRules = "com.android.support.test:rules:${Versions.androidSupportRules}"106 const val supportRunner = "com.android.support.test:runner:${Versions.androidSupportRunner}"107} ...
MainActivityTest.kt
Source:MainActivityTest.kt
1package com.tooqy.football_api.Event2import android.support.test.espresso.Espresso3import android.support.test.espresso.action.ViewActions4import android.support.test.espresso.action.ViewActions.click5import android.support.test.espresso.action.ViewActions.pressBack6import android.support.test.espresso.assertion.ViewAssertions7import android.support.test.espresso.contrib.RecyclerViewActions8import android.support.test.espresso.matcher.ViewMatchers9import android.support.test.rule.ActivityTestRule10import android.support.test.runner.AndroidJUnit411import android.support.v7.widget.RecyclerView12import com.tooqy.football_api.MainActivity13import com.tooqy.football_api.R.id.*14import org.junit.Rule15import org.junit.Test16import org.junit.runner.RunWith17@RunWith(AndroidJUnit4::class)18class MainActivityTest {19 @Rule20 @JvmField21 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
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 SignUpActivityTest1 {17 @Rule18 @JvmField19 var mActivityTestRule = ActivityTestRule(SignUpActivity::class.java)20 @Rule21 @JvmField22 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}...
TestSkenario.kt
Source:TestSkenario.kt
1package com.iav.kade2import 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)...
ExampleInstrumentedTest.kt
Source:ExampleInstrumentedTest.kt
1package ru.xmn.russiancraftbeer2import android.support.test.InstrumentationRegistry3import android.support.test.espresso.Espresso.onView4import android.support.test.espresso.IdlingPolicies5import android.support.test.espresso.action.ViewActions.click6import android.support.test.espresso.assertion.ViewAssertions.matches7import android.support.test.espresso.matcher.ViewMatchers.*8import android.support.test.rule.ActivityTestRule9import android.support.test.runner.AndroidJUnit410import android.text.format.DateUtils11import org.junit.Rule12import org.junit.Test13import org.junit.runner.RunWith14import org.junit.Assert.*15import java.util.concurrent.TimeUnit16import android.support.test.espresso.Espresso17import ElapsedTimeIdlingResource18import android.support.test.espresso.IdlingResource19import android.support.test.uiautomator.UiDevice20import android.support.test.uiautomator.UiSelector21import android.test.InstrumentationTestCase22import ru.xmn.russiancraftbeer.screens.map.ui.MapsActivity23/**24 * Instrumented test, which will execute on an Android device.25 *26 * See [testing documentation](http://d.android.com/tools/testing).27 */28@RunWith(AndroidJUnit4::class)29class ExampleInstrumentedTest{30 @Test31 fun useAppContext() {32 // Context of the app under test.33 val appContext = InstrumentationRegistry.getTargetContext()34 assertEquals("ru.xmn.russiancraftbeer", appContext.packageName)35 }36 @get:Rule37 public var mActivityRule = ActivityTestRule<MapsActivity>(38 MapsActivity::class.java)39 @Test40 fun helpOpen(){41 var waitingTime = DateUtils.SECOND_IN_MILLIS * 542 IdlingPolicies.setMasterPolicyTimeout(43 waitingTime * 2, TimeUnit.MILLISECONDS);44 IdlingPolicies.setIdlingResourceTimeout(45 waitingTime * 2, TimeUnit.MILLISECONDS);46 val idlingResource = ElapsedTimeIdlingResource(waitingTime)47 Espresso.registerIdlingResources(idlingResource)48 onView(withId(R.id.help_button)).perform(click())49 Espresso.registerIdlingResources(idlingResource)50 onView(withId(R.id.help_card)).check(matches(isDisplayed()))51 Espresso.unregisterIdlingResources(idlingResource);52 }53 @Test54 fun markerClick(){55 var waitingTime = DateUtils.SECOND_IN_MILLIS * 556 IdlingPolicies.setMasterPolicyTimeout(57 waitingTime * 2, TimeUnit.MILLISECONDS);58 IdlingPolicies.setIdlingResourceTimeout(59 waitingTime * 2, TimeUnit.MILLISECONDS);60 val idlingResource = ElapsedTimeIdlingResource(waitingTime)61 Espresso.registerIdlingResources(idlingResource)62 val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())63 val marker = device.findObject(UiSelector().descriptionContains("ÐÐÐ"))64 marker.click()65 Espresso.registerIdlingResources(idlingResource)66 onView(withId(R.id.help_card)).check(matches(isDisplayed()))67 Espresso.unregisterIdlingResources(idlingResource);68 }69}...
LoginActivityTest.kt
Source:LoginActivityTest.kt
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}...
HeroesDetailActivityTest.kt
Source:HeroesDetailActivityTest.kt
1package com.costular.marvelheroes.presentation.heroesdetail2import android.content.Intent3import android.support.test.InstrumentationRegistry4import android.support.test.espresso.Espresso5import android.support.test.espresso.Espresso.onView6import android.support.test.espresso.assertion.ViewAssertions7import android.support.test.espresso.assertion.ViewAssertions.matches8import android.support.test.espresso.matcher.ViewMatchers9import android.support.test.espresso.matcher.ViewMatchers.withId10import android.support.test.espresso.matcher.ViewMatchers.withText11import android.support.test.rule.ActivityTestRule12import android.support.test.runner.AndroidJUnit413import com.costular.marvelheroes.R14import com.costular.marvelheroes.presentation.FakeData15import com.costular.marvelheroes.presentation.herodetail.HeroDetailActivity16import org.junit.Test17import org.junit.runner.RunWith18/**19 * Created by costular on 18/03/2018.20 */21@RunWith(AndroidJUnit4::class)22class HeroesDetailActivityTest {23 val activityTestRule = ActivityTestRule<HeroDetailActivity>(HeroDetailActivity::class.java, false, false)24 @Test25 fun testBasicInitialization() {26 activityTestRule.launchActivity(Intent())27 Espresso.onView(ViewMatchers.withText(R.string.app_name)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()))28 }29 @Test30 fun testDataIsLoadedSuccessfully() {31 val intent = Intent(32 InstrumentationRegistry.getTargetContext(),33 HeroDetailActivity::class.java34 ).apply {35 putExtra(HeroDetailActivity.PARAM_HEROE, FakeData.IRON_MAN)36 }37 activityTestRule.launchActivity(intent)38 onView(withId(R.id.heroDetailName)).check(matches(withText(FakeData.NAME)))39 onView(withId(R.id.heroDetailRealName)).check(matches(withText(FakeData.REAL_NAME)))40 onView(withId(R.id.heroDetailHeight)).check(matches(withText(FakeData.HEIGHT)))41 onView(withId(R.id.heroDetailPower)).check(matches(withText(FakeData.POWER)))...
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}...
Espresso
Using AI Code Generation
1import android.support.test.espresso.Espresso;2import android.support.test.espresso.ViewInteraction;3import android.support.test.espresso.action.ViewActions;4import android.support.test.espresso.matcher.ViewMatchers;5import android.support.test.rule.ActivityTestRule;6import android.support.test.runner.AndroidJUnit4;7import android.support.test.uiautomator.UiDevice;8import org.junit.Rule;9import org.junit.Test;10import org.junit.runner.RunWith;11import static android.support.test.espresso.assertion.ViewAssertions.matches;12import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;13import static android.support.test.espresso.matcher.ViewMatchers.withId;14import static android.support.test.espresso.matcher.ViewMatchers.withText;15@RunWith(AndroidJUnit4.class)16public class MainActivityTest {17public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);18public void mainActivityTest() {19ViewInteraction appCompatTextView = Espresso.onView(ViewMatchers.withId(R.id.text_view));20appCompatTextView.perform(ViewActions.click());21Espresso.onView(withId(R.id.text_view)).check(matches(withText("Hello Espresso!")));22}23}
Espresso
Using AI Code Generation
1import android.support.test.espresso.Espresso;2import android.support.test.espresso.ViewInteraction;3import android.support.test.espresso.matcher.ViewMatchers;4import android.support.test.rule.ActivityTestRule;5import android.support.test.runner.AndroidJUnit4;6import android.view.View;7import android.view.ViewGroup;8import android.view.ViewParent;9import org.hamcrest.Description;10import org.hamcrest.Matcher;11import org.hamcrest.TypeSafeMatcher;12import org.junit.Rule;13import org.junit.Test;14import org.junit.runner.RunWith;15import static android.support.test.espresso.Espresso.onView;16import static android.support.test.espresso.action.ViewActions.click;17import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;18import static android.support.test.espresso.action.ViewActions.typeText;19import static android.support.test.espresso.assertion.ViewAssertions.matches;20import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;21import static android.support.test.espresso.matcher.ViewMatchers.withId;22import static android.support.test.espresso.matcher.ViewMatchers.withText;23import static org.hamcrest.Matchers.allOf;24@RunWith(AndroidJUnit4.class)25public class MainActivityTest {26public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);27public void mainActivityTest() {28ViewInteraction appCompatEditText = onView(29allOf(withId(R.id.editText),30childAtPosition(31childAtPosition(32withId(android.R.id.content),33isDisplayed()));34appCompatEditText.perform(click());35appCompatEditText.perform(typeText("Hello World"), closeSoftKeyboard());36ViewInteraction appCompatButton = onView(37allOf(withId(R.id.button), withText("Click"),38childAtPosition(39childAtPosition(40withId(android.R.id.content),41isDisplayed()));42appCompatButton.perform(click());43ViewInteraction textView = onView(44allOf(withId(R.id.textView), withText("Hello World!"),45childAtPosition(46childAtPosition(47withId(android.R.id.content),48isDisplayed()));49textView.check(matches(withText("Hello World!")));50}51private static Matcher<View> childAtPosition(52final Matcher<View> parentMatcher, final int position) {53return new TypeSafeMatcher<View>() {54public void describeTo(Description description) {55description.appendText("Child at position " + position + " in parent ");56parentMatcher.describeTo(description);57}58public boolean matchesSafely(View view) {59ViewParent parent = view.getParent();60return parent instanceof ViewGroup && parentMatcher.matches(parent)61&& view.equals(((ViewGroup
Espresso
Using AI Code Generation
1Espresso.onView(ViewMatchers.withId(R.id.button1)).perform(ViewActions.click());2onData(anything()).inAdapterView(withId(R.id.list)).atPosition(2).perform(click());3onData(anything()).inAdapterView(withId(R.id.list)).atPosition(2).perform(click());4onData(anything()).inAdapterView(withId(R.id.list)).atPosition(2).perform(click());5onData(anything()).inAdapterView(withId(R.id.list)).atPosition(2).perform(click());6onData(anything()).inAdapterView(withId(R.id.list)).atPosition(2).perform(click());7onData(anything()).inAdapterView(withId(R.id.list)).atPosition(2).perform(click());8onData(anything()).inAdapterView(withId(R.id.list)).atPosition(2).perform(click());9onData(anything()).inAdapterView(withId(R.id.list)).atPosition(2).perform(click());10onData(anything()).inAdapterView(withId(R.id.list)).atPosition(2).perform(click());11onData(anything()).inAdapterView(withId(R.id.list)).atPosition(2).perform(click());12onData(anything()).inAdapterView(withId(R.id.list)).atPosition(2).perform(click());13onData(anything()).inAdapterView(withId(R.id.list)).atPosition(2).perform(click());
Espresso
Using AI Code Generation
1 onView(withId(R.id.editText)).perform(typeText("Hello World"));2 onView(withId(R.id.button)).perform(click());3 onView(withId(R.id.textView)).check(matches(withText("Hello World")));4 }5 }6 @RunWith(AndroidJUnit4.class)7 public class MainActivityTest {8 public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(9 MainActivity.class);10 public void testHelloWorld() {11 onView(withId(R.id.editText)).perform(typeText("Hello World"));12 onView(withId(R.id.button)).perform(click());13 onView(withId(R.id.textView)).check(matches(withText("Hello World")));14 }15 }16 @RunWith(AndroidJUnit4.class)17 public class MainActivityTest {18 public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(19 MainActivity.class);20 public void testHelloWorld() {21 onView(withId(R.id.editText)).perform(typeText("Hello World"));22 onView(withId(R.id.button)).perform(click());23 onView(withId(R.id.textView)).check(matches(withText("Hello World")));24 }25 }26 @RunWith(AndroidJUnit4.class)27 public class MainActivityTest {28 public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(29 MainActivity.class);30 public void testHelloWorld() {31 onView(withId(R.id.editText)).perform(typeText("Hello World"));32 onView(withId(R.id.button)).perform(click());33 onView(withId(R.id.textView)).check(matches(withText("Hello World")));34 }35 }36 @RunWith(AndroidJUnit4.class)37 public class MainActivityTest {38 public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(39 MainActivity.class);40 public void testHelloWorld() {41 onView(withId(R.id.editText)).perform(typeText("Hello World"));42 onView(withId(R.id.button)).perform(click());43 onView(withId(R.id.textView)).check(matches(withText("Hello World")));44 }45 }46 @RunWith(AndroidJUnit4.class)47 public class MainActivityTest {
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!!