Reputation: 4888
I'm testing the launch of a fragment inside my activity, so after performing a click on button that going to launch the fragment, I tested the existing of a text on view inside the launched fragment, but the test fail even though that fragment is launched on my phone, and even in the View Hierarchy is showing that the text exist :
View Hierarchy:
+--------->AppCompatTextView{id=2131886318, res-name=text3_textView, visibility=VISIBLE, width=768, height=68, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=695.0, text=Pour finaliser votre inscription nous avons besion
d'une photo de profil, input-type=0, ime-target=false, has-links=false}
The test fail here :
onView(withText("photo de profil")).check(matches(isDisplayed()));
I'm wondering why espresso fail this test, is it because it doesn't wait for the launch of the fragment?
Btw I turned animations off.
Upvotes: 16
Views: 36947
Reputation: 28865
In case you try to access some view that wasn't showed you would see this error. For instance, you want to click some button but a whole screen wasn't showed.
Then you can simply use try-catch
block around the action. It can be applied to Kakao
library and, I think, Espresso
,
Upvotes: 0
Reputation: 3190
What I was doing:
@Rule
@JvmField
var mActivityTestRule = ActivityTestRule(SplashScreenActivity::class.java)
Due to this rule, the Espresso Library was searching for the login EditText
view on the SplashScreenActivity
. The sad part was, this login EditText
view was actually present in the LoginActivity
as a result the test cases failed with the above-mentioned error.
What I did to make everything work:
@Rule
@JvmField
var mActivityTestRule = ActivityTestRule(LoginActivity::class.java)
As soon as I changed the Activity
from SplashScreenActivity
to LoginActivity
in the above rule. Espresso easily found this login EditText
view and all the tests passed.
Just make sure you are pointing Espresso to the correct Activity
/Fragment
where the View
is actually present.
Upvotes: 3
Reputation: 4296
The espresso withText method match that all the string is equal.
In your case you need to match if the string ends with your string.
Your code should be this:
onView(withText(endsWith("photo de profil"))).check(matches(isDisplayed()));
Here you have more examples: http://qathread.blogspot.com.br/2014/01/discovering-espresso-for-android.html
Upvotes: 13