Testing if an element is visible with Xcode 7 UITest
Asked Answered
C

7

34

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?

Carducci answered 20/10, 2015 at 21:28 Comment(0)
S
35

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.

Stadler answered 20/10, 2015 at 22:52 Comment(6)
This is not working for me on Xcode 7.1. I update some controls to hidden dynamically and test afterwards and this always returns true, even if the control is obviously hidden.Expectation
Xcode 7.2 hittable test is not working properly for hidden elements, eitherThorton
Xcode 7.2.1 hittable test is not working properly for hidden elementsFurtado
I suggest filing a bug report with Apple if this has broken in Xcode 7.2.Stadler
On Xcode 7.3(7D175), hittable returns false for custom NSControl with NSAccessibilityCheckBoxRole accessibility role. Accessibility Inspector didn't find any issues or errors with it, but it can't display it's properties in Inspector panel while hovering it. Looks like I've missed something important.Ern
This property on XCUIElement is now called isHittable. Is worth reading the documentation for the cases in which it may return true or false.Scarrow
N
11

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

Normannormand answered 7/5, 2016 at 16:45 Comment(0)
W
3

XCUIElement.hittable works for me (in my particular test case where I am checking several UIButton elements for visibility) - quite sure it is not a right way to do it though

Windbreak answered 27/10, 2015 at 7:29 Comment(0)
V
2

Next code worked for me. At the end of the test you can past code as follow:

while ([app.staticTexts matchingIdentifier:@"accesibilityId"].count > 0) {
        sleep(1);
    }
Vital answered 23/12, 2015 at 4:20 Comment(0)
F
2

The syntax is now .isHittable:

isHittable only returns true if the element is already visible and hittable onscreen. It returns false for an offscreen element in a scrollable view, even if the element would be scrolled into a hittable position by calling click(), tap(), or another hit-point-related interaction method.

Using the .isHittable property will work because hidden elements are not visible or hittable on screen.

Flannelette answered 11/3, 2022 at 16:51 Comment(0)
C
1

I agree hittable doesn't always work for buttons (Swift 2.0, XCode 7.2)

I just discovered that if button is visible, you can find it ONLY among buttons, while if button is hidden, you can find it's tag in staticTexts as well!

XCTAssertFalse(app.buttons["Log out"].exists && app.staticTexts["Log out"].exists) // This button is visible (hidden = false)
XCTAssert(app.buttons["Log in"].exists && app.staticTexts["Log in"].exists) // This one is hidden

Hacky, but works for buttons. Apple should just introduce .hidden or .visible along .hittable and .exists

Corson answered 2/3, 2016 at 15:55 Comment(0)
F
0

My solution is to add accessibilityIdentifier dynamicly

func someMethod() {
    label.isHidden = true
    label. accessibilityIdentifier = "isHidden"
}

func someOtherMethod {
    label.isHidden = false
    label. accessibilityIdentifier = "isVisible"
}

and the in UITest you can use it as

if app.staticTexts["MyLabel"].identifier == "isHidden" {
    dosomething()
}
Flavorful answered 13/3, 2018 at 9:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.