UI 测试 - isSelected 总是返回 false [英] UI Tests - isSelected is always returning false

查看:35
本文介绍了UI 测试 - isSelected 总是返回 false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们最近使用 Xcode 8.2.1 (8C1002) 将 Swift 2.3 项目更新为 Swift 3,现在大多数与 tableViews 和 isSelected 属性相关的 UI 测试都不起作用.它总是返回 false,即使选择了对象(我们可以在 iOS 模拟器中看到它).

We have updated out Swift 2.3 project to Swift 3 recently using Xcode 8.2.1 (8C1002), and now most of our UI Tests related with tableViews and the isSelected property aren't working. It's always returning false, even when the object is selected (we can see it in the iOS Simulator).

有没有人遇到过类似的问题?我们的代码在转换之前可以在 Swift 2.3 中正常工作.以下是我们检索 tableView 单元格的方法:

Has anyone experienced similar issues? Our code used to work normally in Swift 2.3 before the conversion. Here is how we retrieve a tableView cell:

let cell = app.tables.cells.element(at: 4)

注意: app 是一个 XCUIApplication.

Note: app is a XCUIApplication.

这里是我们如何检查它是否被选中:

And here is how we check if it's selected or not:

XCTAssert(cell.isSelected)

另一个观察结果是我们确定对象存在,因为 waitForExpectations 返回 true:

Another observation is that we are sure that the object exists because waitForExpectations is returning true:

let existsPredicate = NSPredicate(format: "exists = 1")
expectation(for: existsPredicate, evaluatedWith: cell, handler: nil)
waitForExpectations(timeout: 20, handler: nil)

为了替换 isSelected,我尝试将 NSPredicate 与 selected = 1isSelected = 一起使用1.没有一个工作.我还尝试在其他问题的答案中使用 accessibilityValue ,但是它并不是那么简单,因为有时我的表格视图中的项目是以编程方式选择/取消选择的.此外,该方法涉及向应用添加测试代码,这不是一个好的做法.

In order to replace isSelected, I've tried to use NSPredicate with selected = 1 and with isSelected = 1. None worked. I also tried to use acessibilityValue based in other question's answer, however it wasn't that simple since sometimes the items in my table view are selected/unselected programatically. Also, that method involved adding test code to the app, which isn't a good practice.

在赏金结束后 由于没有人能够找到该问题的解决方案,而且这显然是 Xcode 中的一个错误,因此我已向 Apple 提交了一份错误报告.当他们发布带有修复程序的 Xcode 版本时,我会在这里发表评论.

EDIT AFTER BOUNTY END: Since no one could find a solution for that problem and that's obviously a bug in Xcode, I've submitted a bug report to Apple. I will comment here when they release an Xcode version with the fix.

额外在我上次编辑后的一天,dzoanb 提供了一个实用的答案.

EXTRA One day after my last edit, dzoanb came with a functional answer.

推荐答案

我做了一些测试和一些研究.您可以查看为此目的创建的应用程序>>此处<<.如果你能检查一下就太好了(它需要一点工作).还有 UI 测试来证明它有效.此外,有两个选项可用,一个是 vanilla XCTest 和一个带有很多助手的库,我正在与我的同事一起创建 AutoMate.但这不是重点.

I made a few tests and a little research. You can check out the app created for this purpose >>here<<. It would be great if you could check it out (it required a little bit of work). There are also UI tests to prove it works. Also, two options are available, one is vanilla XCTest and one library with a lot of helpers I'm creating with my colleagues AutoMate. But that's not the point.

这是我发现的:

1) XCUIElementisSelected 属性取决于 accessibilityTrait.要在 XCTest 中选择的元素必须设置 UIAccessibilityTraitSelected.

1) isSelected property of XCUIElement depends on accessibilityTrait. Element to be selected in XCTest has to have UIAccessibilityTraitSelected set.

2) 我无法重现您的问题,但我能够控制 isSelected 属性.

2) I couldn't reproduce Your problem but I was able to control isSelected property.

3) 是的,它需要一些代码,但如果它对您很重要,它应该可以很好地与 VoiceOver 配合使用.

3) Yes, it requires a little bit of code, but should work well with VoiceOver if it is important for You.

所有必要的代码都在您的自定义 UITableViewCell 子类中.并使用覆盖 UIAccessibilityElement accessibilityTraits 属性.

All necessary code is in Your custom UITableViewCell subclass. And uses overriding UIAccessibilityElement accessibilityTraits property.

private var traits: UIAccessibilityTraits = UIAccessibilityTraitNone

// MARK: UITableViewCell life cycle
override func awakeFromNib() {
    super.awakeFromNib()
    traits = super.accessibilityTraits
}

// MARK: UIAccessibilityElement
override var accessibilityTraits: UIAccessibilityTraits {
    get {
        if isSelected {
            return traits | UIAccessibilityTraitSelected
        }
        return traits
    }

    set {
        traits = newValue
    }
}

希望有帮助.

这篇关于UI 测试 - isSelected 总是返回 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