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

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?



Solution 1:[1]

Simple solution: use Private mode in Safari

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Roman Zakharov
Solution 2 drunkencheetah