Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify Selected State Swift UI Tests

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.

like image 792
Alex Avatar asked Jul 23 '15 21:07

Alex


People also ask

What is UI test in Swift?

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.

What is Xcuielement?

ElementType. The types of UI element that you find, inspect, and interact with in a UI test. Xcode 7.0+

How do I test my iOS UI?

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.

What is XCTest Swift?

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.


2 Answers

XCUIElement has a selected property which you can examine:

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

like image 164
amitburst Avatar answered Oct 19 '22 03:10

amitburst


Version for Swift 4:

let environment = app.segmentedControls.element(boundBy: 0);
XCTAssertTrue(environment.buttons.element(boundBy:0).isSelected, "Wrong environment selected");
like image 31
Alex Avatar answered Oct 19 '22 03:10

Alex