如何在 Xcode 7.2 XCTestCase 中获取滚动视图的标签?

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

在XCode7.2中,如果我能得到XCUIApplication().scrollViews["Foo"].otherElements.otherElements["Page Title"],怎么说呢,"给我这个名为 "Foo"?"

的 scrollView 的页面标题

我试过请求 accessibilityLabel 和 staticTexts。

我尝试将 XCUIApplication().scrollViews["Foo"]、XCUIApplication().scrollViews["Foo"].otherElements 等添加到我的观察列表,但我没有看到任何有用的东西信息。

当我在 XCUIApplication().scrollViews["Foo"].otherElements 上执行 po 时,我得到:

<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}}
    }

(本例中的页面标题为 'PDF article')

所以我觉得我至少应该能够通过 staticTexts 访问它。

看起来你的页面标题是一个自定义视图,它没有继承自 UILabel(UILabel 后代被标记为 StaticTexts)。

您需要在应用程序代码中的页面标题上设置可访问性 标识符(而非标签)。

pageTitle.accessibilityIdentifier = "Page Title"

这应该能让您的代码正常工作,并且是最佳实践。

或者,如果您将搜索链更改为使用标签的值而不是 "Page Title",那也应该有效。但是,如果您打开具有不同标题的不同页面,这将不起作用。

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