Make hasText() error more human readable.
In case if we use in our tests checks like:
. . .
control {
hasText("bla bla")
}
. . .
And this check will be failed. We'll get output like:
E/KASPRESSO: All attempts to interact for 2000 ms totally failed because of AssertionFailedError
E/KASPRESSO: junit.framework.AssertionFailedError: 'view has effective visibility=VISIBLE' doesn't match the selected view.
Expected: bla bla
Got: CustomFontTextView{id=2131362469, res-name=text_sign_in_button, visibility=VISIBLE, width=0, height=0, 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=true, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@cec54a4, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Sign in, input-type=0, ime-target=false, has-links=false}
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
Will be great to have standard hamcrest output like:
Expected: "bla bla"
Got: "ops it's devops =) "
Espresso forces "human readable" output for views at https://github.com/android/android-test/blob/master/espresso/core/java/androidx/test/espresso/matcher/ViewMatchers.java#L535 instead of using Matcher<T>.describeMismatch(Object item, Description mismatchDescription)
Possible solution is parsing error message:
if message contains Expected: with text: is "bla bla", then print only part text=ops it's devops =)
if message contains Expected: view has effective visibility=VISIBLE, then print visibility=GONE
and etc
Hi, @atsiarenia! Thank you for your submission and sorry for so delayed answer. Seems like your issue was resolved automatically with current version of espresso used in Kaspresso 1.4.2. Now it clearly states the expected and actual text, the rest of view details are printed in a separate line. See example below.
Expected: an instance of android.widget.TextView and view.getText() with or without transformation to match: is "R.string.simple_fragment_text_edittext"
Got: view.getText() was "Simple Fragment" transformed text was "Simple Fragment"
View Details: MaterialTextView{id=2131231221, res-name=text_view_title, visibility=VISIBLE, width=583, height=103, 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, layout-params=android.widget.RelativeLayout$LayoutParams@22facce, tag=null, root-is-layout-requested=false, has-input-connection=false, x=248.0, y=110.0, text=Simple Fragment, input-type=0, ime-target=false, has-links=false}
In my case using view.hasText() was a mistake. In case view contains empy text it always throws this exception:
junit.framework.AssertionFailedError: 'an instance of android.widget.TextView and view.getText() with or without transformation to match: is ""
Solution is to use containsText().