Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

java - Clicking button on second activity got error performing 'single click' or 'scroll to' on view

Clicking a button in Espresso test got problem. Let's say I have two activities "Activity1" and "Activity2". Click a dialog OK button in Activity1 starts Activity2 where the button in Activity2 can't be clicked.

// The current activity in testing
// .....
onView(withText(R.string.dialog_btn_ok)).perform(click()); // go to the second activity

// The button on the second activity
onView(withId(R.id.btnMP)).check(matches(isDisplayed())); // this is ok
onView(withId(R.id.btnMP)).perform(click()); // got error here

android.support.test.espresso.PerformException: Error performing 'single click' on view 'with id: ..........

Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints: at least 90 percent of the view's area is displayed to the user. Target view: "Button{id=2131296390, res-name=btnMP, visibility=VISIBLE, width=652, height=160, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=20.0, y=-16.0, text=Modify Parameter, input-type=0, ime-target=false, has-links=false}"

When I change this with perform(scrollTo()), a different error is shown.

// The button on the second activity
onView(withId(R.id.btnMP)).check(matches(isDisplayed())); // this is ok
onView(withId(R.id.btnMP)).perform(scrollTo(), click()); // got error here

android.support.test.espresso.PerformException: Error performing 'scroll to' on view 'with id ....

Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints: (view has effective visibility=VISIBLE and is descendant of a: (is assignable from class: class android.widget.ScrollView or is assignable from class: class android.widget.HorizontalScrollView)) Target view: "Button{id=2131296390, res-name=btnMP, visibility=VISIBLE, width=652, height=160, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=20.0, y=-16.0, text=Modify Parameter, input-type=0, ime-target=false, has-links=false}" at

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The solution is to create your custom GeneralClickAction with view visibility that you want to click less then Espresso's GeneralClickAction requires. Currently the minimum visibility value is 90% - see code here line 60. Set it to be 80% or less. Just copy the whole class rename it and change the value provided to this method to 80 isDisplayingAtLeast(80). Then create your click action that uses your custom GeneralClickAction:

  public static ViewAction customClick() {
      return actionWithAssertions(
          new CustomGeneralClickAction(Tap.SINGLE, GeneralLocation.VISIBLE_CENTER, Press.FINGER));
  }

But I'd rather fix the layout of the activity if possible to avoid creation of workaround for button visibility.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...