I'm opening a SFSafariViewController and can't find the "Done" Button to leave Safari again.
Also if I try to solve it with a swipeRight XCode creates code that it can't use in a test afterwards
let element = app.children(matching: .window).element(boundBy: 0).children(matching: .other).element.children(matching: .other).element.children(matching: .other).element(boundBy: 2)
element.swipeRight()
Any idea how to dismiss SFSafariViewController? (I don't need access to elements in the browser).

It looks like the problem is that XCTest framework for some reason can't tap on the Done button of SFSafariViewController directly (the error message says it can't scroll). However you can tap on the coordinates of that button and it works fine. Tested with Xcode 15 on real iPhone with iOS 17:
let app = XCUIApplication()
let topBrowserBar = app.otherElements["TopBrowserBar"]
XCTAssert(topBrowserBar.exists)
let doneButton = topBrowserBar.buttons["Done"] // Tap doesn't work!
XCTAssert(doneButton.exists)
let tapCoordinate = CGPoint(x: doneButton.frame.midX, y: doneButton.frame.midY)
let tapOffset = CGVector(dx: tapCoordinate.x / app.frame.width, dy: tapCoordinate.y / app.frame.height)
app.coordinate(withNormalizedOffset: tapOffset).tap() // This tap works!
XCTAssert(topBrowserBar.waitForNonExistence(timeout: 3))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With