Skip to content

Commit

Permalink
implementing expresso testing on recycler view
Browse files Browse the repository at this point in the history
  • Loading branch information
on-ferreira committed May 17, 2023
1 parent 9f5d2bb commit 14e67b0
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 11 deletions.
24 changes: 24 additions & 0 deletions .idea/androidTestResultsUserPreferences.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,19 @@ dependencies {
implementation "androidx.room:room-runtime:$roomVersion"
kapt "androidx.room:room-compiler:$roomVersion"

implementation 'androidx.recyclerview:recyclerview:1.2.1'


implementation "androidx.room:room-ktx:$roomVersion"
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.5.1'


}
Original file line number Diff line number Diff line change
@@ -1,24 +1,54 @@
package me.joaovictorsl.adafreetoplaygames

import androidx.test.platform.app.InstrumentationRegistry
import androidx.recyclerview.widget.RecyclerView
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.contrib.RecyclerViewActions
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4

import androidx.test.platform.app.InstrumentationRegistry
import me.joaovictorsl.adafreetoplaygames.ui.gamelist.GameListActivity
import me.joaovictorsl.adafreetoplaygames.ui.recyclerview.adapter.GameAdapter
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
class GameListActivityTest {

@get:Rule
val activityRule = ActivityScenarioRule(GameListActivity::class.java)

@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("me.joaovictorsl.adafreetoplaygames", appContext.packageName)
fun scrollToAndClickItem() {
ActivityScenario.launch(GameListActivity::class.java).use { scenario ->
// Delay for RecyclerView to populate with data
Thread.sleep(2000)

// Replace `desiredPosition` with the index of the desired cell you want to scroll to and click
val desiredPosition = 10

// Scroll to the desired position
Espresso.onView(ViewMatchers.withId(R.id.game_list_recycler_view))
.perform(
RecyclerViewActions.scrollToPosition<GameAdapter.GameViewHolder>(desiredPosition)
)

// Click on the desired cell
Espresso.onView(ViewMatchers.withId(R.id.game_list_recycler_view))
.perform(
RecyclerViewActions.actionOnItemAtPosition<GameAdapter.GameViewHolder>(
desiredPosition,
ViewActions.click()
)
)
}
}
}
}

0 comments on commit 14e67b0

Please sign in to comment.