嗨,我是 UI 测试和 swift 新手。
我想做的只是点击右上角的“+”按钮,然后点击显示的“第一个选项”。 Xcode 说我的测试成功,但我的两次点击都没有执行任何操作。这是我的代码
func test1() {
let app = XCUIApplication()
let addButtonButton = app.navigationBars["Timesheets"].buttons["add button"]
waitForElementToHittable(addButtonButton)
addButtonButton.tap()
let tablesQuery = app.tables
tablesQuery.cells.elementBoundByIndex(0).forceTap()
}
这是waitForElementToHittable() 和forceTap() 的定义
func waitForElementToHittable(element: XCUIElement,
file: String = #file, line: UInt = #line) {
let existsPredicate = NSPredicate(format: "hittable == true")
expectationForPredicate(existsPredicate,
evaluatedWithObject: element, handler: nil)
waitForExpectationsWithTimeout(30) { (error) -> Void in
if (error != nil) {
let message = "Failed to hit \(element) after 30 seconds."
self.recordFailureWithDescription(message,
inFile: file, atLine: line, expected: true)
}
}
}
extension XCUIElement {
func forceTap() {
if self.hittable {
self.tap()
} else {
let coordinate: XCUICoordinate = self.coordinateWithNormalizedOffset(CGVectorMake(0.0, 0.0))
coordinate.tap()
}
}
}
任何指导将不胜感激。谢谢你。
您确实忘记启动该应用程序:
let app = XCUIApplication()
app.launch() // missing in your code
斯威夫特5
extension XCUIElement {
func forceTap() {
if self.isHittable {
self.tap()
} else {
let coordinate: XCUICoordinate = self.coordinate(
withNormalizedOffset: CGVectorMake(0.0, 0.0)
)
coordinate.tap()
}
}
}