Ultimate Appium Scripts Cheatsheet

Introduction to Appium

Appium is an open-source automation tool for mobile applications, enabling testers to automate native, hybrid, and web applications on iOS, Android, and Windows platforms. Its significance lies in cross-platform compatibility, device flexibility, and integration with popular programming languages and test frameworks.

Core Concepts & Architecture

ComponentDescription
Appium ServerMiddleware that receives commands from the client and executes them on mobile devices
Client LibrariesLanguage-specific libraries that send commands to the server
WebDriver ProtocolCommunication protocol based on Selenium’s JSON Wire Protocol
Mobile DriversPlatform-specific drivers (XCUITest, UiAutomator2, WinAppDriver)
Real Devices/EmulatorsPhysical devices or emulators/simulators where tests run

Setting Up Appium Environment

Prerequisites

  • Node.js & npm
  • Java Development Kit (JDK 8 or higher)
  • Android SDK (for Android testing)
  • Xcode (for iOS testing)
  • Appium Desktop (optional for inspector)

Installation Commands

# Install Appium globally
npm install -g appium

# Install Appium Doctor for environment verification
npm install -g appium-doctor

# Check environment setup
appium-doctor --android
appium-doctor --ios

# Start Appium server
appium

Desired Capability Examples

Android Capabilities

DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("platformName", "Android");
caps.setCapability("deviceName", "Android Device");
caps.setCapability("automationName", "UiAutomator2");
caps.setCapability("app", "/path/to/app.apk");
// For installed app
caps.setCapability("appPackage", "com.example.app");
caps.setCapability("appActivity", "com.example.app.MainActivity");

iOS Capabilities

DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("platformName", "iOS");
caps.setCapability("deviceName", "iPhone 12");
caps.setCapability("platformVersion", "14.5");
caps.setCapability("automationName", "XCUITest");
caps.setCapability("app", "/path/to/app.ipa");
caps.setCapability("udid", "device-udid-goes-here"); // For real device

Setting Up Test Scripts

Java (with TestNG)

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.*;
import java.net.URL;

public class AppiumBasicTest {
    
    private AppiumDriver driver;
    
    @BeforeTest
    public void setUp() throws Exception {
        DesiredCapabilities caps = new DesiredCapabilities();
        // Set desired capabilities
        caps.setCapability("platformName", "Android");
        caps.setCapability("deviceName", "Android Device");
        caps.setCapability("automationName", "UiAutomator2");
        caps.setCapability("app", "/path/to/app.apk");
        
        // Initialize driver
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), caps);
    }
    
    @Test
    public void sampleTest() {
        // Test code here
    }
    
    @AfterTest
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

Python

from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
import unittest

class AppiumTest(unittest.TestCase):
    
    def setUp(self):
        caps = {
            "platformName": "Android",
            "deviceName": "Android Device",
            "automationName": "UiAutomator2",
            "app": "/path/to/app.apk"
        }
        self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
        
    def test_sample(self):
        # Test code here
        
    def tearDown(self):
        if self.driver:
            self.driver.quit()
            
if __name__ == '__main__':
    unittest.main()

Element Locator Strategies

StrategyJava ExamplePython Example
IDdriver.findElement(By.id("com.example.app:id/login"))driver.find_element(MobileBy.ID, "com.example.app:id/login")
Accessibility IDdriver.findElement(AppiumBy.accessibilityId("login"))driver.find_element(MobileBy.ACCESSIBILITY_ID, "login")
XPathdriver.findElement(By.xpath("//android.widget.Button[@text='Login']"))driver.find_element(MobileBy.XPATH, "//android.widget.Button[@text='Login']")
Class Namedriver.findElement(By.className("android.widget.Button"))driver.find_element(MobileBy.CLASS_NAME, "android.widget.Button")
Predicate (iOS)driver.findElement(MobileBy.iOSNsPredicateString("name == 'login'"))driver.find_element(MobileBy.IOS_PREDICATE, "name == 'login'")
Android UIAutomatordriver.findElement(AppiumBy.androidUIAutomator("new UiSelector().text(\"Login\")"))driver.find_element(MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("Login")')

Common Mobile Gestures & Interactions

Tap/Click

// Java
driver.findElement(By.id("button")).click();
# Python
driver.find_element(MobileBy.ID, "button").click()

Swipe/Scroll

// Java
TouchAction touchAction = new TouchAction(driver);
touchAction.press(PointOption.point(500, 1000))
           .waitAction(WaitOptions.waitOptions(Duration.ofMillis(1000)))
           .moveTo(PointOption.point(500, 200))
           .release()
           .perform();
# Python
from appium.webdriver.common.touch_action import TouchAction

actions = TouchAction(driver)
actions.press(x=500, y=1000).wait(1000).move_to(x=500, y=200).release().perform()

Long Press

// Java
TouchAction touchAction = new TouchAction(driver);
touchAction.longPress(PointOption.point(100, 100))
           .waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2)))
           .release()
           .perform();
# Python
actions = TouchAction(driver)
actions.long_press(x=100, y=100, duration=2000).release().perform()

Multi-touch (Pinch/Zoom)

// Java
TouchAction action1 = new TouchAction(driver);
action1.press(PointOption.point(100, 100))
       .moveTo(PointOption.point(50, 50))
       .release();

TouchAction action2 = new TouchAction(driver);
action2.press(PointOption.point(200, 200))
       .moveTo(PointOption.point(250, 250))
       .release();

