I want to perform a click on an item in my preference activity. I have tried following code,
onData(withKey("preference-key")).perform(click());
But it throws an exception like,
Caused by:
android.support.test.espresso.AmbiguousViewMatcherException: Multiple
Ambiguous Views found for matcher is assignable from class: class
android.widget.AdapterView
How can I properly perform a click on this item?
First of all, AmbiguousViewMatcherException as you notice means that you have at least two views with the same id, key, text, contentDescription etc.
Your code tells me that you're trying to write AdapterView test like you would have one view, so you try to use onData matcher like it was onView matcher. Sorry, but AdapterView tests are never as simple.
Instead of
onData(withKey("preference-key")).perform(click());
write code like this
onData(anything())
.inAdapterView(allOf(
isDescendantOfA(withId(R.id.fragment1)),
withId(R.id.listview)))
.atPosition(4)
.perform(click());
Look for AmbiguousViewMatcherException Espresso issues on StackOverflow. It's quit often problem, maybe someone had view like yours.
Related
I'm writing automated tests for my company's app. The tests use a combination of Espresso and Robotium. Several of those tests are supposed to check whether certain ReactImageView elements are displaying a specific image.
I first tried using hasBackground() as follows:
onView(...).check(matches(hasBackground(R.drawable...)));
However, this returns a NoMatchingViewException.
I then tried Daniele Bottillo's matcher as described here: https://medium.com/#dbottillo/android-ui-test-espresso-matcher-for-imageview-1a28c832626f
onView(allOf(..., new DrawableMatcher(R.drawable....), isDisplayed())).check(matches(isDisplayed()));
But this didn't work either:
java.lang.ClassCastException: com.facebook.drawee.generic.RootDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
Lastly, I tried Frankie Sardo's solution: Android Espresso match BitmapDrawables with different tint
onView(allOf(..., withImageDrawable(R.drawable...), isDisplayed())).check(matches(isDisplayed()));
However, this again throws a NoMatchingViewException. I'm running out of ideas. Is it even possible to check what resource is attached to an image view?
The easiest way would be to use Barista library and its assertHasDrawable(R.id.<imageview_id>, R.drawable.<drawable_id>) assertion function.
I'm using UIAutomator test framework for long tests (concerning to my acceptance test). And I need to wait until some activity is started.
I decided to use By.clazz (android.support.test.uiautomator package) methods to find activity object. I expected that something like
uiDevice.wait(Until.findObject(By.clazz(SomeActivity.class)), 30000);
will work. But it doesn't. I suppose that object of my activity cannot be found. I tried to use other By.clazz methods with different params but without success.
So, my code is pretty simple:
UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
/*.... do something...
like click on buttons which will open some activities...
*/
//does not work, time value just for sample
uiDevice.wait(Until.findObject(By.clazz(SomeActivity.class)), 30000);
I found workaround solution with using By.res, like
uiDevice.wait(Until.findObject(By.res(BASIC_PACKAGE, "someMainIdInSomeFragment")), 30000);
But I have very complicated structure of the app with base activities and so on. I often have the same layout for different activities with load different fragments. So I need to know that we started exactly SomeActivity ,regardless of loaded fragments.
So, the questions are:
Is it possible to use By.clazz for Activity to find its object?
Is there some another way to find activity object with UIAutomator?
Did I do everything right? Or maybe there are some mistakes? Is it possÑ–ble to do with UiAutomator?
Thanks!
Using class with UiObject2
Find the EditText, make sure to click on it (legacySetText would do it implicitly), and set your text.
val input = By.clazz(EditText::class.java.canonicalName)
device.wait(Until.findObject(input), TIMEOUT).apply {
click()
text = "your_text"
}
Yes, could be done through the id.
// android:id="#+id/widget_id"
By.res(applicationId, "widget_id")
Your syntax seems good to me. Just make sure no spinner (or any other widget) is blocking your view during the click attempt.
I have this simple Espresso interaction:
onView(atIndex(withId(R.id.editTextTextWidget), 0)).inRoot(authViewRootMatcher)
.check(matches(allOf(isDisplayed(), isEnabled())))
.perform(typeText("1"));
The check(matches(allOf(isDisplayed(), isEnabled()))) passes as expected, but the following perform(typeText("1")) does not. I cannot figure out why, for the life of me.
So, I can't believe I'm asking this, but how in the name of Android do I use Espresso to type text into my EditText whose ID is R.id.editTextTextWidget?
I fixed the problem by splitting the check(...) call and perform(...) call:
onView(atIndex(withId(R.id.editTextTextWidget), 0)).inRoot(authViewRootMatcher)
.check(matches(allOf(isDisplayed(), isEnabled())));
onView(atIndex(withId(R.id.editTextTextWidget), 0)).inRoot(authViewRootMatcher)
.perform(typeText("1"));
For some reason this works, and the original doesn't. #GooglePlz
I'm currently trying to get Espresso to match a UIElement by it's class and its text since it currently does not have a resource ID (I know, I know...). I'm not sure what the proper syntax for this is since the Espresso documentation is fuzzy (I'm VERY new to this and programming in general so I'm sure I'm missing something). Here's what I have so far:
onView(allOf(instanceOf(android.widget.CheckBox)), withText("S"))).
perform(scrollTo()).
check(matches(isChecked()));
I've tried typing just "Textbox" but in both cases I get an "Expression Expected" error. As of now this is the only way to identify this element so any pointers will help. Thanks!
Here' s my example:
onView(allOf(instanceOf(Toolbar.class), withChild(withText(R.string.action_settings))))
.check(matches(isDisplayed()));
so I guess that in your test would be
onView(allOf(instanceOf(android.widget.CheckBox.class)), withText("S"))).
perform(scrollTo()).
check(matches(isChecked()));
Hope it will help
I have a viewpager and I want to check for an imageview in it. I have used following code but its not working.
onView(allOf(withId(R.id.img_flag), hasSibling(withText("some
text"))));
this one always returns true
onView(withId(R.id.img_flag)).check(doesNotExist());
this one throws AmbiguousViewMatcherException
I don't know what you're going to check, but to resume:
The first one is always true, because img_flag would be certainly always (I mean: until you put it in another ViewGroup) a sibling to a TextView with some text. It's like mathematical 2+2=4.
So this test only checked if img_flag has sibling with "some text".
AmbiguousViewMatcherException means only that there are more than one view with img_flag id. Espresso don't know which of them shoul not exist so it informs about it.
If you wanna fix the second one please read about onData() Espresso matcher.
Visit also these sites:
https://guides.codepath.com/android/UI-Testing-with-Espresso
https://google.github.io/android-testing-support-library/docs/espresso/index.html
On the second one you would find Espresso Cheat Sheet, where struct of onData() matcher is enough explained
If you have more quesrtions, please feel free to ask.