K_ID
K_ID

Reputation: 103

UiAutomator cannot find UiObject that on a screen

I trying to click on a button with UiAutomator, but receive error "androidx.test.uiautomator.UiObjectNotFoundException"

I tried to locate object in two ways.

  1. UiObject cartButton = uiDevice.findObject(new Selector().resourceId("R.id.group_cart_add_button"));
  2. UiObject2 cartButton = uiDevice.findObject(By.res(InstrumentationRegistry.getInstrumentation().getTargetContext().getPackageName(), "R.id.group_cart_add_button"));

Then I use cartButton.click() but both times I receive an error.

In hierarchy this object is exist enter image description here

And when I try to find it in Evaluate tool, I can do it:enter image description here

But if I try to click, I receive an error:enter image description here

Why?

Upvotes: 1

Views: 3369

Answers (1)

Lino
Lino

Reputation: 6160

You can do the following:

String packageName = "your-app-package-name"
String fullCartButtonResourceId = packageName + ":id/group_cart_add_button";
UiObject2 cartButton = mDevice.findObject(By.res(uk));

or

UiObject cartButton = findObject(new UiObject(new UiSelector().resourceId(fullCartButtonResourceId));

Upvotes: 2

Related Questions