在 XCUITest 中获取 Button 状态

Posted

技术标签:

【中文标题】在 XCUITest 中获取 Button 状态【英文标题】:Getting Button state in XCUITest 【发布时间】:2016-08-16 22:35:48 【问题描述】:

如何在 XCUITest 中获取按钮状态?

它只是一个充当切换按钮的 NSButton。单击时图像会发生变化。在应用程序中,我检查返回 0 或 1 的 button.state。取决于我更改图像的值。

@IBAction func checkBoxClick(sender: NSButton) 
    print("Checkbox state ", sender.state)

但是在编写 XCUITest 自动机代码时,我无法获取按钮的状态。没有像状态这样的属性。此外,当我采用按钮的“值”或“选定”属性时,它总是给出 nil 和 false。

let window = XCUIApplication().windows["Window"]
    let button = window.buttons["Button"]
    button.click()
    print("Button selected ",button.selected)
    print("Button selected value ",button.value)
    button.click()
    print("Button unselected ",button.selected)
    print("Button unselected value ",button.value)

打印的值是

Button selected  false
Button selected value  nil
Button unselected  false
Button unselected value  nil

任何输入。

【问题讨论】:

【参考方案1】:

在您的应用程序代码中,您可以将 0/1 状态映射到选中/未选中。默认状态应该是假的,当它被切换时,反转sender.selected的状态。

selected 属性代表两种状态,并提供与您添加的.state 相同的功能。

【讨论】:

感谢您的回复。 我确实喜欢这个 if (tempButton.state == NSOnState) tempButton.setAccessibilitySelected(true) else tempButton.setAccessibilitySelected(false) 这就是你的建议。它正在使用上面的代码 与我所说的略有不同,但很好的解决方案 - 可能是更好的方法 TBH。 :)

以上是关于在 XCUITest 中获取 Button 状态的主要内容,如果未能解决你的问题,请参考以下文章

XCUITest 等待未经测试的应用程序达到空闲状态

带有 PACT 的 XCUITest 不返回模拟网络响应

XCUITest 预测试设置

使用 Cucumberish 在 XCUITest 设置中重置应用程序

为啥 XCUITest 对 iOS 应用程序运行太慢?

给定 XCUITest 中的文本,如何获取单元格的索引?