XCUITest

教學連結

func waitForElementToAppear(_ element: XCUIElement) -> Bool {
    let predicate = NSPredicate(format: "exists == true")
    let expectation = expectation(for: predicate, evaluatedWith: element, 
                                  handler: nil)

    let result = XCTWaiter().wait(for: [expectation], timeout: 5)
    return result == .completed
}
func waitForElementToAppear(_ element: XCUIElement) -> Bool {
    let predicate = NSPredicate(format: "exists == true")
    let expectation = XCTNSPredicateExpectation(predicate: existsPredicate, 
                                                object: element)

    let result = XCTWaiter().wait(for: [expectation], timeout: 5)
    return result == .completed
}
extension XCUIElement {
    func scrollToElement(element: XCUIElement, scroll:() -> Void) {
        while !element.visible() {
            scroll()
        }
    }

    func visible() -> Bool {
        guard self.exists && !CGRectIsEmpty(self.frame) else {
            return false
        }

        return CGRectIntersectsRect(XCUIApplication().windows.elementBoundByIndex(0).frame, self.frame)
    }
}
func testScrollTable() {
    let app = XCUIApplication()
    let table = app.tables.elementBoundByIndex(0)
    let cell12 = table.cells.elementBoundByIndex(12)
    let mainWindow = XCUIApplication().windows.elementBoundByIndex(0)

    mainWindow.scrollToElement(cell20) {
        let fromPoint: XCUICoordinate = mainWindow.coordinateWithNormalizedOffset(CGVector(dx: 0.5, dy: 0.75))
        let toPoint: XCUICoordinate = mainWindow.coordinateWithNormalizedOffset(CGVector(dx: 0.5, dy: 0.25))

        fromPoint.pressForDuration(0, thenDragToCoordinate: toPoint)
    }
}
  • inverted

    Indicates that the expectation is not intended to happen.

Last updated