Monday 24 May 2021

Python Appium - How to automate gestures - Tap, long press and swipes/scroll

So above gestures are the part of TouchAction class of appium. So let us go in details of this class

TouchActions:
TouchAction objects contains chain of actions. TouchAction class have only one method perform and we should always end our touchAction sequence with .perform() method.

MultiTouch:
MultiTouch object are collection of TouchActions. MultiTouch have add() and perform() methods.

So let us see a real time example.

tap(): You need to pass x and y co-ordinates where you want to tap.
You can find the tap co ordinates from appium session. Give the desired capabilities and it will launch the session screen for you.

On top of screen there is an option swipe by co ordinates. Place the mouse over the item for which you want to tap. At top left corner you could see x and y coordinate values.




And we have the code which we implemented for tap and swipe up and down on Settings app
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
import time

def __init__(self, driver):
self.driver = driver

def test_tap_example():
desired_caps = {
'platformName': 'android',
'udid': 'emulator-5554',
'deviceName': 'Pixel_3_API_24',
'platformVersion': '7.0',
'appPackage': 'com.android.settings',
'appActivity': 'com.android.settings.Settings'
}
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)

driver.find_element_by_id("android:id/title").get_attribute("text")

# Tap example
# Tap on Notification option given under settings
user_actions = TouchAction(driver)
user_actions.tap(x=253, y=1107).perform()
time.sleep(5)
driver.back()

#Swipe/Scroll up and down example
time.sleep(5)
user_actions.press(x=434, y=872).move_to(x=437, y=656).release().perform()
time.sleep(5)
user_actions.press(x=468, y=1165).move_to(x=465, y=1401).release().perform()
time.sleep(5)

Let us see the implementation for long press on dialer app



So here you can record the actions for swipe and then the co ordinated appear . You can select the programming language as well whatever is required by you.

from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
import time

def __init__(self, driver):
self.driver = driver


def test_long_press_example():
desired_caps = {
'platformName': 'android',
'udid': 'emulator-5554',
'deviceName': 'Pixel_3_API_24',
'platformVersion': '7.0',
'appPackage': 'com.android.dialer',
'appActivity': 'com.android.dialer.DialtactsActivity'
}
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
# Long press for sending the number
user_actions = TouchAction(driver)
user_actions.tap(x=543, y=1890).perform()
time.sleep(5)

number = driver.find_element_by_id("com.android.dialer:id/digits")
number.send_keys("810000000")
user_actions.long_press(number)
time.sleep(5)


1 comment:

Feature Posts

Python Appium - Step by step procedure for capturing screenshot

 Why To capture screenshot? It is as important as your logs. If there is any failure for any test scenario, we can provide screenshot for th...