Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XCTest: Tap "Done" button in SFSafariViewController

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).

enter image description here

like image 863
netshark1000 Avatar asked Dec 18 '25 17:12

netshark1000


1 Answers

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))
like image 89
Alexander Vasenin Avatar answered Dec 20 '25 05:12

Alexander Vasenin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!