在UI测试中自动化SFSafariViewController的任何方法?

有没有办法自动化SFSafariViewController? 我喜欢Xcode 7 UI测试function,但它似乎不支持SFSafariViewController自动化。 我正在测试的一些UI流程需要一个Web浏览器,因此应用程序使用SFSafariViewController使其比Web视图更安全。

如果它类似于启动扩展(目前已通过直接交互打破),请尝试在您要查找的元素点击屏幕:

点击启动扩展程序的操作表的示例:

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查询图层。 按位置点击元素不起作用。

目前Xcode 9.3已经支持,但由于恼人的Xcode错误 ,它无法正常工作。

在测试中,您可以打印app.webViews.buttons.debugDescriptionapp.webViews.textFields.debugDescription ,并打印正确的信息,但在taptypeText您将崩溃。

要解决此问题,您可以解析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.. 

关于此的文章: https : //medium.com/@pilot34/work-with-sfsafariviewcontroller-or-wkwebview-in-xcode-ui-tests-8b14fd281a1f

完整代码在这里: https : //gist.github.com/pilot34/09d692f74d4052670f3bae77dd745889