我有一个带有小部件的应用程序,我需要自动创建它的截图。
然而,我很难做到这一点,因为我无法点击 "今日视图 "中的 "编辑 "按钮。如果没有安装小部件,按钮很容易点击。然而,在模拟器复位后,有小部件存在(地图,提醒事项,快捷方式等),按钮不再可以点击。更糟糕的是,'isHittable'返回true :(
我试图运行的代码是。
let app = XCUIApplication()
app.launch()
app.activate()
// Open Notification Center by swiping down
let bottomPoint = app.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 2))
app.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 0)).press(forDuration: 0.1, thenDragTo: bottomPoint)
sleep(1)
// now swipe to reveal Today View (uses custom method)
let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
springboard.scrollViews.firstMatch.swipeRight()
sleep(1)
// To make sure Edit button is visible, swipeUP
springboard.scrollViews.firstMatch.swipeUp()
sleep(2)
// Now tap the edit button (DOESN"T WORK)
springboard.buttons["Edit"].press(forDuration: 1)
sleep(2)
我创建了一个简单的项目来说明这个bug,它位于: 此处.
要亲自查看这个错误,请打开iPhone 11 Pro Max,并将以下小工具添加到 "今日视图 "中。
然后,尝试运行 testExample
检验 XCUITestCrashUITests
. 如果成功的话,在最后应该点击编辑按钮,你应该看到编辑屏幕。然而,它从来没有点击:(
如果有人能帮我找到一个解决方案,那将是巨大的。我已经尝试了所有我能想到的方法,但都没有用......
有时 tap()
失败或什么都不做。一个常见的解决方案是点击元素的坐标,而不是元素本身。
extension XCUIElement {
func tapUnhittable() {
XCTContext.runActivity(named: "Tap \(self) by coordinate") { _ in
coordinate(withNormalizedOffset: CGVector(dx: 0.0, dy: 0.0)).tap()
}
}
}
下面是工作的代码
import XCTest
class XCUITestCrashUITests: XCTestCase {
func testExample() throws {
let app = XCUIApplication()
app.launch()
let bottomPoint = app.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 2))
app.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 0)).press(forDuration: 0.1, thenDragTo: bottomPoint)
let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
springboard.swipeRight()
springboard.swipeUp()
springboard.buttons["Edit"].tapUnhittable()
}
}