I was using the “press” command but found that the “TouchAction” is deprecated in Appium 2.x and doesn’t work anymore! I want to hold touch mode on an element for a few seconds. How can I do this in Appium 2.x!?
You should use W3C supported
action. There are two ways to achieve.
Assume you want to Press and Hold one of the phone contacts to open the Delete/Edit options, we can use following working examples:
Press and Hold using W3C Actions API
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.options.android import UiAutomator2Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.pointer_input import PointerInput
from selenium.webdriver.common.actions import interaction
desired_caps = {
"appium:appPackage": "com.android.contacts",
"appium:appActivity": ".activities.PeopleActivity",
"platformName": "Android",
"appium:automationName": "UiAutomator2"
}
appium_options = UiAutomator2Options().load_capabilities(desired_caps)
driver = webdriver.Remote(appium_server, options=appium_options)
# Find all contacts in the list
contacts = driver.find_elements(by=AppiumBy.ID, value="com.android.contacts:id/cliv_name_textview")
# Create an instance from ActionChains class
actions = ActionChains(driver)
# Create a "touch" type of pointer input. By default it is "mouse"
touch_input = PointerInput(interaction.POINTER_TOUCH, 'touch')
# Override pointer action as 'touch'
actions.w3c_actions = ActionBuilder(self, mouse=touch_input)
# Press and Hold using W3C actions on first contact
actions.w3c_actions.pointer_action.click_and_hold(contacts[0])
actions.perform()
Press and Hold using W3C Mobile Gestures Commands
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.options.android import UiAutomator2Options
desired_caps = {
"appium:appPackage": "com.android.contacts",
"appium:appActivity": ".activities.PeopleActivity",
"platformName": "Android",
"appium:automationName": "UiAutomator2"
}
appium_options = UiAutomator2Options().load_capabilities(desired_caps)
driver = webdriver.Remote(appium_server, options=appium_options)
contacts = driver.find_elements(by=AppiumBy.ID, value="com.android.contacts:id/cliv_name_textview")
# Get element coordinates (position in the page)
element_coord = contacts[0].location
# Long press using W3C Mobile Gestures Commands
driver.execute_script('mobile: longClickGesture', {'x': element_coord['x'], 'y': element_coord['y'], 'duration': 1000})
For more information you can read my post on medium about it: https://monfared.medium.com/gestures-in-appium-part3-press-and-hold-long-press-21a0d2727c91