如何使用UIImagePickerController(或任何其他本机VC)正确地使用UITest应用程序

Koe*_*en. 7 xcode ios swift xcode-ui-testing fastlane-snapshot

我正在尝试使用快照自动创建我的应用程序的屏幕截图,一切顺利,直到我想要浏览UIImagePickerController已设置allowsEditingtrue.

奇怪的是,在iPhone 4s,5s6s模拟器这很好,但在iPhone 6(s)Plus测试似乎无法点击"选择"(荷兰语中的"Kies")按钮裁剪视图.

我的第一次尝试在任何版本中都不起作用:

app.buttons.elementBoundByIndex(2).tap()
Run Code Online (Sandbox Code Playgroud)

并导致以下错误:

file:///%3Cunknown%3E:测试失败: - [MyAppSnapshots testExample()]失败:UI测试失败 - 无法滚动到可见(通过AX操作)按钮0x7f82d450ae30:特征:8589934593,{{327.0,613.5}, {35.0,34.0}},标签:'Kies',错误:错误-25204执行AXAction 2003

然后从这个答案中我抓住了解决方案forceTapElement,除了iPhone 6(s)Plus外,它的确可以正常工作.

app.buttons.elementBoundByIndex(2).forceTapElement()
Run Code Online (Sandbox Code Playgroud)

然后我尝试点击坐标;

let window = app.windows.elementBoundByIndex(0)
let rightBottom = window.coordinateWithNormalizedOffset(CGVectorMake(
  CGRectGetWidth(window.frame) - 20,
  CGRectGetHeight(window.frame) - 20
))
rightBottom.tap()
Run Code Online (Sandbox Code Playgroud)

但这再次无法在任何设备上运行.

那么我该如何测试这些原生接口呢?或者我应该只是添加某种切换到我的代码,以便UIImagePickerController替换为非交互式的东西.

小智 0

看起来像工作方法:

func waitForElementToAppear(element: XCUIElement,
                            timeout seconds: TimeInterval = 5,
                            file: String = #file,
                            line: UInt = #line) {
    let existsPredicate = NSPredicate(format: "exists == true")
    let expectation = expectation(for: existsPredicate, evaluatedWith: element, handler: nil)
    XCTWaiter().wait(for: [expectation], timeout: seconds)
    XCTAssert(element.exists, "Element \(element.identifier) not found")
}

func tapAtPosition(position: CGPoint,
                   file: String = #file,
                   line: UInt = #line) {
    let cooridnate = app.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 0)).withOffset(CGVector(dx: position.x, dy: position.y))
    cooridnate.tap()
}

func selectPhotoFromGalary(index: Int = 0) {
    waitForElementToAppear(element: app.buttons["Photos"], timeout: 10)
    let position = app.images.containing(NSPredicate(format: "label BEGINSWITH 'Photo'")).element(boundBy: index).frame.origin
    tapAtPosition(position: position)
    let chooseButton = app.buttons["Choose"]
    waitForElementToAppear(element: chooseButton, timeout: 5)
    chooseButton.tap()
}
Run Code Online (Sandbox Code Playgroud)