isSelected is always returning false

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).

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)

Note: app is a XCUIApplication.

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

XCTAssert(cell.isSelected)

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)

EDIT: 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.

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.


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.

Here is what I found out:

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

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

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

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

Hope it helps.


Have you tried making a break point before and after the tap, and check the value of the cell? Like the WWDC video here: https://youtu.be/7zMGf-0OnoU (See from 10 minutes in)


isSelected only works on views which inherit from UIControl . UIControl.isSelected informs XCUIElement.isSelected .

Since UITableViewCell does not inherit from UIControl , you aren't seeing the value you want in your tests when you observe cell.isSelected .

I suggest that if you want this to be testable via UI tests that you file a feature request with Apple to make UIControl a protocol, which you could then extend your cells to conform to, or add UITableViewCell.isSelected to the properties that inform XCUIElement.isSelected .

链接地址: http://www.djcxy.com/p/95830.html

上一篇: 从illustrator透明度问题的svg

下一篇: isSelected总是返回false