Ios 有没有办法在UI测试中自动化SFSafariViewController?
有没有办法使SFSafariViewController自动化?我喜欢Xcode 7 UI测试功能,但它似乎不支持SFSafariViewController自动化。我正在测试的一些UI流需要一个web浏览器,因此应用程序使用SFSafariViewController使其比web视图更安全。如果它类似于启动扩展(目前因直接交互而中断),请尝试在您要查找的元素所在的点点击屏幕: 点击启动扩展的操作表的示例:Ios 有没有办法在UI测试中自动化SFSafariViewController?,ios,mobile-safari,coded-ui-tests,xctest,xcode-ui-testing,Ios,Mobile Safari,Coded Ui Tests,Xctest,Xcode Ui Testing,有没有办法使SFSafariViewController自动化?我喜欢Xcode 7 UI测试功能,但它似乎不支持SFSafariViewController自动化。我正在测试的一些UI流需要一个web浏览器,因此应用程序使用SFSafariViewController使其比web视图更安全。如果它类似于启动扩展(目前因直接交互而中断),请尝试在您要查找的元素所在的点点击屏幕: 点击启动扩展的操作表的示例: func tapElementInActionSheetByPosition(eleme
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()
}
注意:必须点击XUIApplication查询层。按位置轻敲元素不起作用。目前,Xcode 9.3支持该功能,但由于存在错误,无法正常工作 在测试中,您可以打印
app.webViews.buttons.debugDescription
或app.webViews.textFields.debugDescription
,并打印正确的信息,但在点击或键入text
后,您会崩溃
要解决此问题,您可以解析debugDescription
,提取坐标并点击“按坐标”。对于文本字段,您可以通过“粘贴”菜单插入文本
private func坐标(forWebViewElement:XUIElement)->XUICoordinate?{
//解析描述以查找其框架
让descr=element.firstMatch.debugDescription
guard let rangeOpen=descr.range(共:“{{”,选项:[.backwards]),
让rangeClose=descr.range(of:“}}”,选项:[.backwards])else{
归零
}
让frameStr=String(descr[rangeOpen.lowerBound..我对此也很感兴趣。你找到了解决这个问题的方法吗?@Martin不幸的是,现在什么都没有。我不清楚你到底想自动化什么。在我的例子中,我想用一个测试帐户将登录流自动化到第三方服务。
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()
}