appium -- positioning package

Automated testing is simple, fast and easy to maintain. The most important part of automatic maintenance is element positioning.


Before we write code, we should make clear our thinking, so that when we write code again, we won't know how to start.

1. Manage elements and positioning methods through yaml (or other tools)

2. Encapsulate read yaml (or other tool) functions

3. By reading yaml information, the location information and elements are separated and filled in the corresponding location method

PS: here is Taobao login practice.

Writing elements and positioning methods

Here we can fill the page elements and positioning methods in the same yaml through yaml data.

Here, we use ">" to separate the location method (by) and element content, which is convenient for later extraction.

# element. yaml


Sign in username: id> password: android>resourceId("") login_button: className>android.widget.Button

Read encapsulated yaml functions

Here, a function is encapsulated to read the contents of yaml file.



yaml import os class GetYaml(): def __init__(self,file_path): # Judge whether the file exists if os.path.exists(file_path): self.file_path = file_path else: print('Can't find%s File path'%file_path) = self.read_yaml() def read_yaml(self): with open(self.file_path,'r',encoding='utf-8')as f: p = return p def get_data(self,key=None): result = yaml.load(,Loader=yaml.FullLoader) if key == None: return result else: return result.get(key) if __name__ == '__main__': read_yaml = GetYaml('element.yaml') xx = read_yaml.get_data('login') print(xx)

Packaging positioning method

Well, here comes the most important packaging positioning.

Small idea:

1. As mentioned earlier, if elements and positioning methods are written together, they should be separated first;

2. It needs to judge the location method, and then put the elements into the corresponding location method;

3. In order to ensure the normal operation of the code, quiet here directly uses the method of display waiting.

# coding:utf-8

from common.read_yaml import GetYaml from import WebDriverWait from import expected_conditions as EC class GetByLocal: def __init__(self,driver): self.driver = driver def get_element(self,path,key): # Get to yaml Address and reader yaml_data = GetYaml(path) local = yaml_data.get_data(key) if local != None: by = local.split('>')[0] local_by = local.split('>')[1] try: if by == 'id': element = WebDriverWait(self.driver, 10, 0.5).until(lambda x: x.find_element_by_id(local_by)) elif by == 'className': element = WebDriverWait(self.driver, 10, 0.5).until(lambda x: x.find_element_by_class_name(local_by)) elif by == 'xpath': element = WebDriverWait(self.driver, 10, 0.5).until(lambda x: x.find_element_by_xptah(local_by)) elif by == 'android': element = WebDriverWait(self.driver, 10, 0.5).until(lambda x: x.find_element_by_android_uiautomator(local_by)) else: loc = (by,local_by) # Yuanzu element = WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located(loc)) return element except: return None else: return None

Extract page information

Here, we use silence to encapsulate the elements of a page into a file, which is convenient for us to use and call

Note: quiet here, only the user name, password, and login button are extracted. You can also write others together (forget password, register, and display toast method)

# coding:utf-8

from common.get_by_local import GetByLocal

class LoginPage:
    def __init__(self,driver):
        self.get_by_local = GetByLocal(driver)
    def get_username_element(self,path):
        return self.get_by_local.get_element(path,'username')

    def get_password_element(self,path):
        return  self.get_by_local.get_element(path,'password')

    def get_login_button_element(self,path):
        return self.get_by_local.get_element(path,'login_button')

Write use cases

The previous preparations have been completed, and here we start to write test cases. (there is no unit test method here, just a simple process, focusing on the method)

from appium import webdriver
from pages.login_page import LoginPage
import time
import os
path  =os.path.dirname(os.getcwd())
# Get to yaml File path
yaml_path = os.path.join(os.path.join(path,'config'),'element.yaml')
class BaseDriver:
    def android_driver(self):
        desired_caps = {
                         'platformName': 'Android',  # Test version
                         'deviceName': 'emulator-5554',   # Device name
                         'platformVersion': '5.1.1', # System version
                        "appPackage": "",   # app Package name
                        "appActivity": "",   # start-up launch Activity
                        "noReset": True,  # Do not empty data
                        "unicodeKeyboard": True,    # use Unicode Encoding method send string
                        "resetKeyboard": True,      # The keyboard is hidden
        driver = webdriver.Remote('', desired_caps)
        return driver
    def login(self,driver):
            page =LoginPage(driver)
            # Get user name elements and input
            use = page.get_username_element(yaml_path)
            # Get password elements and input
            pas = page.get_password_element(yaml_path)
            # Click to log in
            button = page.get_login_button_element(yaml_path)

if __name__ == '__main__':
    x = BaseDriver()
    xx = x.android_driver()


Quiet through small examples to complete the encapsulation of elements and positioning methods, later our automation code is easy to maintain, positioning elements change, we only need to change in yaml file. Reduced maintenance costs.

The packaging method is ten million, which is convenient and which is only for reference.


After reading the quiet blog, if it helps you, you can pay attention to it in the lower right corner. If you don't understand it, you can leave a message below. Continuous update in progress.

Keywords: Android Lambda encoding Selenium

Added by NEWDAY on Sat, 16 May 2020 08:18:03 +0300