Skip to content

Commit

Permalink
Try to fix ui tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeltroger committed Nov 4, 2023
1 parent d3946ed commit a8fb0bc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ class UiTest {
.selectFirstDocument()
.goToPdfFolder()
.selectPdf(fileName = "demo.pdf")
.verifyDocumentLoaded(docName = "demo")
.verifyDocumentLoaded(docName = "demo", expectedDocumentCount = 1)
.selectAnotherDocument()
.goToPdfFolder()
.selectPdf(fileName = "demo1.pdf")
.verifyDocumentLoaded(docName = "demo1")
.verifyDocumentLoaded(docName = "demo1", expectedDocumentCount = 2)

ScreenshotUtil.recordScreenshot("multiple_documents")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.michaeltroger.gruenerpass.robots

import androidx.recyclerview.widget.RecyclerView
import android.view.View
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.contrib.RecyclerViewActions
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withTagValue
import androidx.test.espresso.matcher.ViewMatchers.withText
Expand All @@ -12,6 +11,9 @@ import com.michaeltroger.gruenerpass.utils.verifyIsDisplayed
import com.michaeltroger.gruenerpass.utils.waitUntilIdle
import com.michaeltroger.gruenerpass.utils.waitUntilNoException
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.TypeSafeMatcher

class MainActivityRobot {

Expand All @@ -25,17 +27,17 @@ class MainActivityRobot {
}
}

fun verifyDocumentLoaded(docName: String, expectQr: Boolean = false) = apply {
fun verifyDocumentLoaded(docName: String, expectedDocumentCount: Int = 1, expectQr: Boolean = false) = apply {
waitUntilNoException {
onView(withId(R.id.certificates))
.perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(0))
onView(withId(R.id.certificate))
.perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(0))
onView(
withTagValue(`is`(if (expectQr) "qr_loaded" else "pdf_loaded"))
).verifyIsDisplayed()

onView(withId(R.id.deleteIcon)).verifyIsDisplayed()
onView(withIndex(
withTagValue(`is`(if (expectQr) "qr_loaded" else "pdf_loaded")),
index = expectedDocumentCount - 1
)).verifyIsDisplayed()

onView(withIndex(
withId(R.id.deleteIcon),
index = expectedDocumentCount - 1
)).verifyIsDisplayed()
onView(withText(docName)).verifyIsDisplayed()
}
}
Expand All @@ -53,4 +55,24 @@ class MainActivityRobot {
}
return AndroidFileAppRobot()
}

private fun withIndex(matcher: Matcher<View?>, index: Int): TypeSafeMatcher<View?> {
return NullableViewTypeSafeMatcher(index, matcher)
}
}

private class NullableViewTypeSafeMatcher(
private val index: Int,
private val matcher: Matcher<View?>
) : TypeSafeMatcher<View?>() {
private var currentIndex = 0
override fun describeTo(description: Description) {
description.appendText("with index: ")
description.appendValue(index)
matcher.describeTo(description)
}

override fun matchesSafely(view: View?): Boolean {
return matcher.matches(view) && currentIndex++ == index
}
}

0 comments on commit a8fb0bc

Please sign in to comment.