How appium +pycharm controls App page sliding

Posted by bbristow on Tue, 23 Nov 2021 13:58:11 +0100

When we operate the app, we will slide the page. How can we implement this process in python?

 

How to slide

We all know very well how we manually slide on the app, and then automation is only implemented by imitating the manual method. We analyze it through a graph

 

 

When sliding from top to bottom, we usually press the coordinate (X1, Y1), and then slide down to the coordinate (X2, Y2)

When sliding from bottom to top, we will press the coordinate (X2, Y2) and slide up to the coordinate (X1, Y1)

Of course, the idea of sliding from left to right is the same as sliding from right to left.

swipe

swipe is a method in appium module, which supports left-right sliding and click simulation functions

 def swipe(self, start_x, start_y, end_x, end_y, duration=None):
     # Slide from one point to another for an optional duration
        """Swipe from one point to another point, for an optional duration.

        :Args:
         - start_x - x-coordinate at which to start   Beginning X axis
         - start_y - y-coordinate at which to start  Beginning Y axis
         - end_x - x-coordinate at which to stop    Ending X axis
         - end_y - y-coordinate at which to stop    Ending Y axis
         - duration - (optional) time to take the swipe, in ms.   Duration in milliseconds

        :Usage:
            driver.swipe(100, 100, 100, 400)
        """

We know from the above figure and the source code of swipe that the most important parameter of sliding is coordinates. How can we query the coordinates of mobile phones?

Query coordinates

Open pointer position

We can enter the mobile developer mode, and then open the pointer position, so that we can see the coordinates where we click.

  We can also use python scripts to help us find coordinates

get_window_size

def get_window_size(self, windowHandle='current'):
        """
        Gets the width and height of the current window.
        # Gets the length and width of the screen
        :Usage:
            driver.get_window_size()
        ""

  Because the coordinates of each mobile phone may be different, here we can first obtain the length and width of the mobile phone screen, and then calculate the coordinate position to slide again

# coding:utf-8
from appium import webdriver
import time
desired_caps = {
                        'platformName': 'Android',  # Test version
                        'deviceName': 'emulator-5554',   # Device name
                        'platformVersion': '5.1.1', # System version
                        'appPackage': 'com.yipiao', #Package name of apk
                       'appActivity': '.activity.LaunchActivity', # launcherActivity of apk
                        }
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
# Gets the width of the device
x=driver.get_window_size()['width']
# Gets the length of the device
y=driver.get_window_size()['height']
print(x)
print(y)

App slide

We use Zhixing train ticket APP to slide left and right under actual operation. Because the pages of each mobile phone are different, we can calculate an approximate value for matching

Slide left

# coding:utf-8
from appium import webdriver
import time
desired_caps = {
                        'platformName': 'Android',  # Test version
                        'deviceName': 'emulator-5554',   # Device name
                        'platformVersion': '5.1.1', # System version
                        'appPackage': 'com.yipiao', #Package name of apk
                       'appActivity': '.activity.LaunchActivity', # launcherActivity of apk
                        "noReset": True,  # Do not empty data
                        }
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(6)
driver.find_element_by_xpath('//*[@ text = "next time"]). click()
l = driver.get_window_size()
x1 = l['width'] * 0.75
y1 = l['height'] * 0.5
x2 = l['width'] * 0.25
# Slide left
driver.swipe(x1,y1,x2,y1,500)
print('It's already sliding to the left')

 

If it's quiet here, we won't demonstrate it one by one. Little friends can try it by themselves

Packaging sliding method

We can encapsulate the sliding methods one by one, so that we can call them directly when needed in the process of app automation testing.

def swipeUp(driver, t=500, n=1):
    '''Swipe the screen up'''
    l = driver.get_window_size()
    x1 = l['width'] * 0.5     
    y1 = l['height'] * 0.75   
    y2 = l['height'] * 0.25   
    for i in range(n):
        driver.swipe(x1, y1, x1, y2, t)

def swipeDown(driver, t=500, n=1):
    '''Slide the screen down'''
    l = driver.get_window_size()
    x1 = l['width'] * 0.5        
    y1 = l['height'] * 0.25       
    y2 = l['height'] * 0.75       
    for i in range(n):
        driver.swipe(x1, y1, x1, y2,t)

def swipLeft(driver, t=500, n=1):
    '''Swipe the screen to the left'''
    l = driver.get_window_size()
    x1 = l['width'] * 0.75
    y1 = l['height'] * 0.5
    x2 = l['width'] * 0.25
    for i in range(n):
        driver.swipe(x1, y1, x2, y1, t)

def swipRight(driver, t=500, n=1):
    '''Swipe the screen to the right'''
    l = driver.get_window_size()
    x1 = l['width'] * 0.25
    y1 = l['height'] * 0.5
    x2 = l['width'] * 0.75
    for i in range(n):
        driver.swipe(x1, y1, x2, y1, t)

Topics: Android Appium