iOS XCUITests access element by accessibility

前端 未结 3 630
滥情空心
滥情空心 2020-12-15 21:02

How do I assert a button exists by his accessibilityLabel or identifier?

func testExitsButton() {
    XCTAssertTrue(app.windows.containing(.button, identifie         


        
相关标签:
3条回答
  • 2020-12-15 21:31

    add | "accessibilityIdentifier" String test | in the User Defined Runtime Attributes on the navigation bar, instead of in the accessibility label

    0 讨论(0)
  • 2020-12-15 21:47

    IMPORTANT NOTE: If a superview is set to accessible, XCUITest might not be able to access its subviews.

    You can access the element by setting its accessibility via the storyboard or programmatically as discussed above. You can click the record button when your cursor is in a function which starts with the prefix "test" in order to record how XCUITest sees an element. Sometimes it takes a couple cleans (command shift k) and a couple minutes for the record button to be available. You can also step down your tree from storyboard and use XCUITest functions such as element(boundBy: Int), children(matching: .textField), you can chain them as well: XCUIApplication().tables.cells.containing(.button, identifier: "id"). After that is the easy part, use .exists which returns a boolean.

    0 讨论(0)
  • 2020-12-15 21:52

    Set an accessibility identifier in your application code, and then search for the button using that identifier in your tests.

    // app code
    let button: UIButton!
    button.accessibilityIdentifier = "myButton"
    
    // UI test code
    func testMyButtonIsDisplayed() {
        let app = XCUIApplication()
        let button = app.buttons["myButton"]
        XCTAssertTrue(button.exists)
    }
    

    The accessibility identifier is set independently of text on the button, and is also independent of the accessibility label. It's not best practice to put identifiers for UI elements as the accessibility label, since the accessibility label is read to VoiceOver users to explain the element to them.

    0 讨论(0)
提交回复
热议问题