App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Get Started free
Home Guide Top Appium Commands every Developer must know

Top Appium Commands every Developer must know

Mobile phones have evolved from communication devices to a gadget that can basically fulfill a common man’s requirement- from online shopping to online food ordering. This is done via Mobile Applications that form the core of a number of businesses around the globe. Due to this, there is a need to develop quality Apps which has led to an increase in demand for Mobile Application testing

Mobile Application testing is either done Manually or is Automated. Mobile Application tests can be automated via open-source tools such as Appium. Every automation tester or developer should be familiar with different Appium commands in order to create robust mobile apps. However, knowing all the commands is a challenge, hence, a quick reference cheat sheet will be quite helpful here.

What is Appium?

Appium is a test automation tool used for testing Mobile Applications. It’s open source and supports various platforms such as iOS, Android, and Windows. It allows users to test various types of Mobile Applications such as:

  1. Native Apps: These are native to a particular platform and are written using Android, iOS, or Windows SDKs.
  2. Web Apps: These are pure web apps and are accessed via native browsers like Chrome, Safari, Firefox, etc.
  3. Hybrid Apps: These are an amalgamation of both Native and Web apps.

Learn about the difference between Native, Web, and Hybrid Apps in detail.

Appium offers cross-platform application testing that is, a single API works for both Android and iOS platforms. This means the same code can be used by developers or testers for all the platforms. Like Selenium, Appium also allows testers to write test scripts in different programming languages such as Python, Java, Ruby, PHP, JavaScript, and C#.

Appium Commands Cheat Sheet

Pre-Requisites

Before starting with App Automation Testing with Appium, you would need to follow these steps:

  • Ensure that the Java Library is installed for Appium.
  • Download and install Maven following the steps from the official website.
  • Import statements for various commands can be as follows. Add them to your program as per the commands in use:
  1. import org.openqa.selenium.WebElement;
  2. import org.openqa.selenium.JavascriptExecutor;
  3. import org.openqa.selenium.support.FindBy;
  4. import org.openqa.selenium.support.FindBys;
  5. import org.openqa.selenium.remote.RemoteWebElement;
  6. import org.openqa.selenium.remote.DesiredCapabilities;
  7. import org.openqa.selenium.Dimension;
  8. import org.openqa.selenium.By;
  9. import org.openqa.selenium.Keys;
  10. import org.openqa.selenium.WebDriver;
  11. import io.appium.java_client.pagefactory.*;
  12. import io.appium.java_client.ios.IOSElement;
  13. import io.appium.java_client.AppiumDriver;
  14. import io.appium.java_client.MobileElement;
  15. import io.appium.java_client.android.AndroidDriver;
  16. import io.appium.java_client.ios.IOSDriver;
  17. import io.appium.java_client.MobileElement;
  18. import io.appium.java_client.MultiTouchAction;
  19. import io.appium.java_client.TouchAction;
  20. import io.appium.java_client.android.AndroidElement;

Here are the Basic Appium Commands in the Java version:

Initialize Desired Capabilities

  • Create local Appium Server instance

 appiumLocalService = new AppiumServiceBuilder().usingAnyFreePort().build(); 

  • Start local Appium Server instance

 appiumLocalService.start();

  • iOS Capabilities

Example:

DesiredCapabilities caps = new DesiredCapabilities();

caps.setCapability(“automationName”, “XCUITest”);

caps.setCapability(“deviceName”, “iPhone 13”);

caps.setCapability(“platformName”, “iOS”);

caps.setCapability(“platformVersion”, “15.6”);

  • Android Capabilities

Example:

DesiredCapabilities caps = new DesiredCapabilities();

caps.setCapability(“automationName”, “UiAutomator2”);

caps.setCapability(“deviceName”, “Samsung”);

caps.setCapability(“platformName”, “Android”);

caps.setCapability(“platformVersion”, “7.1”);

  • Install an app on iOS

 DesiredCapabilities caps = new DesiredCapabilities();

caps.setCapability(“app”, “path/to/TestApp.app.zip”);

  • Install an app on Android

Example:

DesiredCapabilities caps = new DesiredCapabilities();

caps.setCapability(“appPackage”, “com.android.calculator2”);

caps.setCapability(“appActivity”, “com.android.calculator2.Calculator”);

caps.setCapability(“app”, “path/to/TestApp.apk”);

  • Start browser on iOS

 DesiredCapabilities caps = new DesiredCapabilities();

caps.setCapability(“browserName”, “Safari”);

  • Start browser on Android

DesiredCapabilities caps = new DesiredCapabilities();

caps.setCapability(“browserName”, “Chrome”);

  • Initialize iOS driver on the local server instance

driver = new IOSDriver<IOSElement>(appiumLocalService, caps); 

  • Initialize Android driver on the local server instance

driver = new AndroidDriver<AndroidElement>(appiumLocalService, caps); 

  • Set WebView Context for Hybrid Apps

driver.context(driver.getContextHandles().stream().filter(c -> c.contains(“WEBVIEW”)).findFirst().orElse(null)); 

  • Initialize iOS remote driver

