1. 程式人生 > >框架綜合實踐(1)-driver的封裝(capability)

框架綜合實踐(1)-driver的封裝(capability)

讀取yaml配置檔案:

有兩種方式,一個是file open,一個是with open

方式1

file = open('../config/WiFibanlv_caps.yaml', 'r')

data = yaml.load(file)

file.close() #必須使用close,否則檔案會出現佔用情況

方式2:

with open('../config/WiFibanlv_caps.yaml', 'r', encoding='utf-8') as file:

    data = yaml.load(file)

安裝包的相對路徑使用方法:

步驟1:先匯入對應的模組import os

步驟2:找到當前檔案的目錄,使用方法os.path.driname(__file__)

步驟3:根據當前檔案目錄,找到上一級目錄,以此類推

步驟4:拼接安裝包所在的路徑

if __name__ == '__main__':

with open('../config/WiFibanlv_caps.yaml', 'r', encoding='utf-8') as file:

     data = yaml.load(file)

base_dir=os.path.dirname(os.path.dirname(__file__))#os.path.dirname(__file__)表示獲取當前檔案的路徑

print(os.path.dirname(__file__))

print(base_dir)

app_path=os.path.join(base_dir, 'app',data['appname'])

#join是表示把幾個路徑拼接起來,

#將base_dir和app資料夾拼接起來,然後找到app資料夾下面的的apk檔名稱

print(app_path)

完整指令碼:

1.定義yaml配置檔案

2.定義log,conf日誌配置檔案

 

3.封裝capability啟動app的指令碼desired_caps.py

#!urs/bin/python

#!_*_ coding:UTF-8 _*_

from appium import webdriver

import yaml

import  logging

import logging.config

import os

#日誌配置檔案

CON_LOG='../config/log.conf'

logging.config.fileConfig(CON_LOG)

logging=logging.getLogger()


def appium_desired():

    #讀取配置檔案的資料,有兩種方式

    #方式1,必須以close結尾

    file = open('../config/WiFibanlv_caps.yaml', 'r')

    data = yaml.load(file)

    file.close() #必須使用close,否則檔案會出現佔用情況

    #方式2

    with open('../config/WiFibanlv_caps.yaml', 'r', encoding='utf-8') as file:

        data = yaml.load(file)



    logging.info("Initialize  APP...")

    desired_caps = {}

    desired_caps['platformName'] = data['platformName']

    desired_caps['platformVersion'] = data['platformVersion']

    # 第一個模擬器預設127.0.0.1:62001  第二個預設:127.0.0.1:62025

    desired_caps['deviceName'] = data['deviceName']

    #定義apk安裝包的相對路徑

    base_dir = os.path.dirname(os.path.dirname(__file__))  # os.path.dirname(__file__)表示獲取當前檔案的路徑

    app_path = os.path.join(base_dir, 'app', data['appname'])

    desired_caps['app'] = app_path



    desired_caps['packageName'] = data['packageName']

    desired_caps['appActivity'] = data['appActivity']

    desired_caps['noReset'] = data['noReset']

    desired_caps['unicodekeyboard'] = data['unicodekeyboard']

    desired_caps['resetkeyboard'] = data['resetkeyboard']

    desired_caps['uiautomationName'] = data['uiautomationName']

    logging.info("Start APP...")

    driver = webdriver.Remote('http://' + str(data['ip']) + ':' + str(data['port']) + '/wd/hub', desired_caps)

    driver.implicitly_wait(8)

    return driver

    #除錯當前指令碼方法

if __name__ == '__main__':

    appium_desired()

每封裝一個模組,都要引用__mian__檢測下當前指令碼是否能執行成功,capability封裝後執行成功結果如下: