Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UI Tests - 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.

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

like image 772
VitorMM Avatar asked Mar 21 '17 18:03

VitorMM


2 Answers

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.

like image 133
dzoanb Avatar answered Nov 09 '22 21:11

dzoanb


Couldn't get that code to compile under Swift 4. This worked for me.

public override var accessibilityTraits: UIAccessibilityTraits {
    get {
        if isSelected {
            return super.accessibilityTraits.union(.selected)
        }
        return super.accessibilityTraits
    }

    set {
        super.accessibilityTraits = newValue
    }
}
like image 26
Mark Bridges Avatar answered Nov 09 '22 22:11

Mark Bridges