I am currently writing automation tests using Java, Appium and Cucumber for our app. I have written my tests for Android and now i am writing tests for iOS in same project using Page Object model.
In Android everytime i run a scenario (test) my app cache / data gets automatically cleared. I have read somewhere that this is default for Android but i'm not sure.
This is not the case for iOS.
Example of this:
First every test contains login. You have to click few buttons, login with credentials and then setup 4 digit code, last step is to click a button which lets you in app.
This works everytime in Android because the data / cache gets cleared everytime not sure if before or after test is run but it does it. Without me specifying anything in my driver setup.
But in iOS it looks like this:
Let's say app is freshly installed. Then first test will go through the whole login flow mentioned above. But when test 2 is being run its expected to do the flow run again. And i have not been able to do that. So the test starts with entering 4 digit code that i setup in previous test and i don't want that because i am using 4 different accounts that are specific to a test.
A workaround for this could be to just logout of the application at the end of the test. But this would never go through the whole login flow. Also i would have to update every test and every future test to logout at the end. Not to mention if any test ever fails and it cant logout of application half of my test are gonna fail because of that. So it is not a good solution for my problem.
I have tried using capabilities: noReset and fullReset but neither seemed to work. If i do fullreset = true then my app gets uninstalled which i don't want obviously. And noReset didnt seem to do anything.
Edit: I am writing tests on real device not simulator/emulator
This is my whole code which creates driver:
As you can see i dont have any specific capabilities or anything that will force Android to clear app cache / data.
public class DriverSetup {
private static ThreadLocal<AppiumDriver> tlDriver = new ThreadLocal<>();
private static final Object lock = new Object();
public AppiumDriver getDriver() throws MalformedURLException {
if (tlDriver.get() == null) {
synchronized (lock) {
if (tlDriver.get() == null) {
String os = System.getProperty("os");
if (os == null) {
throw new IllegalArgumentException("OS system property is not set.");
}
if (os.equalsIgnoreCase("android")) {
tlDriver.set(getAndroidDriver());
} else if (os.equalsIgnoreCase("ios")) {
tlDriver.set(getIOSDriver());
} else {
throw new IllegalArgumentException("Invalid value for OS system property.");
}
}
}
}
return tlDriver.get();
}
public IOSDriver getIOSDriver() throws MalformedURLException {
AppiumDriver driver = null;
if (tlDriver.get() == null) {
synchronized (lock) {
if (tlDriver.get() == null) {
XCUITestOptions options = new XCUITestOptions();
options.setCapability("platformName", "iOS");
options.setCapability("automationName", "XCUITest");
options.setCapability("resetKeyboard", true);
options.setCapability("bundleId", "bundleId");
options.setCapability("udid", "udid");
options.setCapability("noReset", false);
driver = new IOSDriver(new URL("http://127.0.0.1:4723/"), options);
tlDriver.set(driver);
// Set implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
}
}
return (IOSDriver) tlDriver.get();
}
public AppiumDriver getAndroidDriver() throws MalformedURLException {
AppiumDriver driver = null;
if (tlDriver.get() == null) {
synchronized (lock) {
if (tlDriver.get() == null) {
UiAutomator2Options options = new UiAutomator2Options();
options.setCapability("platformName", "Android");
options.setCapability("automationName", "UiAutomator2");
options.setCapability("platformVersion", "13");
options.setCapability("deviceName", "deviceName");
options.setCapability("appPackage", "appPackage");
options.setCapability("appActivity", "appActivity");
options.setCapability("chromeOptions", ImmutableMap.of("w3c", false));
//options.setCapability("unicodeKeyboard", true);
options.setCapability("resetKeyboard", true);
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/"), options);
tlDriver.set(driver);
// Set implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
}
}
return driver;
}
public void quitDriver() {
synchronized (lock) {
AppiumDriver driver = tlDriver.get();
if (driver != null) {
driver.quit();
tlDriver.remove();
}
}
}
// Method to background the app
public static void backgroundApp(AppiumDriver driver) {
((InteractsWithApps) driver).runAppInBackground(Duration.ofSeconds(5));
}
}
Can someone please help me in how can i achieve this? This is a pretty major blocker for me now since we are going to production with new version next week and i really have to get this to work.