MultiTouchAction multiAction = new MultiTouchAction(driver);
multiAction.add(action1).add(action2).perform();
# Python
from appium.webdriver.common.multi_action import MultiAction

action1 = TouchAction(driver)
action1.press(x=100, y=100).move_to(x=50, y=50).release()

action2 = TouchAction(driver)
action2.press(x=200, y=200).move_to(x=250, y=250).release()

multi_action = MultiAction(driver)
multi_action.add(action1, action2)
multi_action.perform()

Handling Waiting Mechanisms

Implicit Wait

// Java
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
# Python
driver.implicitly_wait(10)

Explicit Wait

// Java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("element_id")));
# Python
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
element = wait.until(EC.visibility_of_element_located((MobileBy.ID, "element_id")))

Fluent Wait

// Java
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
    .withTimeout(Duration.ofSeconds(30))
    .pollingEvery(Duration.ofSeconds(5))
    .ignoring(NoSuchElementException.class);

WebElement element = wait.until(driver -> {
    return driver.findElement(By.id("elementId"));
});

Advanced Appium Commands

App Management

// Java
// Install app
driver.installApp("/path/to/app.apk");

// Check if app is installed
boolean isInstalled = driver.isAppInstalled("com.example.app");

// Launch app
driver.launchApp();

// Close app
driver.closeApp();

// Reset app
driver.resetApp();

// Run app in background
driver.runAppInBackground(Duration.ofSeconds(10));
# Python
# Install app
driver.install_app('/path/to/app.apk')

# Check if app is installed
is_installed = driver.is_app_installed('com.example.app')

# Launch app
driver.launch_app()

# Close app
driver.close_app()

# Reset app
driver.reset()

# Run app in background
driver.background_app(10)

Device Interactions

// Java
// Lock device
driver.lockDevice();

// Check if device is locked
boolean isLocked = driver.isDeviceLocked();

// Unlock device
driver.unlockDevice();

// Get device time
String time = driver.getDeviceTime();

// Set device orientation
driver.rotate(ScreenOrientation.LANDSCAPE);

// Press key code (Android)
((AndroidDriver) driver).pressKey(new KeyEvent(AndroidKey.BACK));

// Hide keyboard
driver.hideKeyboard();
# Python
# Lock device
driver.lock()

# Check if device is locked
is_locked = driver.is_locked()

# Unlock device
driver.unlock()

# Get device time
time = driver.device_time

# Set device orientation
driver.orientation = "LANDSCAPE"

# Press key code (Android)
from appium.webdriver.extensions.android.nativekey import AndroidKey
driver.press_keycode(AndroidKey.BACK)

# Hide keyboard
driver.hide_keyboard()

Working with Context (Hybrid Apps)

// Java
// Get all contexts
Set<String> contexts = driver.getContextHandles();
for (String context : contexts) {
    System.out.println(context);
}

// Switch to WEBVIEW
driver.context("WEBVIEW_com.example.app");

// Switch back to NATIVE_APP
driver.context("NATIVE_APP");
# Python
# Get all contexts
contexts = driver.contexts
for context in contexts:
    print(context)
    
# Switch to WEBVIEW
driver.switch_to.context('WEBVIEW_com.example.app')

# Switch back to NATIVE_APP
driver.switch_to.context('NATIVE_APP')

Common Challenges & Solutions

ChallengeSolution
Element not foundUse appropriate wait strategies and verify locators with Appium Inspector
Session startup failureCheck device connectivity, capabilities config, and verify device isn’t locked
Slow test executionOptimize waits, use proper locators, avoid thread.sleep, and implement parallel execution
Unstable testsAdd proper waits, improve element identification strategies, and handle app state properly
Gestures not workingVerify coordinates, ensure sufficient wait times between actions, and check screen orientation
Handling alerts/popupsUse driver.switchTo().alert() methods for handling alerts
Performance issuesUse local devices instead of cloud when possible, optimize setUp/tearDown methods
Android keyboard issuesUse adb keyboard settings or driver.hideKeyboard() appropriately

Best Practices for Appium Scripts

  1. Use unique and stable locators: Prioritize accessibility IDs and IDs over XPath
  2. Implement Page Object Model: Separate page elements and actions from test logic
  3. Handle waits properly: Use explicit waits over implicit waits for better control
  4. Keep test data separate: Externalize test data in JSON/CSV/Excel files
  5. Implement proper reporting: Use frameworks like Extent Reports or Allure for detailed test reports
  6. Set up proper logging: Implement detailed logging for easier debugging
  7. Handle device diversity: Test on different screen sizes and OS versions
  8. Configure timeouts appropriately: Set session, command, and idle timeouts based on app needs
  9. Clean up resources: Properly terminate driver sessions in tearDown methods
  10. Version control your tests: Keep test scripts in version control systems like Git

Example Test Framework Structure

appium-test-framework/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/example/
│   │   │       ├── pages/       # Page objects
│   │   │       ├── utils/       # Helper utilities
│   │   │       └── constants/   # App constants
│   ├── test/
│   │   ├── java/
│   │   │   └── com/example/
│   │   │       ├── tests/       # Test classes
│   │   │       └── base/        # Base test setup
│   │   └── resources/
│   │       ├── testdata/        # Test data files
│   │       └── config/          # Configuration files
├── pom.xml                      # Dependencies (Maven)
└── README.md                    # Project documentation

Resources for Further Learning

Scroll to Top