5

I am trying to find a way to tap a link with the following HTML structure:

<a>
  <div id="facebook_sharing"></div>
</a>

The a tag does not have any attributes or text associated with it, just the div tag inside.

I have tried the following but with no success:

app.links["facebook_sharing"].tap()

Is there a way to find the div tag element and tap on it?

Thanks!

Audible
  • 235
  • 3
  • 10

1 Answers1

18

Xcode UI Testing interacts with your app via accessibility. Just like it doesn't have access to the underlying UIKit elements, it doesn't have direct access to the HTML DOM. You cannot expect the framework to know about something in your markup unless that gets exposed to accessibility in some way.

To "see the app as UI Testing does" open Accessibility Inspector.app on your mac. (You can easily find this by searching for it in Spotlight.) Then, hover over the div with your cursor. If you see any of the following attributes set you can query for the element:

  1. accessibilityTitle
  2. accessibilityPlaceholderValue
  3. accessibilityLabel
  4. accessibilityValue

If none of these are set on the a or div elements you should update them yourself. Think of it this way. If someone who is visually impaired uses the site, how will he/she know that he/she can interact with the Facebook link?

Joe Masilotti
  • 16,815
  • 6
  • 77
  • 87
  • 1
    Thank you for the great explanation. When I hover over the `div` tag within the Safari Console I see the `accessibilityValue` set as `facebook_sharing`. However, when I hover over the WebView within the iOS Simulator I can only see the the `accessibilityTitle` set as my WebView. I am able to access my WebView using `app.otherElements["MyWebViewAccessibilityLabel"]` but I am not able to access the `div` element using `app.otherElements["facebook_sharing"]`. Likewise, `app.otherElements["facebook_sharing"]`.exists returns false. I know I must be doing something wrong here. – Audible Feb 17 '16 at 17:40
  • I would like to add that when viewing the WebView with the iOS Simulator's _Accessibility Inspector_ turned on, I can only see the Accessibility Label for the WebView itself, nothing else is "clickable." If I do this within Safari at google.com I can click on all the links within the page as expected. – Audible Feb 17 '16 at 21:04
  • In this answer, we assume testing is going on with simulator, correct? As opposed to a real device? – Vish May 10 '16 at 04:09
  • 1
    how to add identifier on web so, on iOS side using accessibility app we can access it and use for automation – Jasmin Dec 21 '18 at 07:45
  • it should be mentioned that if you are a hybrid developer, these setting will in no way be set and there is no way to set them from your html/javascript (unless I'm mistaken). The only items I was able to query were, buttons, and staticTexts, and links. I'm not saying this list is exhaustive but at least you get the idea how limited you are. – Post Impatica Jan 10 '20 at 18:31
  • ...images and otherElements too. – Post Impatica Jan 14 '20 at 13:20