Identify Selected State Swift UI Tests
Asked Answered
W

2

16

I want to figure out which segment is selected on a segmented control in Xcode's new UI Testing in Swift.

I can get the segmentedControl XCUIElement, and the 'buttons' associated with it, but I'm not sure how to test for the selected property.

Sorry in advance if this is something obvious that I have missed.

Whelk answered 23/7, 2015 at 21:15 Comment(0)
P
22

XCUIElement has a selected property which you can examine:

XCTAssertTrue(app.segmentedControls.buttons.elementBoundByIndex(0).selected)

Papaverine answered 1/8, 2015 at 2:48 Comment(2)
I'm on Xcode7 Beta 3 and it does not have the selected property in XCUIElementAttributes. However, there is a new version of the Xcode Beta, 4, and according to the reference online it should have a selected property so I'm marking this as the correct answer.Whelk
I have integrated this into the tests now and it works great.Whelk
W
3

Version for Swift 4:

let environment = app.segmentedControls.element(boundBy: 0);
XCTAssertTrue(environment.buttons.element(boundBy:0).isSelected, "Wrong environment selected");
Whelk answered 24/11, 2017 at 9:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.