Clear the Safari cache on iOS devices
There is no existing function to clear the cache of the Safari browser. What you can do is create a script that mimics the behavior of a real user, which includes these steps:
- On the iOS device, open the Settings app.
- Scroll down and find Safari.
- Click Safari.
- Scroll down and click Clear History and Website Data.
The following script has been tested on all iPhone and iPad devices with iOS 11 and later.
Copy
// open and close settings app, so that it comes to a default state.
Map<String, Object> params = new HashMap<>();
params.put("identifier", "com.apple.Preferences");
driver.executeScript("mobile:application:open", params);
params.clear();
params.put("identifier", "com.apple.Preferences");
driver.executeScript("mobile:application:close", params);
params.clear();
params.put("identifier", "com.apple.Preferences");
driver.executeScript("mobile:application:open", params);
switchToContext(driver, "NATIVE_APP");
if(deviceInfo(driver, "model").contains("iPad")){
//scroll and search safari button
scrolltoSafari(driver);
}else{
// scroll up and enter safari in search label for iOS devices
params.clear();
params.put("end", "50%,50%");
params.put("start", "50%,30%");
params.put("duration", "1");
driver.executeScript("mobile:touch:swipe", params);
driver.findElementByXPath("//*[@label=\"Search\"]").sendKeys("Safari");
try{
driver.findElementByXPath("//XCUIElementTypeCell[2]//*[@name='Safari']").click();
}catch(Exception e){
driver.findElementByXPath("//*[@label='Safari']").click();
}
}
// Swipe to bottom
Thread.sleep(1000);
params.clear();
params.put("start", "50%,90%");
params.put("end", "50%,20%");
params.put("duration", "1");
driver.executeScript("mobile:touch:swipe", params);
try {
el = driver.findElement(By.xpath("//*[@value=\"Clear History and Website Data\" and @visible='true']"));
} catch (Exception e) {
params.clear();
params.put("start", "50%,70%");
params.put("end", "50%,20%");
params.put("duration", "1");
driver.executeScript("mobile:touch:swipe", params);
el = driver.findElement(By.xpath("//*[@value=\"Clear History and Website Data\" and @visible='true']"));
}
if (el.isDisplayed()) {
el.click();
}
else {
// Swipe one more time
}
if(deviceInfo(driver, "model").contains("iPad")){
driver.findElement(By.xpath("//*[@label=\"Clear\"]")).click();
}else{
driver.findElement(By.xpath("//*[@label=\"Clear History and Data\"]")).click();
}
public static String deviceInfo(RemoteWebDriver driver, String deviceProperty){
Map<String, Object> params = new HashMap<>();
params.put("property", deviceProperty);
return (String) driver.executeScript("mobile:device:info", params);
}
public static void scrolltoSafari(RemoteWebDriver driver){
do {
try {
driver.findElementByXPath("//*[@value='Safari' and @visible='true']").click();
driver.findElementByXPath("//*[@value='Safari']/..").click();
break;
} catch (Exception NoSuchElementException) {
Map<String, Object> params = new HashMap<>();
params.put("start", "20%,90%");
params.put("end", "20%,30%");
params.put("duration", "2");
Object res = driver.executeScript("mobile:touch:swipe", params);
}
} while (true);
}
public static void switchToContext(RemoteWebDriver driver, String context) {
RemoteExecuteMethod executeMethod = new RemoteExecuteMethod(driver);
Map<String,String> params = new HashMap<String,String>();
params.put("name", context);
executeMethod.execute(DriverCommand.SWITCH_TO_CONTEXT, params);
}