其实是很简单的,只需要继承unittest.TestCase并在重写init时进行控制,并注册解释器退出时关闭测试即可。
config.py
import selenium.webdriver
driver_type = selenium.webdriver.Chrome # 配置默认WebDriver类型
driver = None # type:selenium.webdriver.Remote
browser.py
import unittest
import common.config
import atexit
def quit_driver():
if common.config.driver:
common.config.driver.quit()
class TestCase(unittest.TestCase):
"""控制浏览器的的打开与关闭"""
def __init__(self, methodName='runTest'):
unittest.TestCase.__init__(self, methodName)
if not common.config.driver:
common.config.driver = common.config.driver_type()
atexit.register(quit_driver)
@property
def driver(self):
"""获取driver"""
return common.config.driver