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

问题描述:

在XCode 7.2中,如果我能得到XCUIApplication()。scrollViews [“Foo”] .otherElements.otherElements [“Page Title”],那么说“给我这个名为”Foo“的scrollView的页面标题, ?”如何获取Xcode 7.2 XCTestCase中滚动视图的标签?

我试过要求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文章”)

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

它看起来像你的页面标题是不从UILabel继承的自定义视图(UILabel后裔标记为StaticTexts)。

您需要在应用的代码中为您的页面标题设置一个辅助功能标识(不是标签)。

pageTitle.accessibilityIdentifier = "Page Title" 

这应该让你的代码工作,并且是最好的做法。

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

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