0


函数放在类中调用

1.原来的函数及调用:

import os
import yaml
from appium import webdriver
def android_driver() :
    # 从desired_caps.yaml读取driver配置数据

    # 项目根目录路径,即android-ui-autotest文件夹的路径,获取当前文件所在目录的上一级目录
    BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    # capabilities配置文件desired_caps.py路径
    # os.path.dirname返回pyth的父路径
    # os.path.abspath返回pyth的绝对路径
    DESIRED_CAPS_YAML_PATH = BASE_PATH + '\config\desired_caps.yaml'
    print(DESIRED_CAPS_YAML_PATH)

    stream = open(DESIRED_CAPS_YAML_PATH, 'r')

    data = yaml.load(stream, Loader=yaml.FullLoader)

    desired_caps = {'platformName' : data['platformName'],
                    'platformVersion' : data['platformVersion'],
                    'deviceName' : data['deviceName'],
                    'appPackage' : data['appPackage'],
                    'appActivity' : data['appActivity'],
                    'ensureWebviewsHavePages' : data['ensureWebviewsHavePages'],
                    'noReset' : data['noReset']}

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

2.后面直接再上面加类不,如下:

class baseDrivera :
    @staticmethod
    def android_driver() :
        # 从desired_caps.yaml读取driver配置数据

        # 项目根目录路径,即android-ui-autotest文件夹的路径,获取当前文件所在目录的上一级目录
        BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        # capabilities配置文件desired_caps.py路径
        # os.path.dirname返回pyth的父路径
        # os.path.abspath返回pyth的绝对路径
        DESIRED_CAPS_YAML_PATH = BASE_PATH + '\config\desired_caps.yaml'
        print(DESIRED_CAPS_YAML_PATH)

        stream = open(DESIRED_CAPS_YAML_PATH, 'r')

        data = yaml.load(stream, Loader=yaml.FullLoader)

        desired_caps = {'platformName' : data['platformName'],
                        'platformVersion' : data['platformVersion'],
                        'deviceName' : data['deviceName'],
                        'appPackage' : data['appPackage'],
                        'appActivity' : data['appActivity'],
                        'ensureWebviewsHavePages' : data['ensureWebviewsHavePages'],
                        'noReset' : data['noReset']}

        driver = webdriver.Remote('http://' + str(data['ip']) + ':' + str(data['port']) + '/wd/hub', desired_caps)
        driver.implicitly_wait(15)
baseDrivera.android_driver()
这样也可以。

3.按照下面的也可以;

class baseDrivera :
    def __init__(self, se_driver) :
        self.driver = se_driver
#加上上面那段函数,才能用类
    @staticmethod
    def android_driver() :
        # 从desired_caps.yaml读取driver配置数据

        # 项目根目录路径,即android-ui-autotest文件夹的路径,获取当前文件所在目录的上一级目录
        BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        # capabilities配置文件desired_caps.py路径
        # os.path.dirname返回pyth的父路径
        # os.path.abspath返回pyth的绝对路径
        DESIRED_CAPS_YAML_PATH = BASE_PATH + '\config\desired_caps.yaml'
        print(DESIRED_CAPS_YAML_PATH)

        stream = open(DESIRED_CAPS_YAML_PATH, 'r')

        data = yaml.load(stream, Loader=yaml.FullLoader)

        desired_caps = {'platformName' : data['platformName'],
                        'platformVersion' : data['platformVersion'],
                        'deviceName' : data['deviceName'],
                        'appPackage' : data['appPackage'],
                        'appActivity' : data['appActivity'],
                        'ensureWebviewsHavePages' : data['ensureWebviewsHavePages'],
                        'noReset' : data['noReset']}

        driver = webdriver.Remote('http://' + str(data['ip']) + ':' + str(data['port']) + '/wd/hub', desired_caps)
        driver.implicitly_wait(15)
if __name__ == '__main__':
    Driveara = baseDrivera(['some', 'se_driver'])
    Driveara.android_driver()

重点:

第1段:

class baseDrivera :
    def __init__(self, se_driver) :
        self.driver = se_driver
最后1段调用:
if __name__ == '__main__':
    Driveara = baseDrivera(['some', 'se_driver'])
    Driveara.android_driver()
4.目前没有搞明白第2个写法和第3个写法的区别,要是有明白这块的辛苦帮忙答疑,谢谢啦。
标签: python selenium

本文转载自: https://blog.csdn.net/qq_45746739/article/details/127297018
版权归原作者 样白杨123 所有, 如有侵权,请联系我们删除。

“函数放在类中调用”的评论:

还没有评论