Remote Webdriver helps in making a network request to a Selenium hub to start a driver session since Appium operates on the Client-Server model. It is not recommended to use the Remote WebDriver directly. Hence, it is used with the IOS Driver and Android Driver.

IOS and Android Drivers inherit from Appium Driver. They add additional functions that are useful to the context of mobile automation on IOS and Android devices respectively through Appium.

In the example below, the URL is pointing to the local host. 

Note: You can include the URL which points to your remote web server as well here.

driver = new IOSDriver<IOSElement>(new URL(“http://0.0.0.0:4723/wd/hub“), caps); //This URL points to the local server

  • Initialize Android remote driver

driver = new AndroidDriver<AndroidElement>(new URL(“
http://127.0.0.1:4723/wd/hub“), caps); //This URL points to the local server

Locator Strategies

  • Locate by ID

driver.findElementById(“android:id/button”); 

  • Locate by Accessibility ID

driver.findElementByAccessibilityId(“Views”); 

  • Locate by Class (Single Element)

driver.findElementByClassName(“android.widget.CheckBox”); 

  • Locate by Class (Multiple Elements)

driver.findElementsByClassName(“android.widget.CheckBox”); 

  • Locate by AndroidUIAutomator (UI Automator 2)

driver.findElementByAndroidUIAutomator(“new UiSelector().textContains(\”BUTTON\”);”); 

  • Locate by Image

driver.findElementByImage(base64EncodedImageFile); 

  • Locate by XPath – iOS

driver.findElementByXPath(“(//XCUIElementTypeButton)[1]”); 

  • Locate by XPath – Android

driver.findElementByXPath(“//*[@resource-id=’com.example.android.apis:id/button’]”); 

App & Device Actions

  • For Handling Alert

driver.switchTo().alert().accept();

driver.switchTo().alert().dismiss();

  • For Basic Input operations

element.click();

element.clear();

element.sendKeys(“textToType”);

  • For Changing orientation

driver.rotate(ScreenOrientation.LANDSCAPE); 

  • For Setting text on clipboard

driver.setClipboardText(“9876”, “UserText”);

String clipboard = driver.getClipboardText();

  • For Mobile gestures using TouchAction

TouchAction touchAction = new TouchAction(driver); 

  • For Tapping:

touchAction.tap(TapOptions.tapOptions()

.withPosition(PointOption.point(x, y));

  •  For Pressing:

touchAction.press(PointOption.point(x, y)); 

  • For Waiting:

touchAction.waitAction(WaitOptions

.waitOptions(Duration.ofMillis(200)));

  • For Moving:

touchAction.moveTo(PointOption.point(x, y));

  • For Long Press:

touchAction.longPress(PointOption.point(x, y)); 

  • For Releasing and Performing:

touchAction.release();

touchAction.perform();

  • For Toggle services

driver.toggleAirplaneMode();

driver.toggleData();

driver.toggleLocationServices();

driver.toggleWifi();

  • For Soft keyboard actions

driver.isKeyboardShown(); // returns boolean

driver.hideKeyboard();

  • For Locking device

driver.isDeviceLocked(); // returns boolean

driver.lockDevice();

driver.unlockDevice();

  • For Notifications

driver.openNotifications(); 

  • For File actions

driver.pushFile(“/data/local/tmp/file”, new File(“path/to/file”));

driver.pullFile(“/path/to/device/file”); // returns byte[]

driver.pullFolder(“/path/to/device”); // returns byte[]

  • For getting System time

driver.getDeviceTime(); // returns String 

Advanced Commands – both iOS & Android

  • For Multitouch actions

TouchAction actionOne = new TouchAction(driver); 

  • For Pressing at a point:

actionOne.press(PointOption.point(10, 10)); 

  • For Moving to a point:

actionOne.moveTo(PointOption.point(10, 100)); 

  • For Releasing

 actionOne.release(); 

Example:

TouchAction actionTwo = new TouchAction(driver);

actionTwo.press(PointOption.point(20, 20));

actionTwo.moveTo(PointOption.point(20, 200));

actionTwo.release();

MultiTouchAction action = new MultiTouchAction(driver);

action.add(actionOne);

action.add(actionTwo);

action.perform();

  • For Swipe using TouchAction

Example:

TouchAction touchAction = new TouchAction(driver);

WebElement element =  (WebElement) driver.findElementById(“android:id/text2”);

Point point = element.getCoordinates().onPage();

Dimension size = element.getSize();

touchAction.press(PointOption.point(point.getX() + 5, point.getY() + 5))

.waitAction(WaitOptions.waitOptions(Duration.ofMillis(200)))

.moveTo(PointOption.point(point.getX() + size.getWidth() – 5,

point.getY() + size.getHeight() – 5)).release().perform();


  • For Scroll using Javascript Executor

Example:

JavascriptExecutor js = (JavascriptExecutor) driver;

HashMap<String, String> scrollObject = new HashMap<String, String>();

scrollObject.put(“direction”, “down”); // up, left, right

scrollObject.put(“element”, ((RemoteWebElement) element).getId());

js.executeScript(“mobile: scroll”, scrollObject);


Example:

driver.findElementByAndroidUIAutomator(“new UiScrollable(new

UiSelector()).scrollIntoView(new UiSelector().text(\”Views\”));” );


  • For taking Screenshot

((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

 // BYTES,BASE64 – returns File, byte[] or String (base64 encoded PNG)


  • For updating Device Settings

driver.setSetting(Setting.KEYBOARD_AUTOCORRECTION, false);

driver.getSettings(); // returns Map<String, Object>


  • For Screen Recording

driver.startRecordingScreen();

driver.stopRecordingScreen();

Advanced Commands – iOS 

  • For shaking device

driver.shake();


  • For adding photos

driver.pushFile(“img.jpg”, new File(“path to img.jpg”));


  • For sending voice commands to SIRI

Example:

JavascriptExecutor js = (JavascriptExecutor) driver;

Map<String, Object> args = new HashMap<>();

args.put(“text”, “Hey Siri, what’s happening?”);

driver.executeScript(“mobile: siriCommand”, args);


  • For performing Touch ID in iOS Simulator

driver.performTouchID(true); // Simulates a correct touch ID

driver.performTouchID(false); // Simulates a failed touch ID


  • For simulating hardware key

Example:

JavascriptExecutor js = (JavascriptExecutor) driver;

Map<String, Object> args = new HashMap<>();

args.put(“name”, “home”); // volumeup; volumedown

driver.executeScript(“mobile: pressButton”, args);

Advanced Commands – Android 

  • For setting Battery Percentage

driver.setPowerCapacity(100);; 

  • For setting the state of the battery charger to connected or not

driver.setPowerAC(PowerACState.ON);

driver.setPowerAC(PowerACState.OFF);


  • For adding photos

driver.pushFile(“/Pictures/img.jpg”, new File(“path to img.jpg”));


  • For simulating hardware key

driver.pressKey(new KeyEvent().withKey(AndroidKey.HOME));

driver.longPressKey(new KeyEvent().withKey(AndroidKey.POWER));


  • For getting performance data

driver.getPerformanceData(“my.app.package”, “cpuinfo”, 6); // returns

List<List<Object>>

App Management – iOS 

  • For installing an app

Example:

args.put(“app”, “path/to/app.ipa”);

driver.executeScript(“mobile: installApp”, args);


  • For verifying if the app is installed

Example:

args.put(“bundleId”, “com.myapp”);

(boolean)driver.executeScript(“mobile: isAppInstalled”, args);


  • For launching the app

Example:

args.put(“bundleId”, “com.apple.calculator”);

driver.executeScript(“mobile: launchApp”, args);


  • For switching the app to foreground

Example:

args.put(“bundleId”, “com.myapp”);

driver.executeScript(“mobile: activateApp”, args);

  • For checking current state of the app

Example:

args.put(“bundleId”, “com.myapp”);

(ApplicationState)driver.executeScript(“mobile: queryAppState”, args);

// will return false if app is not running, otherwise return true


  • For terminating the app

Example:

args.put(“bundleId”, “com.myapp”);

(boolean)driver.executeScript(“mobile: terminateApp”, args);

// will return false if app is not running, otherwise return true


  • For removing the app

Example:

args.put(“bundleId”, “com.myapp”);

driver.executeScript(“mobile: removeApp”, args);

App Management – Android

  • For installing an app

driver.installApp(“path/to/app.apk”); 

  • For verifying if the app is installed

driver.isAppInstalled(“com.example.android.apis”); 

// returns bool

  • For launching the app

driver.launchApp(); 

  • For starting activity

driver.startActivity(new Activity(“com.example.android.apis”,

“.ApiDemos”));

  • For getting current activity

driver.currentActivity(); // returns String 

  • For getting current package

driver.getCurrentPackage(); // returns String 

  • For switching the app to foreground

driver.runAppInBackground(Duration.ofSeconds(10));

// Runs for 10 seconds

  • For checking current state of the app

driver.queryAppState(“com.example.android.apis”); 

// returns Application State

  • For getting app strings

driver.getAppStringMap(“en”, “path/to/file”);

 // returns Map<String, String>

  • For closing the app

driver.closeApp(); 

  • For reseting the app

driver.resetApp(); 

  • For terminating the app

driver.terminateApp(“com.example.android.apis”); // returns bool 

  • For removing the app

driver.removeApp(“com.example.AppName”); 

Appium mobile testing has become indispensable with the ever-increasing demands of fast and reliable testing that spans over various platforms, devices, and versions. Appium offers feasibility, flexibility, and cost-friendliness above other testing methods, which enables both development and testing teams to deliver great user experiences within the continuous delivery approach.

To perform mobile app testing using Appium, you could try apps like BrowserStack App Automate, which provides thousands of real mobile devices for testing. Using BrowserStack QAs can access real devices with real operating systems in order to run automated tests via Appium.

Try BrowserStack for Free

Tags
Appium Mobile App Testing

Featured Articles

Appium Tutorial for Mobile Application Testing

Desired Capabilities in Appium

App & Browser Testing Made Easy

Seamlessly test across 20,000+ real devices with BrowserStack