0

I trying to do automation test on android application with Espresso & Java and I want to input text into base_input_view_input in RecycleView in which text of base_input_view_title equal "First name" with layout inspector as bellow:

Like this

I tried with this solution but it not work.

onView(withId(R.id.rvBasicInfo))
.perform(RecyclerViewActions.actionOnItem(allOf(withId(R.id.base_input_view_input),allOf(withId(R.id.base_input_view_title),withText("First name"))),typeText("aaaa")));

Here is the log

Caused by: androidx.test.espresso.PerformException: Error performing 'scroll RecyclerView to: holder with view: (view.getId() is
<2131296360/com.test.a.vvv.app.ccc.ccc:id/base_input_view_input> and (view.getId() is
  <2131296362> and an instance of android.widget.TextView and view.getText() with or without transformation to match: is "First name"))' on view 'RecyclerView{id=2131296919, res-name=rvBasicInfo, visibility=VISIBLE, width=1080, height=1595, has-focus=false, has-focusable=true,
    has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@eb7851f, tag=null, root-is-layout-requested=false,
    has-input-connection=false, x=0.0, y=0.0, child-count=7}'.

Please help me to correct the code. Thank you so much!

TobiSH
  • 2,833
  • 3
  • 23
  • 33
Sarah
  • 1
  • 1

1 Answers1

0

Your matcher is not selecting the correct element. You are currently trying to select an element that has an Id of base_input_view_input and is an element that has an Id of base_input_view_title with the text First name. Obviously, that wouldn't work, since the element you want doesn't have two different Ids! Instead, we should focus on parent/child/sibling relationships between the elements.

For me, it is helpful to pick the most specific part (usually an ID), and work backwards to identify what makes this element truly unique.

allOf(
  withId(R.id.base_input_view_input), 
  isDescendantOfA(
    allOf(
      withId(R.id.relInputView), 
      hasSibling(
        allOf(
          withId(R.id.base_input_view_title), 
          withText("First name")
        )
      )
  )
)

(Parentheses might be off on that...)

I've set up the line breaks to show what level of matching they are at, since things are a little confusing. We want to find:

  • an element with the Id of base_input_view_input
  • AND is a descendant of an element which has...
    • an Id of relInputView
    • AND has a sibling with an element...
      • with an Id of base_input_view_title
      • AND has the text of First name

Since you have multiple elements with the Id of base_input_view_input, we need something more specific. If the next thing that makes this unique is that it's parent element (R.id.relInputView) has a sibling with specific text (in this case, First name), then we should check for an element that has those.

You could also abstract this matcher into a function to more easily re-use it for when the base_input_view_title has different text.

agoff
  • 5,818
  • 1
  • 7
  • 20
  • 1
    Thank you so much for your support! I tried with your suggestion and it's worked! – Sarah Apr 20 '21 at 18:18
  • No problem! I find it is usually easier to get the specific view and then work my way up/down the other views in the layout inspector. – agoff Apr 20 '21 at 19:54
  • 1
    I have one more question: I have a robot keyword (Keyword in Robot Framework) to call API to create new Customer Can I call this keyword in Espresso test case ? – Sarah Apr 22 '21 at 05:07
  • Sorry @Sarah, I don't have significant enough experience with the Robot Framework to answer that. – agoff Apr 22 '21 at 12:59
  • 1
    Thank you @agoff. I found the solution for it – Sarah Apr 26 '21 at 07:16
  • HI @agoff, I have one more quesstion. I want to reset application or clean cache of application before/after run my espresso test cases. How can I do that? – Sarah May 15 '21 at 18:01
  • On my current app, we use `testInstrumentationRunnerArguments clearPackageData: true` in the `build.gradle`. https://stackoverflow.com/questions/31713579/android-studio-clear-application-data-for-instrumentation-test/57461180#57461180 – agoff May 17 '21 at 13:39