Testing if an element is visible with Xcode 7 UITest

后端 未结 6 1362
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 10:19

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.

6条回答
  •  有刺的猬
    2020-12-03 10:50

    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

提交回复
热议问题