Search code examples
swiftxctestxcode7.2

How can I get the label for a scroll view in a Xcode 7.2 XCTestCase?


In XCode 7.2, if I can get XCUIApplication().scrollViews["Foo"].otherElements.otherElements["Page Title"], what's the way to say, "Give me the page title for this scrollView named "Foo"?"

I have tried asking for the accessibilityLabel and staticTexts.

I have tried adding XCUIApplication().scrollViews["Foo"], XCUIApplication().scrollViews["Foo"].otherElements, etc to my watch list but I don't see any useful information.

When I do a po on XCUIApplication().scrollViews["Foo"].otherElements, I get:

<snip>
↪︎Find: Descendants matching type Other
Output: {
      Other 0x7f904b60e910: traits: 8589934592, {{0.0, 0.0}, {768.0, 1024.0}}, label: 'PDF article'
      Other 0x7f9049efb6e0: traits: 8589934592, {{0.0, 0.0}, {768.0, 1024.0}}
    }

(Page title in this case is 'PDF article')

So I feel like I should be able to access it with staticTexts at least.


Solution

  • It looks like your page title is a custom view that doesn't inherit from UILabel (UILabel descendants are marked as StaticTexts).

    You need to set an accessibility identifier (not label) on your page title in the app's code.

    pageTitle.accessibilityIdentifier = "Page Title"
    

    This should make your code work and would be the best practice.

    Alternatively, if you change your search chain to use the value of the label instead of "Page Title", that should work too. However, if you open a different page with a different title, this won't work.

    XCUIApplication().scrollViews["Foo"].otherElements["PDF article"]