-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatically scroll when needed when clicking in tests #6092
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
47 changes: 47 additions & 0 deletions
47
collect_app/src/androidTest/java/org/odk/collect/android/support/Interactions.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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.odk.collect.android.support | ||
|
||
import android.view.View | ||
import androidx.test.espresso.Espresso.onView | ||
import androidx.test.espresso.Root | ||
import androidx.test.espresso.action.ViewActions.click | ||
import androidx.test.espresso.action.ViewActions.scrollTo | ||
import org.hamcrest.Matcher | ||
import org.odk.collect.android.support.WaitFor.tryAgainOnFail | ||
|
||
object Interactions { | ||
|
||
/** | ||
* Click on the view matched by [view]. The root to use can optionally be specified with | ||
* [root] (otherwise Espresso will use heuristics to determine the most likely root). If | ||
* initially clicking on the view fails, this will then attempt to scroll to the view and | ||
* retry the click. | ||
*/ | ||
fun clickOn(view: Matcher<View>, root: Matcher<Root>? = null) { | ||
val onView = if (root != null) { | ||
onView(view).inRoot(root) | ||
} else { | ||
onView(view) | ||
} | ||
|
||
try { | ||
onView.perform(click()) | ||
} catch (e: Exception) { | ||
onView.perform(scrollTo(), click()) | ||
} | ||
} | ||
|
||
/** | ||
* Like [clickOn], but an [assertion] can be made after the click. If this fails, the click | ||
* action will be reattempted. | ||
* | ||
* This can be useful in cases where [clickOn] itself appears to succeed, but the test fails | ||
* because the click never actually occurs (most likely due to some flakiness in | ||
* [androidx.test.espresso.action.ViewActions.click]). | ||
*/ | ||
fun clickOn(view: Matcher<View>, root: Matcher<Root>? = null, assertion: () -> Unit) { | ||
tryAgainOnFail { | ||
clickOn(view, root) | ||
assertion() | ||
} | ||
} | ||
} |
24 changes: 17 additions & 7 deletions
24
collect_app/src/androidTest/java/org/odk/collect/android/support/pages/FirstLaunchPage.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,25 +1,35 @@ | ||
package org.odk.collect.android.support.pages | ||
|
||
import androidx.test.espresso.matcher.ViewMatchers.withSubstring | ||
import org.odk.collect.android.support.Interactions | ||
import org.odk.collect.strings.R.string | ||
|
||
class FirstLaunchPage : Page<FirstLaunchPage>() { | ||
|
||
override fun assertOnPage(): FirstLaunchPage { | ||
assertText(org.odk.collect.strings.R.string.configure_with_qr_code) | ||
assertText(string.configure_with_qr_code) | ||
return this | ||
} | ||
|
||
fun clickTryCollect(): MainMenuPage { | ||
return tryAgainOnFail(MainMenuPage()) { | ||
scrollToAndClickSubtext(org.odk.collect.strings.R.string.try_demo) | ||
Interactions.clickOn(withSubstring(getTranslatedString(string.try_demo))) { | ||
MainMenuPage().assertOnPage() | ||
} | ||
|
||
return MainMenuPage() | ||
} | ||
|
||
fun clickManuallyEnterProjectDetails(): ManualProjectCreatorDialogPage { | ||
scrollToAndClickText(org.odk.collect.strings.R.string.configure_manually) | ||
return ManualProjectCreatorDialogPage().assertOnPage() | ||
return clickOnString( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change should resolve this flake. |
||
string.configure_manually, | ||
ManualProjectCreatorDialogPage() | ||
) | ||
} | ||
|
||
fun clickConfigureWithQrCode(): QrCodeProjectCreatorDialogPage { | ||
scrollToAndClickText(org.odk.collect.strings.R.string.configure_with_qr_code) | ||
return QrCodeProjectCreatorDialogPage().assertOnPage() | ||
return clickOnString( | ||
string.configure_with_qr_code, | ||
QrCodeProjectCreatorDialogPage() | ||
) | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using this helper is advisable over the other
clickOn
as it seems there's some flakiness in Espresso'sViewActions.click
that are resolved with this "try again on fail" approach.