iosxcodecoordinatestapxcode-ui-testing

Xcodes UI Testing - Tapping an x y location


I am trying to tap an x, y location, while using Xcode and UI Testing. Here is my code:

 XCUIApplication().coordinateWithNormalizedOffset(CGVector(dx: 50, dy: 50)).tap()

The middle of the button is definitely at that location and cannot be identified as an individual object (hence using the x, y coordinates).

However, this is not pressing the button. Anyone know if there is a correct way of tapping an x y coordinate?


Solution

  • The dx and dy are not pixel offsets but normalized vectors. For example, 0.5 is actually the middle of the element. Your code is trying to tap an element outside of the screens bounds (by ~50x!).

    I suggest trying to solve the problem with a different approach. First off, why can't the button be identified? What have you tried? Are you using a custom UIButton subclass?

    I ask because you can (usually) expose any control or UI element to UI testing with the right accessibility attributes. For example, you can set accessibilityLabel and accessibilityIdentifier to the button's text. Then you can use that value to access the button under test.

    // Production Code
    let button = UIButton()
    button.setTitle("Save", forState: .Normal)
    button.accessibilityIdentifier = "Save"
    
    // UI Test Code
    ```
    let app = XCUIApplication()
    app.buttons["Save"].tap()