Most Complete Appium Java Cheat Sheet
Initialize
// maven: io.appium:java-client
appiumLocalService = new AppiumServiceBuilder().usingAnyFreePort().build(); // Creates local Appium server instance
appiumLocalService.start(); // Starts local Appium server instance
var desiredCapabilities = new DesiredCapabilities();
// Android capabilities
desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Virtual Device");
desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "7.1");
// iOS capabilities
desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "XCUITest");
desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone 11");
desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");
desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "13.2");
// Install and start an app on Android
desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.example.android.apis");
desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, ".ApiDemos");
desiredCapabilities.setCapability(MobileCapabilityType.APP, "path/to/TestApp.apk");
// Install and start an app on iOS
desiredCapabilities.setCapability(MobileCapabilityType.APP, "path/to/TestApp.app.zip");
// Start mobile browser on Android
desiredCapabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome");
// Start mobile browser on iOS
desiredCapabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "safari");
// Set WebView Context for Hybrid Apps
driver.context(driver.getContextHandles().stream().filter(c -> c.contains("WEBVIEW")).findFirst().orElse(null));
// Use local server instance
driver = new AndroidDriver<AndroidElement>(appiumLocalService, desiredCapabilities); // initialize Android driver on local server instance
driver = new IOSDriver<IOSElement>(appiumLocalService, desiredCapabilities); // initialize iOS driver on local server instance
// Use remote Appium driver
driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"), desiredCapabilities); // initialize Android remote driver
driver = new IOSDriver<IOSElement>(new URL("http://0.0.0.0:4723/wd/hub"), desiredCapabilities); // initialize iOS remote driver
Locators Advanced Actions - Common
driver.findElementById("android:id/text1"); // Multitouch action
driver.findElementByClassName("android.widget.CheckBox"); TouchAction actionOne = new TouchAction(driver);
driver.findElementByXPath("//*[@text='Views']"); actionOne.press(PointOption.point(10, 10));
driver.findElementByAccessibilityId("Views"); actionOne.moveTo(PointOption.point(10, 100));
driver.findElementByImage(base64EncodedImageFile); actionOne.release();
driver.findElementByAndroidUIAutomator("new TouchAction actionTwo = new TouchAction(driver);
UiSelector().text(\"Views\");"); actionTwo.press(PointOption.point(20, 20));
driver.findElementByIosNsPredicate("type == 'XCUIElementBottn' AND actionTwo.moveTo(PointOption.point(20, 200));
name == 'ComputeSumButton'"); actionTwo.release();
// Find multiple elements MultiTouchAction action = new MultiTouchAction(driver);
driver.findElementsByClassName("android.widget.CheckBox"); action.add(actionOne);
action.add(actionTwo);
action.perform();
// Swipe using touch action
Actions TouchAction touchAction = new TouchAction(driver);
var element = driver.findElementById("android:id/text1");
// Alert handling Point point = element.getCoordinates().onPage();
driver.switchTo().alert().accept(); Dimension size = element.getSize();
driver.switchTo().alert().dismiss(); touchAction
// Basic actions; .press(PointOption.point(point.getX() + 5, point.getY() + 5))
element.click(); .waitAction(WaitOptions.waitOptions(Duration.ofMillis(200)))
element.sendKeys("textToType"); .moveTo(PointOption.point(point.getX() + size.getWidth() - 5,
element.clear(); point.getY() + size.getHeight() - 5))
// Change orientation .release().perform();
driver.rotate(ScreenOrientation.LANDSCAPE); // PORTRAIT // Scroll using JavascriptExecutor
// Clipboard var js = (JavascriptExecutor)driver;
driver.setClipboardText("1234", "plaintext"); Map<String, String> swipe = new HashMap<>();
String clipboard = driver.getClipboardText(); swipe.put("direction", "down"); // "up", "right", "left"
// Mobile gestures swipe.put("element", element.getId());
TouchAction touchAction = new TouchAction(driver); js.executeScript("mobile:swipe", swipe);
touchAction.tap(TapOptions.tapOptions() // Scroll element into view by Android UI automator
.withPosition(PointOption.point(x, y)).withTapsCount(count)); driver.findElementByAndroidUIAutomator("new UiScrollable(new
touchAction.press(PointOption.point(x, y)); UiSelector()).scrollIntoView(new UiSelector().text(\"Views\"));" );
touchAction.waitAction(WaitOptions // Update Device Settings
.waitOptions(Duration.ofMillis(200))); driver.setSetting(Setting.KEYBOARD_AUTOCORRECTION, false);
touchAction.moveTo(PointOption.point(x, y)); driver.getSettings(); // returns Map<String, Object>
touchAction.release(); // Take screenshot
touchAction.longPress(PointOption.point(x, y)); ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); // BYTES,
touchAction.perform(); BASE64 – returns File, byte[] or String (base64 encoded PNG)
// Simulate phone call (Emulator only) // Screen record
driver.makeGsmCall("5551237890", GsmCallActions.ACCEPT); driver.startRecordingScreen();
driver.makeGsmCall("5551237890", GsmCallActions.CALL); driver.stopRecordingScreen();
driver.makeGsmCall("5551237890", GsmCallActions.CALL);
driver.makeGsmCall("5551237890", GsmCallActions.HOLD);
// Set GSM voice state (Emulator only)
driver.setGsmVoice(GsmVoiceState.DENIED);
Advanced Actions - Android
driver.setGsmVoice(GsmVoiceState.HOME);
driver.setGsmVoice(GsmVoiceState.OFF); // Add photos
driver.setGsmVoice(GsmVoiceState.ON); driver.pushFile("/mnt/sdcard/Pictures/img.jpg", new
driver.setGsmVoice(GsmVoiceState.ROAMING); File("path/to/img.jpg"));
driver.setGsmVoice(GsmVoiceState.SEARCHING); // Simulate hardware key
driver.setGsmVoice(GsmVoiceState.UNREGISTERED); driver.pressKey(new KeyEvent().withKey(AndroidKey.HOME));
// Set GSM signal strength (Emulator only) driver.longPressKey(new KeyEvent().withKey(AndroidKey.POWER));
driver.setGsmSignalStrength(GsmSignalStrength.GOOD); // Set battery percentage
driver.setGsmSignalStrength(GsmSignalStrength.GREAT); driver.setPowerCapacity(100);
driver.setGsmSignalStrength(GsmSignalStrength.MODERATE); // Set the state of the battery charger to connected or not
driver.setGsmSignalStrength(GsmSignalStrength.NONE_OR_UNKNOWN); driver.setPowerAC(PowerACState.ON);
driver.setGsmSignalStrength(GsmSignalStrength.POOR); driver.setPowerAC(PowerACState.OFF);
// Set network speed (Emulator only) // Get performance data
driver.setNetworkSpeed(NetworkSpeed.LTE); driver.getPerformanceData("my.app.package", "cpuinfo", 5); // returns
// Simulate receiving SMS message (Emulator only) List<List<Object>>
driver.sendSMS("555-555-5555", "Your code is 123456");
// Toggle services
driver.toggleAirplaneMode();
driver.toggleData(); Advanced Actions - iOS
driver.toggleLocationServices();
driver.toggleWifi(); // Add photos
// Soft keyboard actions driver.pushFile("img.jpg", new File("path/to/img.jpg"));
driver.isKeyboardShown(); // returns boolean // Simulate hardware key
driver.hideKeyboard(); Map<String, Object> args = new HashMap<>();
// Lock device args.put("name", "home"); // volumeup; volumedown
driver.isDeviceLocked(); // returns boolean driver.executeScript("mobile: pressButton", args);
driver.lockDevice(); // Sending voice commands to Siri
driver.unlockDevice(); Map<String, Object> args = new HashMap<>();
// Notifications args.put("text", "Hey Siri, what's happening?");
driver.openNotifications(); driver.executeScript("mobile: siriCommand", args);
// Geolocation actions // Perform Touch ID in iOS Simulator
driver.location(); // returns Location{Latitude, Longitude, driver.performTouchID(false); // Simulates a failed touch ID
Altitude} driver.performTouchID(true); // Simulates a passing touch ID
driver.setLocation(new Location(94.23, 121.21, 11.56)); // Shake device
// Get system time driver.shake();
driver.getDeviceTime(); // returns String
// Get display density
driver.getDisplayDensity(); // returns long
// File actions Application Management - iOS
driver.pushFile("/data/local/tmp/file", new File("path/to/file"));
driver.pullFile("/path/to/device/file"); // returns byte[]
// Install app
driver.pullFolder("/path/to/device"); // returns byte[]
Map<String, Object> args = new HashMap<>();
args.put("app", "path/to/app.ipa");
driver.executeScript("mobile: installApp", args);
Application Management - Android // Remove app
Map<String, Object> args = new HashMap<>();
args.put("bundleId", "com.myapp");
// Install app driver.executeScript("mobile: removeApp", args);
driver.installApp("path/to/app.apk"); // Verify app is installed
// Remove app Map<String, Object> args = new HashMap<>();
driver.removeApp("com.example.AppName"); args.put("bundleId", "com.myapp");
// Verify app is installed (boolean)driver.executeScript("mobile: isAppInstalled", args);
driver.isAppInstalled("com.example.android.apis"); // returns bool // Launch app
// Launch app Map<String, Object> args = new HashMap<>();
driver.launchApp(); args.put("bundleId", "com.apple.calculator");
// Start activity driver.executeScript("mobile: launchApp", args);
driver.startActivity(new Activity("com.example.android.apis", // Switch app to foreground
".ApiDemos")); Map<String, Object> args = new HashMap<>();
// Get current activity args.put("bundleId", "com.myapp");
driver.currentActivity(); // returns String driver.executeScript("mobile: activateApp", args);
// Get current package // Terminate app
driver.getCurrentPackage(); // returns String Map<String, Object> args = new HashMap<>();
// Reset app args.put("bundleId", "com.myapp");
driver.resetApp(); // will return false if app is not running, otherwise true
// Close app (boolean)driver.executeScript("mobile: terminateApp", args);
driver.closeApp(); // Check app's current state
// Run current app in background Map<String, Object> args = new HashMap<>();
driver.runAppInBackground(Duration.ofSeconds(10)); // 10 seconds args.put("bundleId", "com.myapp");
// Terminate app // will return false if app is not running, otherwise true
driver.terminateApp("com.example.android.apis"); // returns bool (ApplicationState)driver.executeScript("mobile: queryAppState", args);
// Check app's current state
driver.queryAppState("com.example.android.apis"); // returns
ApplicationState
// Get app strings
driver.getAppStringMap("en", "path/to/file"); // returns
Map<String, String>