UI自动化测试单例实现报错:AttributeError: ‘NoneType‘ object has no attribute ‘get_driver‘

UI自动化测试单例实现报错:AttributeError: ‘NoneType’ object has no attribute ‘get_driver’


from selenium import webdriver

from configs.env import Env


class Singleton1(object):
    _instance = None

    def __new__(cls, *args, **kwargs):
        print('判断hasattr现在是否有_instance属性', hasattr(cls, '_instance'))

        if not hasattr(cls, '_instance'):
            cls._instance = super(Singleton1, cls).__new__(cls, *args, **kwargs)
        return cls._instance


class Singleton2(object):
    _instance = None

    def __new__(cls, *args, **kwargs):
        if cls._instance is None:
            cls._instance = super(Singleton2, cls).__new__(cls, *args, **kwargs)
        return cls._instance


class A(Singleton1):
    def a(self):
        pass


class CommDriver(Singleton1):
    driver = None

    """
    通用的浏览器类:
        - 打开浏览器
        - 最大化
        - 浏览器的类型
        - 有头和无头的设置
              - 关闭浏览器
    """

    # def __init__(self):
    #     self.driver = webdriver.Chrome()

    def get_driver(self, browser_type=Env.BROWSER_TYPE, headless_flag=Env.HEADLESS_FLAG):
        """
        根据env的配置, 获取一个浏览器,最大化并设置页面最大超时时间
        :param browser_type: 浏览器类型
        :param headless_flag: 有头无头的标记
        :return:
        """
        if self.driver is None:
            if not headless_flag:  # 有头

                if browser_type == 'firefox':
                    self.driver = webdriver.Firefox()
                elif browser_type == 'chrome':
                    self.driver = webdriver.Chrome()
            else:  # 无头
                if browser_type == 'firefox':
                    option = webdriver.FirefoxOptions()
                    option.add_argument('--headless')
                    self.driver = webdriver.Firefox(options=option)
                elif browser_type == 'chrome':
                    option = webdriver.ChromeOptions()
                    option.add_argument('--headless')
                    self.driver = webdriver.Chrome(options=option)

            self.driver.maximize_window()
            self.driver.set_page_load_timeout(Env.PAGE_LOAD_TIMEOUT) # 加载页面的最大超时时间
        return self.driver


if __name__ == '__main__':
    test_flag = 1
    if test_flag == 3:
        d3 = CommDriver()
        d4 = CommDriver()
        print(id(d3) == id(d4))

    if test_flag == 2:
        a1 = A().a
        a2 = A().a
        print(id(a1) == id(a2))

    if test_flag == 1:
        d1 = CommDriver().get_driver()
        d2 = CommDriver().get_driver()
        print(id(d1) == id(d2))

UI自动化测试单例实现报错:AttributeError: ‘NoneType‘ object has no attribute ‘get_driver‘_第1张图片
UI自动化测试单例实现报错:AttributeError: ‘NoneType‘ object has no attribute ‘get_driver‘_第2张图片
UI自动化测试单例实现报错:AttributeError: ‘NoneType‘ object has no attribute ‘get_driver‘_第3张图片
UI自动化测试单例实现报错:AttributeError: ‘NoneType‘ object has no attribute ‘get_driver‘_第4张图片

你可能感兴趣的:(ui,python,开发语言)