有没有办法让SFSafariViewController自动化呢?我喜欢Xcode7UI测试特性,但它似乎不支持SFSafariViewController自动化。我正在测试的一些UI流程需要web浏览器,因此该应用程序使用SFSafariViewController来使其比web视图更安全。
发布于 2016-04-13 04:48:41
如果它类似于启动扩展(目前与直接交互中断),请尝试在您要查找的元素所在的点点击屏幕:
点击启动扩展的操作表的示例:
func tapElementInActionSheetByPosition(element: XCUIElement!) {
let tableSize = app.tables.elementBoundByIndex(0).frame.size
let elementFrame = element.frame
// get the frame of the cancel button, because it has a real origin point
let CancelY = app.buttons["Cancel"].frame.origin.y
// 8 is the standard apple margin between views
let yCoordinate = CancelY - 8.0 - tableSize.height + elementFrame.midY
// tap the button at its screen position since tapping a button in the extension picker directly is currently broken
app.coordinateWithNormalizedOffset(CGVectorMake(elementFrame.midX / tableSize.width, yCoordinate / app.frame.size.height)).tap()
}
注意:您必须在XCUIApplication查询层点击。按位置点击元素不起作用。
发布于 2018-04-26 17:20:15
目前,Xcode9.3已经支持这一点,但是由于annoying Xcode bug,它不能正常工作。
在测试中,您可以打印app.webViews.buttons.debugDescription
或app.webViews.textFields.debugDescription
,它会打印正确的信息,但在tap
或typeText
之后,您会崩溃。
要解决这个问题,你可以解析debugDescription
,提取坐标,然后点击坐标。对于文本字段,您可以通过“粘贴”菜单插入文本。
private func coordinate(forWebViewElement element: XCUIElement) -> XCUICoordinate? {
// parse description to find its frame
let descr = element.firstMatch.debugDescription
guard let rangeOpen = descr.range(of: "{{", options: [.backwards]),
let rangeClose = descr.range(of: "}}", options: [.backwards]) else {
return nil
}
let frameStr = String(descr[rangeOpen.lowerBound..<rangeClose.upperBound])
let rect = CGRectFromString(frameStr)
// get the center of rect
let center = CGVector(dx: rect.midX, dy: rect.midY)
let coordinate = XCUIApplication().coordinate(withNormalizedOffset: .zero).withOffset(center)
return coordinate
}
func tap(onWebViewElement element: XCUIElement) {
// xcode has bug, so we cannot directly access webViews XCUIElements
// as workaround we can check debugDesciption, find frame and tap by coordinate
let coord = coordinate(forWebViewElement: element)
coord?.tap()
}
完整代码在这里:https://gist.github.com/pilot34/09d692f74d4052670f3bae77dd745889
https://stackoverflow.com/questions/32763010
复制相似问题