selenium,单例driver


单例driver

class GetDriver(object):
	driver =None
	browser_type = getattr(GlobalConfig,"BROWSER_TYPE",None)  # maybe Firefox, Chrome, IE
	url =getattr(GlobalConfig,"URL",None)
#定义类方法,以使用每一次调用都能获取相同的driver实例
    #定义类方法,以使用每一次调用都能获取相同的driver实例
	def __init__(self):
		pass
	@classmethod
    def get_driver1(cls):
        """
        通过if语句,来控制初始化不同浏览器的启动,默认是启动Chrome
        :return: driver
        """
        # self.log.info("启动%s浏览器"%self.browser_type)
        if GetDriver.driver is  None:
            if cls.browser_type == 'Firefox':
                GetDriver.driver = webdriver.Firefox()
            elif cls.browser_type == 'Chrome':
                GetDriver.driver = webdriver.Chrome(r"C:\安装包\chromedriver_win32\chromedriver.exe")
            elif cls.browser_type == 'IE':
                GetDriver.driver = webdriver.Ie()
            else:
                GetDriver.driver = webdriver.Chrome()
            GetDriver.driver.maximize_window()
            GetDriver.driver.implicitly_wait(15)
            GetDriver.driver.get(cls.url)
        return GetDriver.driver