This article is excerpted from the internal textbook of Hogwarts Testing Institute
At work, we often need to perform gesture operations on the application page, such as sliding, long pressing, dragging, etc. AppiumDriver provides us with an auxiliary class to simulate gesture operations
TouchAction, you can use it to gesture on the mobile phone screen.
See the link for specific usage: https://ceshiren.com/t/topic/3275
Import TouchAction
from appium.webdriver.common.touch_action import TouchAction
Common gesture operations
The common gesture operations provided by TouchAction are as follows:
- Press press
- release
- move_to move
- tap Click
- Long press
- Wait wait
- Cancel cancel
- perform execution
press
**
**
The press() method provided by TouchAction can press elements or coordinates. Usually combined with release()
Method to click on an element (including pressing and lifting).
Perform press operation on a control. The usage is as follows:
press(WebElement el)
press the point with coordinates (x,y) as follows:
*
press(int x, int y)
release
**
**
Release operation can be used in combination with other events. Represents an end sign of the series of actions. Perform a release operation on a control. The usage is as follows:
release(WebElement el)
You can also execute release after the last operation without adding any parameters. The usage is as follows:
*
release()
move_to
**
**
Take the control as the target and move from a point to the target. The usage is as follows:
move_to(WebElement el)
Take point (x,y) as the target and move from one point to the target. The usage is as follows:
*
move_to(WebElement el, int x, int y)
tap
**
**
Click the center point of a control. The usage is as follows:
tap(WebElement el)
Click with the (x,y) coordinate point as the target. The usage is as follows:
*
tap(int x, int y)
Based on the upper left corner of the control el, move x units right along the X axis and y units down along the y axis. Click on this point. The usage is as follows:
*
tap(WebElement el, int x, int y)
longpress
**
**
Long press a control. The usage is as follows:
long_press(WebElement el)
Take (x,y) as the target to realize long press. The usage is as follows:
*
long_press(int x, int y)
Press and hold the x coordinate in the upper left corner of the control offset by x units and the y coordinate on the left offset by y units. The usage is as follows:
long_press(WebElement el, int x, int y)
wait
**
**
Wait in milliseconds. You can pause for a few seconds during the operation event before continuing the operation. The usage is as follows:
wait(long timeout)
cancel
**
**
You can cancel the execution of events in the event chain. The usage is as follows:
cancel()
perform
**
**
To execute the events in the event chain, this method is usually called at the end to execute the actions in the event chain in sequence. The usage is as follows:
perform()
case
Open the test application, slide from the element "Views" text to the "Accessibility" element, and create a test file
test_touchaction.py, the code is as follows:
Test app official download address: https://github.com/appium/appium/tree/master/sample-code/apps
#!/usr/bin/env python# -*- coding: utf-8 -*-from appium import webdriverfrom appium.webdriver.common.touch_action import TouchAction class TestTouchAction(): def setup(self): caps = {} caps['platformName'] = 'Android' caps['platformVersion'] = '6.0' caps['deviceName'] = 'emulator-5554' caps['appPackage'] = 'io.appium.android.apis' caps['appActivity'] = 'io.appium.android.apis.ApiDemos' self.driver = webdriver.Remote(\ "http://127.0.0.1:4723/wd/hub", caps) self.driver.implicitly_wait(5) def teardown(self): self.driver.quit() def test_touchaction_unlock(self): # click Views el1 = self.driver.find_element_by_accessibility_id( "Views") # click Accessibility el2 = self.driver.find_element_by_accessibility_id( "Accessibility") # TouchAction slide action = TouchAction (self. Driver) action. Press (el1). Wait (100). Move_ to\ (el2).wait(100).release().perform()
As can be seen from the above code, first locate the two elements el1 and el2 as the start element and end element in the sliding process. After finding the start element and end element of our sliding, create one
TouchAction object, call the press() method inside to click the starting point element, use the wait() method to add a wait between events, and use move_to( )
The method completes the movement of gestures, then calls the release() method to complete the gesture lifting, and finally calls the perform() method to add to the TouchAction.
The event chain in is executed sequentially.
**Recommended Learning**
The content has been comprehensively upgraded, with 4-month 20 + project actual combat intensive training, and Senior Test architects and open source project authors personally teach BAT's leading-edge best practices,
Take you one-stop to master the core skills necessary for test and development (benchmarking Ali P6 +, annual salary 50W +)! Directly promote the test manager of BAT famous enterprises, and generally increase the salary by 50% +!
⬇️ Click "read the original text" to improve the core competitiveness of the test!