【问题标题】:How to correctly UITest an application with UIImagePickerController (or any other native VC)如何使用 UIImagePickerController(或任何其他本机 VC)正确 UITest 应用程序
【发布时间】:2016-05-19 13:29:23
【问题描述】:

我正在尝试使用 Snapshot 自动创建我的应用程序的屏幕截图,一切顺利,直到我想浏览已将 allowsEditing 设置为 trueUIImagePickerController

奇怪的是,在 iPhone 4s5s6s 模拟器中这一切正常,但在 iPhone 6(s) Plus 测试似乎无法点击裁剪视图中的“选择”(荷兰语中的“Kies”)按钮。

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

app.buttons.elementBoundByIndex(2).tap()

并导致以下错误:

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

然后,我从this answer 获取了forceTapElement 的解决方案,该解决方案适用于除iPhone 6(s) Plus之外的所有设备。

app.buttons.elementBoundByIndex(2).forceTapElement()

然后我尝试点击坐标;

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

但这又在任何设备上都不起作用。

那么我该如何测试这些原生接口呢?或者我是否应该在我的代码中添加某种开关,以便将 UIImagePickerController 替换为非交互的东西。

【问题讨论】:

  • 你能解决这个问题吗?因为我现在也面临着它!我确实尝试了所有提到的解决方案,但在任何设备上都不起作用,除非我确实设置了 .allowsEditing = false。
  • 不,从未找到解决方案。虽然我已经尝试了一段时间。
  • 感谢您的回复!如果您仍然需要它(或其他任何人),我可以通过在点击选择按钮之前放置 sleep(1) 来修复它。不确定是否每个人都应该这样。

标签: ios xcode swift xcode-ui-testing fastlane-snapshot


【解决方案1】:

看起来像工作方法:

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()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 2015-11-24
    • 1970-01-01
    相关资源
    最近更新 更多