conftest作为全局调用,用于登录之后的其他元素使用,且保持登录状态的操作
在开始前需要搭建好测试环境,环境包含python+webdriver+浏览器驱动(此处为firefox)
1、conftest.py ##
import pytest
from selenium import webdriver
import time
from testcase.test_1 import test_001
@pytest.fixture(scope="session")
def driver(request):
print("获取火狐浏览器")
d = webdriver.Firefox()
test_001(d)
def end():
time.sleep(5)
d.quit()
request.addfinalizer(end)
return d
2、测试用例 test_1.py
import pytest
from time import sleep
def test_001(driver):
driver.get("http://www.baidu.com")
sleep(1)
driver.find_element_by_id("kw").send_keys("元素定位")
driver.find_element_by_id("su").click()
print("第一个登录测试:登录成功")
def test0010(test_001):
print("第二个登录测试:登录失败")