How to perform multiple drag gestures using XCUICoordinate in XCTest without releasing the element in-between?

78 Views Asked by At

In my app, there's a card view, and when it's dragged to a corner of the screen (Left, Right, Up, Down), the screen displays the corner to which the card is dragged. I've written a UI test to validate this behavior, but it currently requires me to create four separate cards to run the test. This is because the card is released after the following call:

cardCenterCoordinate.press(forDuration: 0.1, thenDragTo: cardCenterCoordinate.withOffset(offset))

Is there a way to keep the drag gesture active throughout the entire process and move the same card to the four different directions, then return it to the center without releasing it?

func testDragInteractionOnPracticeScreenFlashcards() throws {
    let cardView = app.staticTexts["Card"]
    XCTAssertTrue(cardView.waitForExistence(timeout: 5))

    let hittableTimeout = 3.0

    let cardCenterCoordinate = cardView.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))
    let screenSize = app.windows.element(boundBy: 0).frame.size

    let distanceToMoveInX = screenSize.height * 0.5
    let distanceToMoveInY = screenSize.width * 0.5

    let checkpoints: [(String, CGVector)] = [
        ("Right", CGVector(dx: distanceToMoveInX, dy: 0)),
        ("Top", CGVector(dx: 0, dy: -distanceToMoveInY)),
        ("Left", CGVector(dx: -distanceToMoveInX, dy: 0)),
        ("Down", CGVector(dx: 0, dy: distanceToMoveInY))
    ]

    for (label, offset) in checkpoints {
        cardCenterCoordinate.press(forDuration: 0.1, thenDragTo: cardCenterCoordinate.withOffset(offset))
        XCTAssertTrue(app.staticTexts[label].waitForExistence(timeout: hittableTimeout))
    }
}
0

There are 0 best solutions below