问题
用例调用,封装selenium前置方法测试报错,请问如何解决啊
报错信息
环境
这是conftest.py文件中的调用浏览器前置方法
def pytest_addoption(parser):
parser.addoption(
"--browser", action="store", default=config['WEB自动化配置']['browser'],
# help="browser option:firefox or chrome or ie"
help="browser option:firefox or chrome"
)
parser.addoption(
"--host", action="store", default=config['项目运行设置']['TEST_URL'], help="test host->http://127.0.0.1"
)
@pytest.fixture(scope=“function”)
def driver(request):
try:
from selenium import webdriver
name = request.config.getoption('--browser')
# request.config.getoption('--browyyg')
_driver = None
if name == 'chrome':
# _driver = webdriver.Chrome(executable_path=os.path.join(bp.DRIVER_DIR, 'chromedriver.exe'))
_driver = webdriver.Chrome()
# print(_driver)
# elif name == 'ie':
# _driver = webdriver.Ie(executable_path=os.path.join(bp.DRIVER_DIR, 'chromedriver.exe'))
# elif name == 'firefox':
# _driver = webdriver.Firefox(executable_path=os.path.join(bp.DRIVER_DIR, 'chromedriver.exe'))
elif name == 'chromeheadless':
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
_driver = webdriver.Chrome(options=chrome_options)
_driver.set_window_size(1920, 1080)
GlobalMangager().set_valuer('driver', _driver)
# 增加隐式等待
_driver.implicitly_wait(5)
print('正在启动浏览器:%s' % name)
def fn():
print('用例执行完毕正在退出')
_driver.quit()
request.addfinalizer(fn)
return _driver
except ImportError:
pytest.exit('为安装selenium')
except Exception as e:
pytest.exit('启动webdriver失败{}'.format(e))
@pytest.fixture(scope=“function”)
def driver(request):
try:
from selenium import webdriver
name = request.config.getoption(‘–browser’)
# request.config.getoption(‘–browyyg’)
_driver = None
if name == ‘chrome’:
# _driver = webdriver.Chrome(executable_path=os.path.join(bp.DRIVER_DIR, ‘chromedriver.exe’))
_driver = webdriver.Chrome()
elif name == 'chromeheadless':
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
_driver = webdriver.Chrome(options=chrome_options)
_driver.set_window_size(1920, 1080)
GlobalMangager().set_valuer('driver', _driver)
_driver.implicitly_wait(5)
print('正在启动浏览器:%s' % name)
'''
后置方法
'''
def fn():
print('用例执行完毕正在退出')
_driver.quit()
request.addfinalizer(fn)
return _driver
except ImportError:
# exit()退出
pytest.exit('为安装selenium')
except Exception as e:
pytest.exit('启动webdriver失败{}'.format(e))
用例文件
import pytest
from Base.baseContainer import GlobalMangager
from Base.baseData import DataDriver
from PageObject.web_txzj_test.web_login import LoginPage
from selenium import webdriver
from Base.baseContainer import GlobalMangager
class TestCase():
@pytest.mark.parametrize('case_data', DataDriver().get_case_data('login'))
def test_login(self, driver, case_data):
# driver = webdriver.Chrome()
# # 将driver存着入全局变量中
# GlobalMangager().set_valuer('driver', driver)
lp = LoginPage()
lp.login(case_data['name'], case_data['pwd'])
lp.assert_login_ok(case_data['flag'])