Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 21966

EarlGrey - how to find any table view cell with a certain text inside

I have a custom UITableViewCell with a label inside. Both the cell and the label have an accessibility identifier, and I am trying to assert that in the UI there is at least one cell that contains a specified text. I am writing the assertion this way:

EarlGrey
    .selectElement(with: grey_text(self.customTestString))
    .using(searchAction: grey_scrollInDirection(GREYDirection.down, 45), onElementWithMatcher: grey_kindOfClass(UILabel.self))
    .assert(grey_sufficientlyVisible())

The property customTestString contains the string I am looking for. I tried also to look for my UITableViewCell custom subclass but in any case I get this failure:

Exception: MultipleElementsFoundException

Exception Name: MultipleElementsFoundException
Exception Reason: Multiple UI elements matched for given criteria.
Exception with Assertion: {
  "Assertion Criteria":  "assertWithMatcher:matcherForSufficientlyVisible(>=0.750000)",
  "Element Matcher":  "((kindOfClass('UILabel') || kindOfClass('UITextField') || kindOfClass('UITextView')) && hasText('TEST_STRING'))",
  "Recovery Suggestion":  "Create a more specific matcher to narrow matched element"
}

Exception Details: Search action: <GREYScrollAction: 0x600001a44b40>. 
Search action element matcher: kindOfClass('iComplain.ComplaintTableViewCell').
Error Trace: [
  {
    "Description":  "Multiple elements were matched: (
    "<UILabel:0x7fa653455880; AX=Y; AX.label='TEST_STRING'; AX.frame={{15, 209}, {85, 16}}; AX.activationPoint={57.5, 217}; AX.traits='UIAccessibilityTraitStaticText'; AX.focused='N'; frame={{15, 32}, {85, 16}}; alpha=1; UIE=N; text='TEST_STRING'>",
    "<UILabel:0x7fa653455600; AX=Y; AX.label='TEST_STRING'; AX.frame={{15, 182}, {107.5, 20.5}}; AX.activationPoint={68.75, 192.25}; AX.traits='UIAccessibilityTraitStaticText'; AX.focused='N'; frame={{15, 5}, {107.5, 20.5}}; alpha=1; UIE=N; text='TEST_STRING'>"
). Please use selection matchers to narrow the selection down to single element.",
    "Error Domain":  "com.google.earlgrey.ElementInteractionErrorDomain",
    "Error Code":  "5",
    "File Name":  "GREYElementInteraction.m",
    "Function Name":  "-[GREYElementInteraction grey_errorForMultipleMatchingElements:withMatchedElementsIndexOutOfBounds:]",
    "Line":  "965",
    "TestCase Class":  "iComplainTests.EarlGreyComplaintUITests",
    "TestCase Method":  "testWriteComplaint"
  }
]

As far as I understand it's complaining about the fact that the assertion is ambiguous because it can be run on multiple cells, but I don't know how to solve it. Any hint?

Upvotes: 0

Views: 702

Answers (1)

sharan
sharan

Reputation: 1

grey_scrollInDirection expects a direction and the matcher for an element in the view hierarchy that you can scroll on. Try passing the accessibility identifier for the table view in the grey_scrollInDirection method. For more information - you can refer to this https://github.com/google/EarlGrey/blob/master/docs/api.md#selecting-off-screen-ui-elements.

I would suggest using the accessibility identifier of the UI element as a matcher for selectElement method as that is highly recommended and since you mention that both the cell and the label inside it have accessibility identifiers. To narrow down to the element you are looking for on the list of UI elements in the view hierarchy, you can form your matcher for selecting the element (label) using the grey_allOf matcher and passing it an array of matchers like this -

EarlGrey
        .selectElement(with: grey_allOf([grey_accessibilityID(labelID), grey_ancestor(grey_accessibilityID(cellID)), grey_sufficientlyVisible()]))
        .usingSearch(action: grey_scrollInDirection(.down, 45), onElementWith: grey_accessibilityID(tableViewID))
        .assert(grey_notNil())

The above code looks for an element - which has an identifier labelID, that has an ancestor with an identifier cellID (your tableViewCell), and which is a sufficientlyVisible element in the view hierarchy by scrolling on the table view with identifier tableID. The usingSearch method repeatedly scrolls 45 points down on the tableView with identifier tableID, either until it finds the element and assertion succeeds or it reaches the end of the scroll view and the assertion fails. You can refer the selection API for more information here - https://github.com/google/EarlGrey/blob/master/docs/api.md#selection-api.

Upvotes: 0

Related Questions