Appium Analog Gesture Advanced Operations

Keywords: Android Mobile

    TouchAction is an auxiliary class of AppiumDriver that focuses on gesture operations such as sliding, long press, dragging, and so on.
    The principle is to put a series of actions in a chain and send them to the server. After the server accepts the chain, it parses the actions and executes them one by one.
  • 1. Pre-code
    from appium import webdriver
    # server startup parameters
    desired_caps = {}
    # Device Information
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '5.1'
    desired_caps['deviceName'] = '192.168.56.101:5555'
    # app information
    desired_caps['appPackage'] = 'com.android.settings'
    desired_caps['appActivity'] = '.Settings'

    # Declare our driver object
    driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
  • ⚠All gestures need to be executed in order to run.

  • 2. Finger tapping

      Simulate finger tapping on screen
      Method: tap(element=None, x=None, y=None)
      Method: perform() #Send command to server to perform operation
      Parameters:
          1.element: the element to which it is positioned
          2.x: The X-axis coordinates of an element are usually used relative to the coordinates of the upper left corner of the element
          3.y: The Y-axis coordinates of elements are usually used
    
      Business Scenario:
          1. Enter Settings
          2. Click WLAN Options
    
      Code implementation:
          # Tap screen by element positioning
          el = driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")
          TouchAction(driver).tap(el).perform()
    
          # Tap the screen in coordinates, WLAN coordinates: x=155,y=250
          # TouchAction(driver).tap(x=155,y=250).perform()
    
  • 3. finger press operation

      Simulate finger pressing on the screen to leave.
    
      Method: press(el=None, x=None, y=None)
      Method: release() #End action, finger off screen
      Parameters:
          1.element: the element to which it is positioned
          2.x: The X-axis coordinates of elements are usually used
          3.y: The Y-axis coordinates of elements are usually used
    
      Business Scenario:
          1. Enter Settings
          2. Click WLAN Options
    
      Code implementation:
          # Press screen by element positioning
          el = driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")
          TouchAction(driver).press(el).release().perform()
    
          # Press the screen in coordinates, WLAN coordinates: x=155,y=250
          # TouchAction(driver).tap(x=155,y=250).release().perform()
    
  • 4. Wait for operation

      Method: wait(ms=0)
      Parameters:
          ms: milliseconds paused
    
      Business Scenario:
          1. Enter Settings
          2. Click WLAN Options
          3. Long press WiredSSID option for 5 seconds
    
  • 5. finger long press operation

      Simulate your mobile phone pressing the screen for a while and leaving as you like.
    
      Method: long_press(el=None, x=None, y=None, duration=1000)
      Parameters:
          1.element: the element to which it is positioned
          2.x: The X-axis coordinates of elements are usually used
          3.y: The Y-axis coordinates of elements are usually used
          4.duration: duration, default 1000ms
    
      Business Scenario:
          1. Enter Settings
          2. Click WLAN Options
          3. Long press WiredSSID option for 5 seconds
    
      Code implementation:
          # Click WLAN
          driver.find_element_by_xpath("//*[contains(@text,'WLAN')]").click()
          # Locate to WiredSSID
          el =driver.find_element_by_id("android:id/title")
          # Long by element by element positioning
          TouchAction(driver).long_press(el,duration=5000).release().perform()
    
          # Long by element by coordinate, WiredSSID coordinate: x=770,y=667
          # Add Wait (Long Press) / No Wait (No Long Press Effect)
          # TouchAction(driver).long_press(x=770,y=667).perform()
    
  • 6. Finger movement
      Simulate mobile phone sliding
      Method: move_to(el=None, x=None, y=None)
      Parameters:
          1.el: Located element
          2.x: X-axis offset from previous element
          3.y: Y-axis offset from previous element
    
      Business Scenario 1:
          1. Enter Settings
          2. Slide the screen up
    
    Code implementation:
        # Locate to Storage
        el = driver.find_element_by_xpath("//*[contains(@text,'storage')] ")
        # Locate More
        el1 = driver.find_element_by_xpath("//*[contains(@text,'more')] ")
        # Elemental sliding
        TouchAction(driver).press(el).move_to(el1).release().perform()
        # Slide up in coordinate mode
        # TouchAction(driver).press(x=240,y=1000).move_to(x=0,y=-400).release().perform() 
        # ⚠_press().move_to() actually uses the TouchAction method, which requires relative coordinates.
        # TouchAction(driver).press(x=240,y=600).wait(100).move_to(x=240,y=100).release().perform()
        # ⚠_press().wait().move_to() actually calls the swip method, which drops down and feels like a bug. You can query the swip in the log, which is not recommended.
    Business Scenario 2:
        1. Enter Settings
        2. Slide the screen up to the visible Security option
        3. Enter Security
        4. Click Screen Lock
        5. Click on the pattern
        6. Draw patterns
    Code implementation:
        # Locate to WLAN
        el1 = driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")
        # Locate to Storage
        el2 = driver.find_element_by_xpath("//*[contains(@text,'storage')] ")
        # Slide to WLAN on Storage
        driver.drag_and_drop(el2,el1)
        # Locate User
        el3 = driver.find_element_by_xpath("//*[contains(@text,'user')] ")
        # Note that this time using the drag_and_drop method, the incoming Storage Location still uses its original location on the screen, so it is up to the user to slide from the storage, otherwise the Storage Location needs to be re-created
        # Slide User Location on Storage
        driver.drag_and_drop(el2,el3)
        # Click the safety button
        driver.find_element_by_xpath("//*[contains(@text,'secure')]').click()
        # Click the Screen Lock Mode button
        driver.find_element_by_xpath("//*[contains(@text,'screen lock')]'). click()
        # Click the pattern button
        driver.find_element_by_xpath("//*[contains(@text,'pattern')]').click()
        # Draw the pattern in four coordinates 1:(244,967) 2:(723,967) 3:(723,1442) 4:(244,1916)
        TouchAction(driver).press(x=244,y=967).wait(100).move_to(x=479,y=0).wait(100)\
            .move_to(x=0,y=475).wait(100).move_to(x=-479,y=474).release().perform()

Effect:

203 original articles published, 76 praised, 80,000 visits+
Private letter follow

Posted by kontesto on Sat, 01 Feb 2020 21:18:43 -0800