Where to run Appium tests and how to make them stable
Three decisions determine the stability of mobile tests: where to run, how to run in parallel and what to do with app state. Emulators and simulators suit daily runs, while real devices and cloud farms are needed for the camera, biometrics, push and a range of models. Parallel sessions need their own udid and ports, and state is better prepared through an API or a deep link, since retries only mask flakiness.
Appium · Infrastructure
On your laptop the test is green, on CI every third run fails. What exactly differs?
Mobile tests are flaky more often than web ones: the result is affected by the device, the emulator, the network, animations and leftovers of state from the previous test. This lesson covers three decisions that determine stability: where to run, how to run in parallel and what to do with app state.
Environments. Different goals need different devices:
| Environment | What for | Limitation |
|---|---|---|
| Android emulator | Daily runs in CI | Needs hardware acceleration; camera and fingerprint are only simulated |
| iOS simulator | Fast checks of logic | macOS and Xcode only |
| Real devices | Camera, biometrics, push, performance | Costly, need maintenance and a USB connection to a host |
| Cloud farm | Many models and OS versions | Cost, network, vendor options |
iOS simulators and the WebDriverAgent build work only on macOS with Xcode, so iOS on CI needs Mac agents. For Android emulators on Linux, hardware acceleration (KVM) matters: without it the emulator is too slow and tests fail on timeouts rather than on errors. Cloud farms (BrowserStack, Sauce Labs and others) give many models and OS versions without your own fleet: the server URL and vendor options change, while the test stays the same.
// cloud: the server URL and vendor options change, the test stays
URL hub = new URL("https://" + user + ":" + key + "@hub.example-cloud.test/wd/hub");
UiAutomator2Options o = new UiAutomator2Options().setApp(appUrl);
o.setCapability("vendor:options", Map.of("deviceName", "Pixel 8", "osVersion", "14"));
How it works and why: parallelism. One Appium server drives several sessions if they do not share resources. Conflicts arise on shared ports and devices, so every session must have its own appium:udid and unique ports: appium:systemPort for UiAutomator2 (if unset, the driver takes the first free one from the range 8200 to 8299, but an explicit value makes a run predictable), appium:chromedriverPort for WebView, appium:wdaLocalPort for iOS and a separate appium:derivedDataPath when WDA is built in parallel.
Session isolation
// Android: own device and port per session
UiAutomator2Options a = new UiAutomator2Options()
.setUdid("emulator-5554")
.setSystemPort(8201)
.setApp("/builds/app-debug.apk");
UiAutomator2Options b = new UiAutomator2Options()
.setUdid("emulator-5556")
.setSystemPort(8202)
.setApp("/builds/app-debug.apk");
// iOS: the counterpart
XCUITestOptions ios = new XCUITestOptions()
.setUdid("00008030-001C...")
.setWdaLocalPort(8101);
Tests must be independent: shared accounts and data break them just as in web automation. A usual scheme: one test process per device, a device pool and a task queue where a test takes a free device and returns it after quit().
App state. The appium:noReset capability does not reset state between sessions (fast, but data stays), while appium:fullReset removes the app with its data and installs it anew (clean, but slow). The two flags are not combined. In practice a reset with full isolation is costly, so state is more often prepared faster: through an API or a deep link instead of walking the UI.
noReset and fullReset
Fast, but with leftovers
The app is not relaunched and data stays: handy for fast runs, but the test depends on the previous one.
Clean, but slow
The app with its data is removed and installed anew: isolation is high, startup time is longer.
Two platforms, one Page Object. The @AndroidFindBy and @iOSXCUITFindBy annotations with AppiumFieldDecorator substitute the locator by the platform of the driver:
public class LoginPage {
@AndroidFindBy(accessibility = "login_button")
@iOSXCUITFindBy(accessibility = "login_button")
private WebElement loginButton;
public LoginPage(AppiumDriver driver) {
PageFactory.initElements(new AppiumFieldDecorator(driver), this); // picks the locator by platform
}
public void login() { loginButton.click(); }
}
Causes of flakiness. The most frequent: Thread.sleep instead of explicit waits, animations, system dialogs and push notifications, fragile XPath, shared ports and devices, leftover state after the previous test, a slow emulator on CI. Each is treated its own way: waits and stable locators, disabled animations, isolation of sessions and data. On Android animations are turned off with device settings:
adb shell settings put global window_animation_scale 0
adb shell settings put global transition_animation_scale 0
adb shell settings put global animator_duration_scale 0 # turn animations off
Limits and alternatives.
- Retrying "until green" does not cure the cause, it only masks flakiness. If a retry is enabled temporarily, the report must show a mark that the test was retried.
- An emulator does not replace real devices: camera, biometrics, push and performance behave differently, and vendors change system dialogs. A typical strategy: the main run on emulators, a sample on real devices and in the cloud.
- A cloud removes fleet maintenance but adds a network between the test and the device, so tests get slower and timeouts appear. Check the parallel session limits of your plan.
- One server for many sessions saves memory, but a server failure stops all of them. For large fleets a separate server per device or group is run.
- State through an API is faster than the UI, but it does not replace one end-to-end test of the login itself: keep it.
Typical mistakes.
- Parallel sessions without a unique
systemPortorwdaLocalPort: "port already in use" or tests on someone else's device. - One account for all parallel tests.
fullReseteverywhere: runs several times slower, ornoReseteverywhere: tests depend on each other.- Not turning off animations and not removing system dialogs on CI.
- Treating failures with retries instead of looking for the cause.
Check yourself
Which parameters must be unique for parallel sessions?
Answer
The device (appium:udid) and the ports that sessions could share: appium:systemPort on Android, appium:wdaLocalPort on iOS, also chromedriverPort for WebView and derivedDataPath when WDA is built in parallel.
How do noReset and fullReset differ?
Answer
noReset does not reset the app between sessions: fast, but data stays. fullReset removes the app with its data and installs it again: clean, but slow. They are not used together.
Why does retrying a failed test "until green" not solve the stability problem?
Answer
It hides the cause: a race, an animation, shared state or a fragile locator. The test stays flaky and the run gets longer. The cause must be found and removed, and a retry kept only as temporary and visible in the report.
udid and ports (systemPort, wdaLocalPort). I cure flakiness with explicit waits and stable locators, not sleep. Middle/Senior: I build a device pool: one process per device, independent data per test, animations off, state prepared through an API or a deep link, and fullReset only where clean isolation is needed. Emulators for the main run, real devices and the cloud for a sample. Retries are temporary and visible in the report, and I look for the cause in the trace and the server logs.