-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implementing expresso testing on recycler view
- Loading branch information
1 parent
9f5d2bb
commit 14e67b0
Showing
3 changed files
with
72 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 41 additions & 11 deletions
52
app/src/androidTest/java/me/joaovictorsl/adafreetoplaygames/ExampleInstrumentedTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} |