Xcode UI 测试 [xcode7-beta6] - 使用可访问性标签时断言实际标签值

Xcode UI Testing [xcode7-beta6] - Asserting actual label values when using accessibility labels

问题其实很简单:

在这个对象上使用可访问性标签时,有没有办法断言特定标签(例如UI标签)的显示值?

据我所知,示例中的所有断言(例如 XCTAssertEquals),无论是来自 WWDC Talk or Blogposts,都只是检查元素是否存在于 XCTAssertEquals(app.staticTexts["myValue"].exists, true) 之类的查询中或者 table 中的单元格数是否正确 XCTAssertEquals(app.tables.cells.count, 5)。因此,当避免使用可访问性标签时,可以检查对象是否显示了某个值,而不是检查哪个对象/元素。 当使用可访问性标签时,它剥夺了我查询显示值的机会,因为 app.staticTexts["myValue"] 现在无法提供结果,但 app.staticTexts["myAccessibilityLabel"] 会命中。

假设我想测试我的 "Add new Cell to Table" 功能,我可以测试是否真的有一个新单元格添加到列表中,但我不知道新单元格是添加在顶部还是底部列表中的某处或介于两者之间。

对我来说,在 UI 测试时,检查特定元素是否具有特定值的简单方法应该是不费吹灰之力的。

可能由于缺少文档,我可能忽略了显而易见的内容。如果是这样,请告诉我。

我认为您问的是几个不同的问题,所以我会尝试单独回答每个问题。

  1. Is there a way to assert the displayed value from a specific label (e.g. UILabel) when using an accessibility label on this object?

简而言之,没有。 UI 测试通过连接到可访问性 API 来工作,因此您只能基于此查询对象。但是,您可以检查某些元素(例如控件)的 -value 属性。这用于测试开关是打开还是关闭。请注意,这些也归结为使用可访问性 API,只是一种不同的方法(-accessibilityValue 而不是 -accessibilityIdentifier-accessibilityLabel)。

  1. ...but I have no idea if the new cell is added at the top or the bottom of the list or somewhere in between.

要查询 XCUIElement 的框架,您可以使用公开 -frame 的新 XCUIElementAttributes 协议。例如:

let app = XCUIApplication()
app.launch()

app.buttons["Add New Cell to Table"].tap()

let lastCell = app.cells["Last Cell"]
let newCell = app.cells["New Cell"]

XCTAssert(newCell.exists)
XCTAssert(newCell.frame.minY > lastCell.frame.maxY)
  1. For me, an easy way to check if a specific element has a certain value should be a no-brainer when it comes to UI Testing.

如果您从可访问性的角度考虑一切,这就不是问题了。 UI 测试只能通过可访问性 API 与您的元素交互,因此您必须实施它们。启用这些设置后,您还可以获得额外的好处,即让您的应用更易于用户访问。

尝试将单元格的 -accessibilityLabel-accessibilityIdentifier 设置为显示的文本。 UI 关于使用哪一个测试可能很挑剔。

  1. It is possible that due to the missing documentation I might overlook the obvious. If so, just tell me.

XCTest 和 UI 测试没有任何官方文档。所以我已经从框架中公开的头文件中提取了我自己的。请注意,即使它们是从源代码中提取的,它们也是非官方的。

XCTest / UI Testing Documentation

从 apple 论坛看来可以获取标签的值:

The only way I've found is to not set an Accessibility Label, but use identifier instead. Then XCUIElement.label will change to match the current text of the label.

However there is a gotcha: if you have previously set Accessibility Label in XC, and remove it, an entry setting the label to "" remains in the storyboard. In this case, not only will calling .label will return "", but you won't be able to query for the label by it's text!

The only thing you can do is delete and re-add the label, or manually edit the xml.

lastobelus - https://forums.developer.apple.com/thread/10428

请务必在设置 .text 值时设置 UILabel 的 .accessibilityValue 属性。然后在 UITest 中,您可以像这样测试可访问性值:

    let labelElement = app.staticTexts["myLabel"]
    ...
    XCTAssertEqual(labelElement.value as! String, "the expected text")

对我有用的是将 UILabel 的可访问性标识符设置为 MyLabel

func myLabelText() -> String {
  let myLabelUIElement: XCUIElement = self.application.staticTexts["MyLabel"]

  return myLabelUIElement.label
}

测试 Xcode 8 和 iOS 10