Software testing | Touch screen operation test automation

In the test work, we often need to perform gesture operations such as sliding, long pressing, and dragging on the App page (or mobile phone screen). AppiumDriver provides an auxiliary class TouchAction that simulates gesture operations. Through this class, gesture operations can be simulated on the mobile phone screen. .

1. Import TouchAction

The demo code for importing TouchAction is as follows (Python version and Java version).

Python version

from appium.webdriver.commoon.touch_action import TouchAction

java version

import io.appium.java_client.TouchAction

2. Common gesture operation methods

The operation methods of the commonly used simulated gestures provided by TouchAction are as follows:

press press;

release release;

move_to/moveTo moves;

tap click;

long_-press/longPress long press;

wait wait;

cancel cancel;

perform executes.

(1) press (press)

The press() method provided by TouchAction can simulate the pressing operation of an element or a certain coordinate point on the page. Usually, the release() method is used to realize the simulation operation of clicking on an element (including two actions of pressing and lifting).

When a click operation is performed on a control, the press() method can be used, and the usage is as follows (Python version and Java version).

Python version

press(WebElement el)

To perform a click operation at a point whose coordinates are (x, y), the usage of press() is as follows (Python and Java versions).

press(int x,int y)

java version

To perform a click operation at a point whose coordinates are (x, y), the usage of press() is as follows (Python version and Java version).

press(int x,int y)

(2) release release

The simulated release operation in the test work can be used in combination with press() and long_press(). release represents the action event after the press() and long_press() methods end. The code to perform the release operation on a tested control is as follows (Python version and Java version).

Python version

release(webElement el)

It is also possible to execute release after the previous operation, without adding any parameters, the code is as follows.

release()

java version

release()

(3) Mobile

With the control as the target, the test simulation moves from a point to a target point, the code is as follows (Python version and Java version).

Python version

move_to(WebElement el)

With (x,y) point as the target, the test simulation moves from one point to the target point, the code is as follows.

move_to(WebElement el,int x,int y)

java version

With (x,y) point as the target, the test simulation moves from one point to the target point, the code is as follows.

moveTo(WebElement el,int x,int y)

(4) tap (click)

The test simulates clicking on the center point of a certain control, the code is as follows (Python version and Java version).

Python version

tap(WebElement el)

Based on the upper left corner of the control el, move x units to the right along the x axis, and move down y units along the y axis. Click on this point, the code is as follows:

tap(WebElement el ,int x , int y)

Use the (x, y) coordinate point as the target to simulate the click operation, the code is as follows:

tap(int x,int y)

java version

To implement a click simulation operation on a certain point on the page, the code is as follows:

tap(int x,int y)

(5) Long press

Test the operation of simulating a long press on a certain control, the code is as follows (Python version and Java version).

Python version

long_press(WebElement el)

In the test, simulate the long press operation with the (x, y) point as the target, the code is as follows:

long_press(WebElement el,int x_len,int y_len)

java version

Only simulate the long press operation at the coordinate point, the code is as follows:

longPress(int x,int y)

(6) wait

The simulated wait in the test, in milliseconds. It is possible to pause briefly for a few seconds while simulating an operational event before continuing. The code is as follows (Python version and Java version).

Python version

wait (long timeout)

java version

wait (long timeout)

(7) cancel cancel

In the test, the events in the event chain are simulated and canceled, and the code is as follows (Python version and Java version).

Python version

cancel()

java version

cancel

(8) execute perform

The test simulates the execution time in the event chain. Generally, the perform method is called at the end of the test script to sequentially execute the actions in the event chain. The code is as follows (Python version and Java version).

Python version

preform()

java version

perform()

3. Case

The download address of the App for testing is as follows:

' https://GitHub site/appium/appium/tree/master/sample-code/apps'

Steps:

1) Open the test application;

2) Swipe from the element "Views" to the element "Accessibility".

The demo code is as follows (Python version and Java version).

(1) Python demo code

#!/usr/bin/env python 
# -*-coding:utf-8 -*-
#测试文件 test_touchaction.py
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
class TestTouchAction():
   def setup(self):
     capa['platformName'] = 'Android'
     caps['platformVersion'] = '6.0'
     caps['deviceName'] = '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 reardown(self):
       self.driver.quit()
       
   def test_touchaction_unlock(self):
       #点击 View
       ell = self.driver.find_element_by_accessibility_id(
          "Views")
       #点击 Accessibliity
       el2 = self.driver.find_element_by_accessibilty_id(
           "Accessibility")
       # TouchAction 滑动操作
       action = TouchAction(self.driver)
       action.press(ell).wait(100).move_to\
       (el2).wait(100).release().perform()

(2) Java demo code

public class TouchActionTest(
     static AppiumDriver driver;
     
     @BeforeAll
     public static void beforeAll() throws MalformedURLException{
         DesiredCapabilities caps = new DesiredCapabilities();
         caps.setCapability("deviceName","emulator--5554");
         caps.setCapability("platformName","Android");
         caps.setCapability("appPackage","io.appium.android.apis");
         caps.setCapability("appActivity","io.appium.android.apis.\
         ApiDemos");
         URL appiumServer = new URL("http://127.0.0.1:4723/wb/hub");
         driver = new AndroidDriver(appiumServer,caps);
         driver.manage().tiemouts().implicityWait 10,/
         TimeUnit.SECONDS);
     }
         @Test
         void test(){
         //创建TouchAction对象
         TouchAction action = new TouchAction<>(driver);
         //TouchAction滑动操作
         action.press(PointOption.point((int)(width * 0.5),\
         (int) (height * 0.8 ))).waitAction(WaitOptions.\
         waitOptions(Duration.ofSecond(2))).moveTo(\
         PointOption.point(int) (width * 0.5),\
         (int) (height * 0.2))).release().perform();
         }
      )

The above two pieces of test code achieve the same operation, create a TouchAction object, call the press() method inside to realize the click on the element; use the wait() method to add waiting between times; use move_to()/moveTo( ) method to complete the movement operation of the gesture; then call the release() method to complete the lifting of the gesture operation; finally call the perform() method to perform the sequential execution of the time chain added to the TouchAction.

Finally:  In order to give back to the die-hard fans, I have compiled a complete software testing video learning tutorial for you. If you need it, you can get it for free 【保证100%免费】

How to obtain the full set of materials: Click the small card below to get it yourself

 

Guess you like

Origin blog.csdn.net/weixin_57794111/article/details/131637136