appium -- positioning package

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

thinking

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

login:

Sign in username: id>com.taobao.taobao:id/aliuser_login_mobile_et password: android>resourceId("com.taobao.taobao:id/aliuser_register_sms_code_et") login_button: className>android.widget.Button

Read encapsulated yaml functions

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

# read_yaml.py

import

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.data = self.read_yaml() def read_yaml(self): with open(self.file_path,'r',encoding='utf-8')as f: p = f.read() return p def get_data(self,key=None): result = yaml.load(self.data,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
# get_by_local.py

from common.read_yaml import GetYaml from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support 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)

# login_page.py
# 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": "com.taobao.taobao",   # app Package name
                        "appActivity": "com.ali.user.mobile.login.ui.UserLoginActivity",   # 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('http://127.0.0.1:4723/wd/hub', desired_caps)
        return driver
    def login(self,driver):
            page =LoginPage(driver)
            time.sleep(8)
            # Get user name elements and input
            use = page.get_username_element(yaml_path)
            use.send_keys('1111')
            # Get password elements and input
            pas = page.get_password_element(yaml_path)
            pas.send_keys('22222')
            # Click to log in
            button = page.get_login_button_element(yaml_path)
            button.click()

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

 

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