首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >有没有办法在UI测试中自动化SFSafariViewController?

有没有办法在UI测试中自动化SFSafariViewController?
EN

Stack Overflow用户
提问于 2015-09-24 21:44:31
回答 2查看 1.8K关注 0票数 12

有没有办法让SFSafariViewController自动化呢?我喜欢Xcode7UI测试特性,但它似乎不支持SFSafariViewController自动化。我正在测试的一些UI流程需要web浏览器,因此该应用程序使用SFSafariViewController来使其比web视图更安全。

EN

回答 2

Stack Overflow用户

发布于 2016-04-13 04:48:41

如果它类似于启动扩展(目前与直接交互中断),请尝试在您要查找的元素所在的点点击屏幕:

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

代码语言:javascript
运行
复制
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查询层点击。按位置点击元素不起作用。

票数 2
EN

Stack Overflow用户

发布于 2018-04-26 17:20:15

目前,Xcode9.3已经支持这一点,但是由于annoying Xcode bug,它不能正常工作。

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

要解决这个问题,你可以解析debugDescription,提取坐标,然后点击坐标。对于文本字段,您可以通过“粘贴”菜单插入文本。

代码语言:javascript
运行
复制
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://medium.com/@pilot34/work-with-sfsafariviewcontroller-or-wkwebview-in-xcode-ui-tests-8b14fd281a1f

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32763010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档