Appium Basics

What is Appium?

Appium is an open-source automation tool used for testing mobile applications. It allows testers and developers to automate the testing of mobile apps across different platforms (iOS, Android) using the same API (Application Programming Interface). Appium works by interacting with the user interface (UI) of mobile apps just like a real user would, performing actions such as tapping buttons, entering text, and swiping screens. It supports multiple programming languages and test frameworks, making it flexible and easy to use for mobile app testing. With Appium, you can write automated tests that simulate user interactions and validate the behavior and functionality of your mobile apps.

Components of Appium

Appium comprises several key components that work together to facilitate mobile app automation. These components include:

  • Appium Server:
    The core component of Appium responsible for facilitating communication between the testing scripts and the mobile devices or emulators.
  • Appium Clients:
    These are libraries or frameworks available in various programming languages (e.g., Java, Python, JavaScript, C#) that provide bindings to interact with the Appium Server.
  • Mobile Platforms:
    Appium supports automation on multiple mobile platforms, including iOS and Android.
  • WebDriver Protocol:
    Appium implements the WebDriver protocol, a standardized protocol for automating web browsers.
  • Device Providers:
    Appium interacts with real mobile devices and emulators/simulators provided by device providers.
  • Automation Drivers:
    These are platform-specific automation drivers responsible for translating the WebDriver commands received from the Appium Server into actions on the mobile device or emulator.
  • Appium Inspector:
    An optional tool provided by Appium for inspecting the UI elements of mobile apps.

Appium Locators

Java
C#
Python
Javascript
Copy
driver.findElement(By.id("id"));

driver.findElement(By.name("name"));

driver.findElement(By.className("className"));

driver.findElement(By.tagName("tagName"));

driver.findElement(By.linkText("linkText"));

driver.findElement(By.partialLinkText("partialLinkText"));

driver.findElement(By.cssSelector("cssSelector"));

driver.findElement(By.xpath("xpath"));

driver.findElement(AppiumBy.accessibilityId("accessibilityId"));

driver.findElement(AppiumBy.androidDataMatcher("androidDataMatcher"));

driver.findElement(AppiumBy.androidViewMatcher("androidViewMatcher"));

driver.findElement(AppiumBy.androidUIAutomator("androidUIAutomator"));

driver.findElement(AppiumBy.androidViewTag("androidViewTag"));

driver.findElement(AppiumBy.image("image"));

driver.findElement(AppiumBy.iOSClassChain("iOSClassChain"));

driver.findElement(AppiumBy.iOSNsPredicateString("iOSNsPredicateString"));
Content coming soon
Content coming soon
Content coming soon

Driver Methods

Java
C#
Python
Javascript
Copy
//This method is used to add custom appium commands in Appium 2.0.
driver.addCommand(HttpMethod.GET,"url", "testMethod");

//Executes the given command and returns a response.
driver.execute("command");

//Verifies if the given extension is not present in the list of absent
// extensions for the given driver instance.
driver.assertExtensionExists("extensionName");

//This method is used to get build version status of running Appium server.
driver.getStatus();

//Marks the given extension as absent for the given driver instance.
// This API is designed for private usage.
driver.markExtensionAbsence("extName");

//Deletes all downloadable files.
driver.deleteDownloadableFiles();

//Downloads a file from the specified location.
driver.downloadFile("fileName", Paths.get("path"));

//Performs images matching by template to find possible occurrence of
// the partial image in the full image with default options.
driver.findImageOccurrence(new File("File"),new File("file"));

//Set the file detector to be used when sending keyboard input. By default,
// this is set to a file detector that does nothing
FileDetector fd = driver.getFileDetector();

//Get settings stored for this test session It's probably better to use
// a convenience function, rather than use this function directly.
driver.getSettings();

//Validates if the driver is currently in a web browser context.
driver.isBrowser();

//Get the names of available contexts.
((SupportsContextSwitching) driver).getContextHandles();
Content coming soon
Content coming soon
Content coming soon

Driver Methods (Native)

Java
C#
Python
Javascript
Copy
//Retrieves battery info from the device under test.
driver.getBatteryInfo();

//Toggles Wifi on and off.
driver.toggleWifi();

//Opens notification drawer on the device under test.
driver.openNotifications();

//Activates the given app if it installed, but not running
// or if it is running in the background.
driver.activateApp("com.example.com");

//Switches to the given context.
driver.context("context");

//Get the current activity being run on the mobile device.
driver.currentActivity();

//Authenticate users by using their finger print scans on supported emulators.
driver.fingerPrint(1);

//Get the clipboard text.
driver.getClipboardText();

//Get the name of the current context.
driver.getContext();

//Hides the keyboard if it is showing.
driver.hideKeyboard();

//Install an app on the mobile device.
driver.installApp("com.example.com");

//This method locks a device. It will return silently if the device is already locked.
driver.lockDevice();

//Get device orientation.
driver.getOrientation();

//Remove the specified app from the device (uninstall).
driver.removeApp("com.example.com");

//Check GPS service state.
driver.isLocationServicesEnabled();

//Emulate GSM call event on the connected emulator.
driver.makeGsmCall("1234567890", GsmCallActions.CALL);

//Runs the current app in the background for the time requested.
driver.runAppInBackground(Duration.ofSeconds(10));

//Emulate send SMS event on the connected emulator.
driver.sendSMS("1234567890","Hello");

//Sets the current device's geolocation coordinates.
driver.setLocation(new AndroidGeoLocation(11.334,12.98888));

//Terminate the particular application if it is running.
driver.terminateApp("com.example.com");

//Toggle Airplane mode and this works on Android versions below 6 and above 10.
driver.toggleAirplaneMode();

//Toggles GPS service state. This method only works reliably since API 31
driver.toggleLocationServices();

//Unlock the device if it is locked.
//This method will return silently if the device is not locked.
driver.unlockDevice();
Content coming soon
Content coming soon
Content coming soon

Appium Gestures


Tap Gesture

Java
C#
Python
Javascript
Copy
WebElement element = driver.findElement(AppiumBy.id(""));
JavascriptExecutor jsTap = (JavascriptExecutor) driver;
Map<String, Object> paramsTap = new HashMap<>();
paramsTap.put("element", ((RemoteWebElement) element).getId());
jsTap.executeScript("mobile: tap", paramsTap);
Content coming soon
Content coming soon
Content coming soon

Double Tap Gesture

Java
C#
Python
Javascript
Copy
WebElement element = driver.findElement(AppiumBy.id("id"));
JavascriptExecutor jsDoubleTap = (JavascriptExecutor) driver;
Map<String, Object> paramsDoubleTap = new HashMap<>();
paramsDoubleTap.put("element", ((RemoteWebElement) element).getId());
jsDoubleTap.executeScript("mobile: doubleTap", paramsDoubleTap);
Content coming soon
Content coming soon
Content coming soon

Click Gesture

Java
C#
Python
Javascript
Copy
driver.executeScript("mobile: clickGesture", ImmutableMap.of(
	"elementId", ((RemoteWebElement) element).getId()
));
C# code coming soon
Python code coming soon
Javascript code coming soon

Double Click Gesture

Java
C#
Python
Javascript
Copy
((JavascriptExecutor) driver).executeScript("mobile: doubleClickGesture", 
ImmutableMap.of("elementId", ((RemoteWebElement) element).getId()
));
Content coming soon
Content coming soon
Content coming soon

Swipe Gesture

Java
C#
Python
Javascript
Copy
((JavascriptExecutor) driver).executeScript("mobile: swipeGesture",
 ImmutableMap.of("left", 100, "top", 100, "width", 200, "height", 200,
                "direction", "left", "percent", 0.75
));
C# code coming soon
Python code coming soon
Javascript code coming soon

Scroll Gesture

Java
C#
Python
Javascript
Copy
((JavascriptExecutor) driver).executeScript("mobile: scrollGesture", 
ImmutableMap.of("left", 100, "top", 100, "width", 200, "height", 200,
		"direction", "down", "percent", 3.0 ));
C# code coming soon
Python code coming soon
Javascript code coming soon

Drag Gesture

Java
C#
Python
Javascript
Copy
((JavascriptExecutor) driver).executeScript("mobile: dragGesture", 
ImmutableMap.of("elementId", ((RemoteWebElement) element).getId(),
"endX", 100, "endY", 100 ));
C# code coming soon
Python code coming soon
Javascript code coming soon

Fling Gesture

Java
C#
Python
Javascript
Copy
((JavascriptExecutor) driver).executeScript("mobile: flingGesture", 
ImmutableMap.of("elementId", ((RemoteWebElement) element).getId(),
"direction", "down", "speed", 500 ));
C# code coming soon
Python code coming soon
Javascript code coming soon

Pinch Open Gesture

Java
C#
Python
Javascript
Copy
((JavascriptExecutor) driver).executeScript("mobile: pinchOpenGesture",
ImmutableMap.of("elementId", ((RemoteWebElement) element).getId(),"percent", 0.75));
C# code coming soon
Python code coming soon
Javascript code coming soon

Pinch Close Gesture

Java
C#
Python
Javascript
Copy
((JavascriptExecutor) driver).executeScript("mobile: pinchCloseGesture",
ImmutableMap.of("elementId", ((RemoteWebElement) element).getId(),"percent", 0.75));
C# code coming soon
Python code coming soon
Javascript code coming soon