How to clear iPhone simulator Safari history and data in XCUITest?

Viewed 134

How can I clear my Safari history and data for the iPhone simulator in an XCUITest before running the test?

I am trying to test the login features of an app and would like the login credentials not to be saved i.e. google sign in. For this I need to clear the safari history and data in the iPhone simulator settings. Is there a way of doing this in code in the setup or tear down methods?

2 Answers

Simple solution: use Private mode in Safari

You could do that by having a setup function that wipes the Safari data through Settings. Something like this:

func clearSafariData() {
  let settingsApp = XCUIApplication(bundleIdentifier: "com.apple.Preferences")
  settingsApp.launch()
  settingsApp.cells["Safari"].tap()
  settingsApp.cells["Clear History and Website Data"]
  settingsApp.buttons["Clear History and Data"].tap()
  settingsApp.terminate()
}
Related