Home > Back-end >  if else case in Android Espresso UI testing
if else case in Android Espresso UI testing

Time:03-31

In the application, I am writing Espresso UI test for login flow in the app. I wrote the test and it works fine, but I have one case where I have problem.

In this application, users sometimes get bonuses when opening app (usually they don't but this is let's say 1:100 scenario) In these situations, the pop up appears on the screen and my test fails because my code tries to trigger the button that is not visible (it is under the pop up).

I would like to write if-else in a test where I can for example check the visibility of the pop up close button. So if this is visible click on it and if it's not, then continue normally.

But I didn't find any if-else syntax in UI testing. Can someone with more experience in UI espresso testing help me with some advice here? THanks

CodePudding user response:

Create two tests. One where bonus popup cannot appear and one where bonus popup always appears. This way so can test both behaviours.

You should not leave anything to a chance, and your issue is exactly the example of why not.

CodePudding user response:

I agree with Primož that you should not have an indeterminate test. But, if that is not possible, you could always use a try/catch with a specific exception to attempt to close the popup before the test.

Something like this might work (may need to tweak the catch depending on the exact exception throw when you do not have the popup view.

try {
  onView(withId(R.android.foo)).perform(click())
} catch (e: NoSuchViewException) {
 // code to be executed if view does not exist
}
  • Related