netshark1000
netshark1000

Reputation: 7403

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

Upvotes: 2

Views: 1139

Answers (2)

Alexander Vasenin
Alexander Vasenin

Reputation: 12933

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

Upvotes: 1

Amaury Vela
Amaury Vela

Reputation: 72

This worked for me:

let doneButton = app.buttons["Done"]
waitUntilElementExists(element: doneButton)
doneButton.tap()

The waitUntilElementExists I think its the key to making it work, I wait 60 seconds tops.

Upvotes: 1

Related Questions