Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing if an element is visible with Xcode 7 UITest

I want to verify if an element is visible or not depending on its .hidden property but I don't find a valid way to do that using the new Xcode 7 UI test stuff.

I've tried with myelement.exists and myelement.hittable but they doesn't seems to work as I expected. I suppose they work on conjunction with the hidden property. An hidden element shouldn't exists and is not hittable... but this is not the current behaviour (I can understand the exists behaviour... but a hidden element should be not hittable IMO).

Is there another way to verify the "hidden" property value?

like image 615
MatterGoal Avatar asked Oct 20 '15 21:10

MatterGoal


People also ask

How do you find the elements in XCUITest?

Finding elements in XCUITest. Our main quest as part of automating UI tests is trying to find elements on the screen. On XCUITest, there are three main ways of finding them: UI Test Recorder, Accessibility Inspector and debugging using po.

How do I write unit tests in Xcode?

Adding a unit test in Xcode If you already have a project, you can add a Unit Testing Bundle to it as well. Go to File > New > Target. Select iOS Unit Testing Bundle and then click Next. When you create a new Unit Test target for your project, it consists of a template class.


2 Answers

As of Xcode 7.1 Beta 3, UI Testing does not currently support validating the visibility of an element. I suggest filing a radar to bring the necessary attention to Apple.

Xcode 7.1 has fixed this issue. hittable now checks to see if the element is correct.

like image 132
Joe Masilotti Avatar answered Sep 19 '22 16:09

Joe Masilotti


1) I am testing the UI with swift in Xcode 7.3 and I using both .hittable and .exists for testing whether a label is hidden or not and they both work. I test for 'true' and 'false' to make sure either way agree with the result.

I have a label whose static text is "Track Info" and set to be hidden when app is first loaded, then later on I press a button to show the label, and here is the result after the label is shown.

// test fails

let trackInfoLabel = app.staticTexts["Track info"] XCTAssertEqual(trackInfoLabel.exists, true)  XCTAssertEqual(trackInfoLabel.hittable, true) 

// test passes

XCTAssertEqual(trackInfoLabel.exists, false) XCTAssertEqual(trackInfoLabel.hittable, false) 

// test passes

let trackInfoLabel = app.staticTexts["Track Info"] XCTAssertEqual(trackInfoLabel.exists, true)  XCTAssertEqual(trackInfoLabel.hittable, true) 

// test fails

XCTAssertEqual(trackInfoLabel.exists, false) XCTAssertEqual(trackInfoLabel.hittable, false) 

Leter on when I press the button to hide the label, all the results turned opposite. This confirms that both properties (hittable and exists) works for label.hidden setting.

2) Another way to find out if an element is hidden, you can do is element.frame.size.width == 0 || element.frame.size.height == 0

like image 29
Ohmy Avatar answered Sep 20 '22 16:09

Ohmy