PIXEL
DOCK

I like the smell of Swift in the morning…

Xcode UITests: How to check if a UISwitch is on

Posted: | Author: | Filed under: Swift, UITests, Xcode | 12 Comments »

A simple scenario: You are writing a UITest that should check if a UISwitch is switched on.

In a UnitTest you would simply do this:

XCTAssert(yourSwitch.isOn)

When you are running a UITest you cannot do that, because during a running UITest you cannot access the UISwitch directly. Xcode only gives you access to your app’s UI elements via the XCUIElement class. That class is used for all accessible UI elements so it does not have a isOn property like UISwitch has.

So how do you test that the UISwitch is on?

It’s actually pretty easy, but not really obvious. XCUIElement conforms to XCUIElementAttributes which gives you access to a value property of type Any?

So, as value can literally be Any(thing) I tried to cast it to a Bool, because isOn also is a Bool, right?

Not working! Turns out that when you access a UISwitch in your UITest its value property is of type NSTaggedPointerString. Which is a subclass of NSString and can be cast to String (when you are using Swift). So the Bool isOn value has been mapped to a “0” or “1” String.

To test if the first UISwitch in your current view has been switched on, you can do this in a UITest:

let firstSwitch = app.switches.element(boundBy: 0)
XCTAssertEqual(firstSwitch.value as! String, "1")

If you want to access your UISwitch via the accessibility identifier you can do it like this:

let mySwitch = app.switches["MySwitchIdentifier"]

12 Comments

  • 1

    John Bettssaid at

    Hi. Which library are you using?

  • 2

    Jörnsaid at

    Hi John,

    I am not using any library, it’s just Apple’s own XCTest framework.

    Best,

    Jörn

  • 3

    John Bettssaid at

    Thank you jörn. Do you know how can I work with XCTest framework with java? Do you have some link?

  • 4

    Jörnsaid at

    I am afraid XCTest is for Objective-C / Swift only. I am not a Java Dev, so unfortunately I cannot help you with any links regarding UITests in Java.

  • 5

    John Bettssaid at

    Thank you very much!

  • 6

    Leonsaid at

    That was very helpful thanks!

  • 7

    Monami Chatterjeesaid at

    How to set accessibility identifier to the UISwitch , I have two UISwitch button

  • 8

    Jörnsaid at

    Hi Monami, a UISwitch conforms to the UIAccessibilityIdentification protocol. That means that it has an accessibilityIdentifier property that you can set: mySwitch.accessibilityIdentifier = “switch_1”

  • 9

    Monami Chatterjeesaid at

    I have added

    switchButton.isAccessibilityElement = true
    switchButton.accessibilityIdentifier = “switch_1”

    and in XCTest class I put

    for i in 0…1 {
    let switch101 = app.switches.matching(identifier: “switch_1”).element(boundBy: i)
    let switch101Exists = switch101.waitForExistence(timeout: 10)
    XCTAssert(switch101Exists)
    switch101.tap()
    }

    but it is not working.
    Giving me XCAssertTrue failed

  • 10

    Jörnsaid at

    You can access a UISwitch using the accessibility identifier like this:

    let switch101 = app.switches[“switch_1”]

  • 11

    Monami Chatterjeesaid at

    Hi Jorn, Thanks for your help..

  • 12

    Bentosaid at

    very helpful, thanks!


Leave a Reply