一、基类文件Driver.py开发
驱动选择基类文件,主要解决的是如下问题:
1、不同浏览器的驱动选择
2、是否默认无头模式运行的选择
3、是否打开手机调试模式的选择
4、用例执行是否发送到远程容器环境运行
首先我需要做的是设置一个特殊化的变量,这个变量无其它意义只是用例占位符,如下
driver = None
随后我会编写一个函数去引用这个特殊的变量driver,具体代码如下
def _capture_screenshot(path, filename=None):
if filename is None:
filename = str(time.time()*100000).split(".")[0] + ".png"
file_path = os.path.join(path, filename)
driver.save_screenshot(file_path)
return filename
本函数主要是selenium的截图功能封装函数,传入文件名称即可保存截图。
接下来就是驱动选择基类文件的核心代码了,先附上代码供参考:
def browser_driver(browser_name: str, headless=True, is_mobile=True, is_remote=False):
global driver
driver = None
if driver is None:
# browser_name = Config.BROWSER_NAME
# headless = Config.HEADLESS
# is_mobile = Config.IS_MOBILE
if browser_name == "chrome":
options = webdriver.ChromeOptions()
# 無頭
if bool(headless):
options.add_argument('--headless')
# options.add_argument(f'--proxy-server={ProxyServer.proxy.proxy}')
if bool(is_mobile):
options.add_experimental_option('mobileEmulatio