Touch Actions in Appium are a set of mobile automation commands, which can be used to simulate user interactions in the context of a touch screen device. The actions will mimic the gestures and touches of a user holding a touch device (such as a smartphone). Using touch actions means you can mimic various interactions that one usually does on a phone, such as:
- Tap
- Swipe
- Pinch
In this article will give an overview on how to mimck these actions in your automated Appium tests, running on mobile devices.
What are the various Touch Actions in Appium?
Below is a list of the most commonly used touch actions with Appium:
- Swipe
- Tap
- Pinch
- Long Press
Swipe
A swipe is an action where you start from an x, y (start) position and keep one or more fingers down on the screen. The swipe will stop when you reach another x, y (end) position and lift all fingers up. This logic can be written in code:
The code will perform an action where a long press will start from the start position. Moving to the end position where a release is called.
Tap
A tap is a simple click on a coordinate of the screen. You will require both the x and y coordinate to perform the tap, or an element. In case of an element, Appium will find the coordinates of the center of the element.
element = driver.find_element(AppiumBy.XPATH, '//android.widget.TextView[@content-desc="tb-btn"]')
actions.tap(ele).perform()
Pinch
A pinch is where you use two fingers on the screen. For example, you can use a pinch to zoom in or out of the screen's content. If you move your fingers to the opposite of each other, you can zoom-in.
zoom_action = MultiAction(driver)
# Zoom
finger1.long_press(x=xx, y=yy).move_to(x=0, y=50).wait(200).release()
finger2.long_press(x=xx, y=yy).move_to(x=0, y=-50).wait(200).release()
zoom_action.add(finger1, finger2)
Long Press
With a long press (also known as press-and-hold), you can press an element or coordinate for a longer duration than a tap, with one or more fingers (or a stylus). For example, on iOS this can be used to show a context-sensitive menu.
Installing and Configuring Appium
To install and configure Appium, you'll need to follow these general steps:
Prerequisites
Make sure you have NodeJS installed on your machine. You can download it from the NodeJS website.
Install Appium
You can easily install Appium with NPM or Yarn.
Install Appium Dependencies
Depending on the mobile platform you want to run on, you might need to make sure you have the necessary dependencies installed.
For Android, make sure you have installed the Android SDK.
For iOS, make sure you have installed the XCode version that works with the iOS version you want to test on.
Run Appium
To run Appium simply run the appium command in your terminal. Appium will automatically listen on port 4723.
Examples using Touch Actions with Appium
Below are some examples on how to use Appium and Touch Actions, with Python. These examples will connect to the TestingBot device grid.
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.common.appiumby import AppiumBy
import time
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Pixel 3'
desired_caps['appPackage'] = 'com.testingbot.demo.app'
driver = webdriver.Remote("https://hub.testingbot.com/wd/hub", desired_caps)
deviceSize = driver.get_window_size()
screenWidth = deviceSize['width']
screenHeight = deviceSize['height']
startx = screenWidth*8/9
endx = screenWidth/9
starty = screenHeight/2
endy = screenHeight/2
startx2 = screenWidth/9
endx2 = screenWidth*8/9
starty2 = screenHeight/2
endy2 = screenHeight/2
actions = TouchAction(driver)
# Perform the action swiping from left to Right
actions.long_press(None,startx,starty).move_to(None,endx,endy).release().perform()
# Perform the action swiping from Right to Left
actions.long_press(None,startx2,starty2).move_to(None,endx2,endy2).release().perform()
driver.quit()
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.common.multi_action import MultiAction
from appium.webdriver.common.appiumby import AppiumBy
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Pixel 3'
desired_caps['appPackage'] = 'com.testingbot.demo.app'
driver = webdriver.Remote("https://hub.testingbot.com/wd/hub", desired_caps)
xx = driver.get_window_size()['width']/2
yy = driver.get_window_size()['height']/2
action1 = TouchAction(driver)
action2 = TouchAction(driver)
zoom_action = MultiAction(driver)
action1.long_press(x=xx, y=yy).move_to(x=0, y=50).wait(500).release()
action2.long_press(x=xx, y=yy).move_to(x=0, y=-50).wait(500).release()
zoom_action.add(action1, action2)
driver.quit()
Best practices when using Touch Actions with Appium
When using touch actions with Appium, it's important to follow best practices to ensure reliable and effective test automation. Below are some best practices that you can integrate in your tests:/p>
Use Touch Action Chaining
You can chain various touch actions in a sequence. This allows you to combine various gestures and create complex interactions, such as various swipe, drag and tap combinations.
Use Real-Device Testing
While emulators and simulators are useful for initial testing, consider performing touch action testing on physical devices as well. Physical devices may have subtle differences in touch behavior and performance compared to emulators/simulators.
Handle Different Screen Resolutions
Make sure you are testing your touch actions on devices with various screen resolutions. You need to make sure your automated tests work on different devices, with different resolutions.
Common problems when using Touch Actions with Appium
When using touch actions with Appium, you may encounter some common issues. Below are some examples:
Element Identification
Identifying the correct elements to perform touch actions on can be difficult. Make sure that you use appropriate locators (such as IDs, class names, or XPath) to accurately identify the elements you want to interact with.
Timing and Synchronization
Mobile apps can have varying response times, which means touch actions need to be timed correctly. Make sure you are using implicit or explicit waits to check if the element that you want to interact with is available to the automated test.
Calibration and Offset
Some devices might be using software that change the layout of some OS elements. Especially low end Android devices, or Android devices from specific manufacturers may use their own custom OS. This might introduce UI elements that do not appear on other devices.
If that is the case, it's important to make sure that if you use x and y coordinates, to take this into consideration.
Performance
Automated touch actions can sometimes impact the performance and stability of the app or the test environment. Long-running or repetitive touch actions may cause memory leaks, performance degradation, or crashes. Consider optimizing your test scripts and reducing unnecessary touch actions to maintain stability and efficiency.