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.
In this case, you might want to look at UI testing for Swift. UI testing allows you to automate this procedure, so every time you update your code, Xcode will automatically test whether your app works correctly without you having to manually do it.
ElementType. The types of UI element that you find, inspect, and interact with in a UI test. Xcode 7.0+
Go to File > New > Target. Then, search for UI Testing Bundle. Select Unit Testing Bundle and then click Next. As UI tests take time, it is usually best to stop immediately when a failure occurs.
Use the XCTest framework to write unit tests for your Xcode projects that integrate seamlessly with Xcode's testing workflow. Tests assert that certain conditions are satisfied during code execution, and record test failures (with optional messages) if those conditions aren't satisfied.
XCUIElement
has a selected
property which you can examine:
XCTAssertTrue(app.segmentedControls.buttons.elementBoundByIndex(0).selected)
Version for Swift 4:
let environment = app.segmentedControls.element(boundBy: 0);
XCTAssertTrue(environment.buttons.element(boundBy:0).isSelected, "Wrong environment selected");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With