CheatSheet for Appium(Android+iOS)

Mayank Gupta
2 min readJul 3, 2023

--

Here’s a cheatsheet for Appium, a popular mobile testing framework:

// Import the necessary Appium classes
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.ios.IOSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

// DesiredCapabilities for Android
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("platformName", "Android");
desiredCapabilities.setCapability("deviceName", "deviceName");
desiredCapabilities.setCapability("appPackage", "appPackage");
desiredCapabilities.setCapability("appActivity", "appActivity");
AppiumDriver<MobileElement> driver = new AndroidDriver<>(new URL("http://localhost:4723/wd/hub"), desiredCapabilities);

// DesiredCapabilities for iOS
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("platformName", "iOS");
desiredCapabilities.setCapability("deviceName", "deviceName");
desiredCapabilities.setCapability("platformVersion", "platformVersion");
desiredCapabilities.setCapability("app", "path/to/app");
AppiumDriver<MobileElement> driver = new IOSDriver<>(new URL("http://localhost:4723/wd/hub"), desiredCapabilities);

// Find element by ID
MobileElement elementById = driver.findElement(By.id("elementId"));

// Find element by accessibility id
MobileElement elementByAccessibilityId = driver.findElement(ByAccessibilityId("accessibilityId"));

// Find element by XPath
MobileElement elementByXPath = driver.findElement(By.xpath("//xpath-expression"));

// Find element by class name
MobileElement elementByClassName = driver.findElement(By.className("className"));

// Find element by name
MobileElement elementByName = driver.findElement(By.name("elementName"));

// Find element by Android UI Automator
MobileElement elementByAndroidUIAutomator = driver.findElement(MobileBy.AndroidUIAutomator("androidUIAutomatorExpression"));

// Find element by iOS predicate
MobileElement elementByiOSPredicate = driver.findElement(MobileBy.iOSNsPredicateString("iOSPredicateString"));

// Type text into an input field
elementById.sendKeys("text to type");

// Click on an element
elementByXPath.click();

// Get the text of an element
String text = elementByClassName.getText();
System.out.println(text);

// Get the attribute value of an element
String attributeValue = elementByName.getAttribute("attributeName");
System.out.println(attributeValue);

// Wait for an element to be visible
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(elementById));

// Perform swipe gesture
driver.swipe(startX, startY, endX, endY, duration);

// Perform tap gesture
TouchAction action = new TouchAction(driver);
action.tap(tapOptions().withElement(element(elementByAccessibilityId))).perform();

// Perform long press gesture
action.longPress(longPressOptions().withElement(element(elementById)).withDuration(ofSeconds(2))).release().perform();

// Capture a screenshot
File screenshot = driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("path/to/save/screenshot.png"));

// Close the app
driver.closeApp();

// Quit the driver and close the session
driver.quit();

This is just a basic cheatsheet to get you started with Appium. The framework provides many more features and commands for advanced mobile testing scenarios. Make sure to refer to the official Appium documentation for more detailed information and examples: http://appium.io/docs/en/about-appium/api/

For more articles consider making a follow on my account. Thanks…

--

--

Mayank Gupta

QA Automation Lead | Web Automation | Mobile Automation | API Automation l Performance | Web Security | IOT | Blockchain