Python+Appium automated testing (14)-yaml configuration Desired capabilities

I. Introduction

In the previous appium automated test examples, we wrote the data for constructing the driver instance object (that is, Desired Capabilities) in the business code, as follows:

# -*- coding:utf-8 -*-
# @author: 给你一页白纸
from appium import webdriver
desired_caps = {
    "platformName": "Android",
    "platformVersion": "10",
    "deviceName": "PCT_AL10",
    "appPackage": "com.ss.android.article.news",
    "appActivity": ".activity.MainActivity",
    "automationName": "UiAutomator2",
    "unicodeKeyboard": True,
    "resetKeyboard": True,
    "noReset": True,
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

Two, use the yaml file to configure the Capabilities example

The directory structure is as follows:

The capabilities data in the desired_caps.yml file under config is as follows ( click here for how to read and write yaml files in Python ):

platformName: Android
platformVersion: '10'
deviceName: PCT_AL10
appActivity: .activity.MainActivity
appPackage: com.ss.android.article.news
automationName: UiAutomator2
resetKeyboard: true
noReset: true
unicodeKeyboard: true
ip: 127.0.0.1
port: 4723

The function to construct the driver in the baseDriver.py file under common is as follows:

# -*- coding:utf-8 -*-
# @author: 给你一页白纸
import yaml, os
from appium import webdriver
# 项目根目录路径,即android-ui-autotest文件夹的路径
BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# capabilities配置文件desired_caps.py路径
DESIRED_CAPS_YAML_PATH = CONFIG_PATH + '/config/desired_caps'
def android_driver():
    # 从desired_caps.yaml读取driver配置数据
    stream = open(DESIRED_CAPS_YAML_PATH, 'r')
    data = yaml.load(stream, Loader=yaml.FullLoader)
    desired_caps = {}
    desired_caps['platformName'] = data['platformName']
    desired_caps['platformVersion'] = data['platformVersion']
    desired_caps['deviceName'] = data['deviceName']
    desired_caps['appPackage'] = data['appPackage']
    desired_caps['appActivity'] = data['appActivity']
    desired_caps['automationName'] = data['automationName']  
    desired_caps['unicodeKeyBoard'] = data['unicodeKeyBoard']
    desired_caps['resetKeyboard'] = data['resetKeyboard']
    desired_caps['noReset'] = data['noReset']
    driver = webdriver.Remote('http://' + str(data['ip']) + ':' + str(data['port']) + '/wd/hub', desired_caps)
    driver.implicitly_wait(8)
    return driver

Third, update the content of the yaml file

Sometimes we need to modify the configuration information in the yaml file through scripts. For example, if the test machine is replaced, the platformVersion and deviceName in capabilities have changed, and the script automatically obtains these two parameters of the new test machine and updates them to the desired_caps.yml file.

Update yaml file ideas:

1. First read the content of the yaml file that needs to be updated;

2. Replace the content that needs to be changed in the read content with new content;

3. Write the yaml file again.

The sample code is as follows:

# -*- coding:utf-8 -*-
# @author: 给你一页白纸
import yaml
def update_yaml():
    with open('./config/desired_caps', 'r',  encoding='utf-8') as f:
        content = yaml.load(f, Loader=yaml.FullLoader)
    content['platformVersion'] = 'newPlatformVersion'
    content['deviceName'] = 'newDeviceName'
    with open('./config/desired_caps', 'w', encoding='utf-8') as nf:
        yaml.dump(data=content, stream=nf, allow_unicode=True)

Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:

insert image description here

Software testing interview applet

The software test question bank maxed out by millions of people! ! ! Who is who knows! ! ! The most comprehensive quiz mini program on the whole network, you can use your mobile phone to do the quizzes, on the subway or on the bus, roll it up!

The following interview question sections are covered:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. web, app, interface automation, 7. performance testing, 8. programming basics, 9. hr interview questions, 10. open test questions, 11. security testing, 12. computer basics

These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can help you too!   

Guess you like

Origin blog.csdn.net/nhb687095/article/details/132407459