In my appium mobileweb test automation project (Appium v3.2, JDK 21) with real devices like Android with Chrome browser ( Android v16.0 Samsung Galaxy S21 FE, Chrome v147.0), and iOS(v.26.4.2, iphone 11) with Safari browser, particularly while using iPhone device a new browser session is NOT being used. Meaning in a previous test session, say the test failed in the middle with out being logged out or WebDriver being quit, just the browser session is closed.
For the next test session, the browser (In this case the Safari browser) is starting at the same place the old session has stopped and since it is not finding the correct web elements to login on this old page, it was failing obviously.
I am using the following Java code set up to initiate the IOSDriver.
Please look in to enhancing the Java code to initiate a brand new browser session each time a test is re-launched.
case "iOS":
try {
XCUITestOptions iOsOptions = new XCUITestOptions();
iOsOptions.setPlatformName(platform);
iOsOptions.setPlatformVersion(platformVersion);
iOsOptions.setDeviceName(deviceName);
iOsOptions.setAutomationName("XCUITest");
iOsOptions.setNoReset(false); // or just omit; false is the default
iOsOptions.setUdid(udId);
iOsOptions.setCapability("appium:safariInitialUrl", url);
iOsOptions.setCapability("browserName", browser);
iOsOptions.setCapability("appium:autoAcceptAlerts", true); // accept all alerts
iOsOptions.setCapability("appium:showXcodeLog", true);//display the output of the Xcode command used to run the tests
//Timeout capabilities added to avoid session creation failure
iOsOptions.setCapability("appium:newCommandTimeout", 300);
iOsOptions.setCapability("appium:wdaLaunchTimeout", 120000);
iOsOptions.setCapability("appium:wdaConnectionTimeout", 120000);
iOsOptions.setCapability("appium:useNewWDA", false);//change to true, to force clean WDARunner
iOsOptions.setCapability("appium:safariIgnoreFraudWarning", true);
iOsOptions.setCapability("appium:safariAllowPopups", true);
//Timeout capabilities
//Safari specific capabilities
iOsOptions.setCapability("appium:nativeWebTap", false); // // uses JavaScript-based taps,otherwise native(flaky taps in Safari)
driver = new IOSDriver(url, iOsOptions);
} catch (Exception e) {
System.out.println("Exception during returning the IOSDriver");
e.printStackTrace();
}
break;
default:
throw new Exception("invalid platform-"+platform+",browswer-"+browser);
}
I used your suggestion. I observed (did I ??) that the Safari session even when abandoned in the middle previously is starting brand new this time.
But lately like today, I have clearly seen the past problem re-appearing that Safari browser is freshly invoked and it is showing up where the previous session got discontinued or dropped and failing to find the login page web elements, thus failing. I doubt that I may be using self contradicting options. Here is my new code snippet.
case "iOS":
try {
XCUITestOptions iOsOptions = new XCUITestOptions();
iOsOptions.setPlatformName(platform);
iOsOptions.setPlatformVersion(platformVersion);
iOsOptions.setDeviceName(deviceName);
iOsOptions.setAutomationName("XCUITest");
iOsOptions.setForceAppLaunch(true);//discard previous failed web page and start fresh
iOsOptions.setNoReset(false); // or just omit; false is the default
iOsOptions.setUdid(udId);
iOsOptions.setCapability("appium:safariInitialUrl", url);
iOsOptions.setCapability("browserName", browser);
iOsOptions.setCapability("appium:autoAcceptAlerts", true); // accept all alerts
iOsOptions.setCapability("appium:showXcodeLog", true);//display the output of the Xcode command used to run the tests
//>>>Suggested by AI, on Jan27th, 2026 Timeout capabilities added to avoid session creation failure
iOsOptions.setCapability("appium:newCommandTimeout", 300);
iOsOptions.setCapability("appium:wdaLaunchTimeout", 120000);
iOsOptions.setCapability("appium:wdaConnectionTimeout", 120000);
iOsOptions.setCapability("appium:useNewWDA", false);//change to true, to force clean WDARunner
iOsOptions.setCapability("appium:safariIgnoreFraudWarning", true);
iOsOptions.setCapability("appium:safariAllowPopups", true);
//<<< Suggested by AI
//Safari specific capabilities
iOsOptions.setCapability("appium:nativeWebTap", false); // // uses JavaScript-based taps,otherwise native(flaky taps in Safari)
driver = new IOSDriver(url, iOsOptions);
} catch (Exception e) {
System.out.println("Exception during returning the IOSDriver");
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
Dear @Aleksei
Appreciate your quick help.
I used this statement iOsOptions.setCapability(“appium:initialDeeplinkUrl”, “about:blank”); // Start Safari with a blank page to ensure a clean session
Then unfortunately iOSDriver session could not be created. May be it is my own setup issue or may be the syntax I have used is wrong. Please correct my syntax, if it is wrong.
BTW, two observations or rather clarifications about my question itself.
When I replied that your first suggestion optionsXCUITest.setForceAppLaunch(true) has not worked, it is like this. Since I am testing with multiple web sites, say URL1 and URL2, then when I am using continuously URL1, then it is working, like it is able to initiate the session brand new, even though the previous run was stopped in the middle.
But when I have to use URL2 after testing with URL1, then it is failing to load the new url and keep opening the same old URL1 itself.
Now I hope, I conveyed my question better, instead of confusing you further.
I have a framework, which I have to use to test automate an eCommerce web site with SAP as it’s back end. While testing this same web site, there are different variation of the same site, like as an agent and/or price coordinator, which each have their urls, say URL1, and URL2.
While the suite contains a series of test cases, some use URL1, and others use URL2 in the same regression cycle. With iOS, this is becoming a problem.
While iOsOptions.setCapability(“appium:initialDeeplinkUrl”, “about:blank”); is working fine, to load the URL freshly from login step, even after a previous test has failed in the middle of the test, it is not successful in jumping from a test using URL1 to a next test using URL2, instead it is trying to load the old page and trying to run the new test.
Probably, some other parameter would work for this requirement, separately.
Dear @Aleksei
Sorry for the delayed response. I was working on some other priorities. Pheww…
More over I confused you much. Here is fresh and new…
As you told, I used the below iOsOptions.setCapability(“appium:initialDeeplinkUrl”, “about:blank”); // Start Safari with a blank page to ensure a clean session
It is failing to create the IOSDriver properly and showing me this console message:
*Make sure the correct bundle identifier has been provided in capabilities and check the device log for possible crash report occurrences
*
Probably it is meant for mobile application, while I am testing on mobileweb !?
Plus the rest of the steps given as per the comments, even though did not error out, they are not helping for the specific purpose of the issue in question.
// 1. open neutral screen // 2. Navigate your domain // 3. Clear all tracking parameters manually // 4. Force refresh to pull down fresh assets