Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if navigationBar button is enabled/disabled in UI test on iOS?

In my iOS app I've added UI tests, where I need to check if navigationBar button is enabled/disabled at different point of time.

Currently I'm using:

XCUIElement* saveButton = self.app.navigationBars[@"TSSIDAddCardView"].buttons[@"Save"];


XCTAssertEqual(saveButton.hittable, YES);

However, this always returns YES. The .exists test returns YES as well.

Does anyone knows how to do the proper test?

like image 852
Eugene Gordin Avatar asked Dec 17 '15 18:12

Eugene Gordin


3 Answers

XCUIElement conforms to XCUIElementAttributes, which provides @property(readonly, getter=isEnabled) BOOL enabled;.

So you can just do this on your XCUIElement* saveButton:

Objective-C

XCTAssertEqual(saveButton.enabled, YES);

Swift

XCTAssertEqual(saveButton.isEnabled, true)
like image 144
Slipp D. Thompson Avatar answered Nov 19 '22 04:11

Slipp D. Thompson


You can get the actual UI component via the value property.

With that you can check if it is enabled or not.

Something like:

UIBarButtonItem *saveButton = self.app.navigationBars[@"TSSIDAddCardView"].buttons[@"Save"].value;
XCTAssertTrue(saveButton.enabled);
like image 39
InsertWittyName Avatar answered Nov 19 '22 04:11

InsertWittyName


So with help of @InsertWittyName I found the solution:

UIBarButtonItem *saveButton = self.app.navigationBars[@"TSSIDAddCardView"].buttons[@"Save"];
XCTAssertFalse(saveButton.enabled);
like image 2
Eugene Gordin Avatar answered Nov 19 '22 02:11

Eugene Gordin