杰瑞科技汇

TouchAction在Python中如何使用?

Of course! Let's dive deep into TouchAction in Python, specifically within the context of the Appium framework.

TouchAction在Python中如何使用?-图1
(图片来源网络,侵删)

What is TouchAction?

TouchAction is a class in the Appium Python Client library that allows you to simulate complex user gestures on a mobile device. Instead of just clicking or typing, you can chain together a series of touch actions to create more advanced interactions, such as:

  • Tapping on a specific element or coordinate.
  • Swiping in any direction.
  • Scrolling through a list or view.
  • Long-pressing (for context menus, drag-and-drop, etc.).
  • Dragging and dropping an element.
  • Flicking (a quick swipe with velocity).

Essentially, it's the primary tool for performing multi-touch and complex gesture-based automation.


The Core Concept: The Action Chain

The magic of TouchAction lies in its chainable methods. You don't perform each action individually. Instead, you build a sequence of actions and then "release" them to execute the entire gesture in one go.

The typical workflow is:

TouchAction在Python中如何使用?-图2
(图片来源网络,侵删)
  1. Instantiate TouchAction(driver).
  2. Chain methods to define the sequence of actions (.press(), .moveTo(), .wait(), .release(), etc.).
  3. Perform the entire chain with .perform().

Key Methods of TouchAction

Here are the most common methods you'll use:

Method Description Example
.press() Starts the touch action by pressing down at a specific element or coordinate. .press(x=100, y=200) or .press(element)
.moveTo() Moves the touch from the current position to a new element or coordinate. .moveTo(element=el) or .moveTo(x=150, y=250)
.release() Lifts the finger, ending the touch action. This is required to complete the chain. .release()
.wait() Pauses the action for a specified duration in milliseconds. Useful for long presses or controlling swipe speed. .wait(duration=2000) (2 seconds)
.tap() A shortcut for a quick press and release. Good for simple taps. .tap(element=el, count=2) (double tap)
.perform() Executes the entire chain of actions that have been built. .perform()

Practical Examples

Let's assume you have an Appium driver instance named driver.

Simple Tap

Tapping is the most common action. While driver.find_element(...).click() is simpler, TouchAction is needed for more control.

from appium.webdriver.common.touch_action import TouchAction
from appium import webdriver
# Assume 'driver' is your initialized Appium driver
# driver = webdriver.Remote('http://localhost:4723/wd/hub', capabilities)
# Find the element to tap on
element_to_tap = driver.find_element("id", "com.example.app:id/login_button")
# Create a TouchAction object
actions = TouchAction(driver)
# Chain the actions: press on the element, then release
actions.press(element_to_tap).release()
# Perform the gesture
actions.perform()

Long Press (e.g., for a context menu)

A long press is useful for revealing options or triggering special behaviors.

from appium.webdriver.common.touch_action import TouchAction
# Find the element to long-press
element_to_long_press = driver.find_element("accessibility id", "Profile Picture")
actions = TouchAction(driver)
# Chain: press, wait for 2 seconds (2000ms), then release
actions.press(element_to_long_press).wait(2000).release()
actions.perform()

Swipe (e.g., navigating through a carousel)

Swiping is a fundamental gesture. We'll press, move our finger, and then release.

from appium.webdriver.common.touch_action import TouchAction
# Let's swipe from right to left on the screen
# We'll use screen coordinates for this example
start_x = 800
start_y = 400
end_x = 200  # Move to the left
end_y = 400
actions = TouchAction(driver)
# Chain: press at start point, move to end point, then release
actions.press(x=start_x, y=start_y).wait(500).moveTo(x=end_x, y=end_y).wait(500).release()
actions.perform()

Drag and Drop

This is a great example of chaining multiple actions together. We'll press on one element, move it over another, and release.

from appium.webdriver.common.touch_action import TouchAction
# Find the source and destination elements
source_element = driver.find_element("id", "com.example.app:id/drag_item")
destination_element = driver.find_element("id", "com.example.app:id/drop_zone")
actions = TouchAction(driver)
# Chain the entire drag and drop sequence
# 1. Press on the source element
# 2. Move to the destination element
# 3. Release
actions.press(source_element).moveTo(destination_element).release()
actions.perform()

Modern Alternative: W3C Actions API

Appium supports the newer W3C Actions API, which is the standard for browser and mobile automation. It's more powerful and flexible than the legacy TouchAction API. Many new projects are encouraged to use it.

Why consider the W3C Actions API?

  • More powerful: Can handle multiple pointers (fingers) simultaneously.
  • More explicit: The syntax clearly defines actions like "pointer down," "move," and "pointer up."
  • The future: It's the modern standard.

Example: Swipe with W3C Actions API

from appium.webdriver.common.actions import pointer_actions
from appium.webdriver.common.actions.action_builder import ActionBuilder
from appium.webdriver.common.actions.pointer_input import PointerInput
# Use PointerInput with TOUCH as the pointer type
finger = PointerInput(pointer_actions.POINTER_TOUCH, "finger")
# Create an action builder
actions = ActionBuilder(driver, pointer_input=[finger])
# Add actions to the sequence
# 1. Move finger to start position and press down
actions.add_actions([
    finger.create_pointer_move(x=800, y=400),
    finger.create_pointer_down(pointer_actions.TOUCH_POINTER_BUTTON)
])
# 2. Move finger to end position
actions.add_actions([
    finger.create_pointer_move(x=200, y=400)
])
# 3. Lift finger up
actions.add_actions([
    finger.create_pointer_up(pointer_actions.TOUCH_POINTER_BUTTON)
])
# Perform the gesture
actions.perform()

Summary: TouchAction vs. W3C Actions API

Feature TouchAction W3C Actions API
Simplicity Easier for simple, single-finger gestures. More verbose but more explicit.
Power Good for most single-finger actions. More powerful, supports multi-touch and complex scenarios.
Standard Legacy Appium-specific API. W3C Standard, the future of automation.
Recommendation Perfect for beginners and most common use cases. Recommended for advanced automation and new projects.

Conclusion

For anyone starting with Appium in Python, TouchAction is an essential tool to learn. It provides a straightforward and readable way to build complex gestures. While the W3C Actions API is the more modern and powerful alternative, understanding TouchAction will give you a solid foundation for mobile automation. Start with TouchAction for your basic swipes, taps, and long presses, and then explore the W3C API as your needs become more complex.

分享:
扫描分享到社交APP
上一篇
下一篇