1、若有多个案例时,会打开多个网页
解决方法:单例模式
- 在testcases包中新增conftest.py 文件@pytest.fixture(scope='session', autouse=True)
- driver也需要修
conftest.py 文件
import logging import pytest from src.pages.loginpage import LoginPageObject from src.initilization.broswerdriver import BuildUpDriver @pytest.fixture(scope='session', autouse=True) def login_douban(): # 执行所有测试用例之前跑 logging.info("Start init login douban") login_page_object = LoginPageObject() login_page_object.login('*****', '****') # 执行完所有测试用例后跑 yield logging.info("End all test cases") BuildUpDriver.driver.quit()
from selenium import webdriver class BuildUpDriver: driver='' @staticmethod def build_up_driver(): chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--disable-infobars') BuildUpDriver.driver = webdriver.Chrome(options=chrome_options) BuildUpDriver.driver.implicitly_wait(10) BuildUpDriver.driver.get("https://www.douban.com/") BuildUpDriver.driver.maximize_window()
2、自动化脚本运行时报错信息为: E AttributeError: 'str' object has no attribute 'find_element'
解决方法:这个是由于driver没有入口得原因,查看代码,在runner.py中添加driver得方法:
BuildUpDriver.build_up_driver()
3、生成报告时,需要allure,下载pip3 install pytest-allure-adaptor。
运行时报错:pytest pluggy.manager.PluginValidationError: unknown hook 'pytest_namespace'报错处理办法
解决方法:是由于pytest版本问题,将pytest换成旧版本
# 卸载已安装的pytest pip uninstall pytest # 安装4.0.2版本pytest pip install pytest==4.0.2