Xcode UI Test - How to Find and tap() an Element Inside a WebView
Asked Answered
S

1

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!

Straub answered 16/2, 2016 at 21:6 Comment(0)
K
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?

Kaylee answered 17/2, 2016 at 11:39 Comment(6)
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.Straub
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.Straub
In this answer, we assume testing is going on with simulator, correct? As opposed to a real device?Litigation
how to add identifier on web so, on iOS side using accessibility app we can access it and use for automationAngelineangelique
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.Cash
...images and otherElements too.Cash

© 2022 - 2024 — McMap. All rights reserved.