Close all tabs at once in mobile Safari on iPhone or iPad
In iOS 10 and later, Safari supports an unlimited number of tabs. If you find yourself with too many tabs open and want to start fresh, you can use a shortcut to close all tabs at once. For closing tabs manually, see the Apple documentation.
To close all tabs as part of your automation tests, see the following Java code sample using Appium that can work for iOS 11 and later.
Restriction: This automation code sample does not work with iOS 10 and earlier, or with any Android version.
Copy
Map<String, Object> params = new HashMap<>();
params.clear();
params.put("automation", "os");
driver.executeScript("mobile:browser:open", params);
driver.context("NATIVE_APP");
WebElement browserTab = driver.findElementByXPath("//*[@label=\"Tabs\"]");
TouchAction action = new TouchAction(driver);
action.longPress(browserTab).press(browserTab);
action.perform();
action.longPress(browserTab).release();
action.perform();
params.clear();
params.put("content", "Close All");
params.put("timeout", "30");
driver.executeScript("mobile:text:find", params);
driver.context("NATIVE_APP");
driver.findElementByXPath("//XCUIElementTypeButton[contains(@label,'Close All')]").click();
Thread.sleep(10000);