【发布时间】:2021-11-27 14:33:40
【问题描述】:
有人可以帮我解决这个 Selenium Pytest 框架问题吗?我试着用谷歌搜索它。但没有任何帮助。为长长的问题道歉。想确保我能清楚地描述问题。
我在下面的字典“my_test_data”中有员工测试数据。以下是我想要在单次运行中为每个员工实现的场景(py.test -v -s)。要求单次运行的原因是,我想从每个测试套件中获取事务 ID 并发送合并电子邮件。例如,如果测试为 5 名员工运行,那么将生成 5 个唯一的事务 ID,我通过使用 selenium 检查 Web 元素来获取这些事务 ID。
- 登录门户
- 输入个人信息
- 输入教育信息
- 输入工作经历信息
- 提交
- 获取会话 ID 并将其附加到“session_id”列表中。
- 退出
所以,如果我有 5 名员工,那么“session_id”列表中将提供 5 个唯一的会话 ID。为所有 5 名员工运行所有测试后,我将获取会话 ID 并发送合并的电子邮件以及其他一些其他信息。
我尝试通过夹具中的参数化来做到这一点(params=["employee_1", "employee_1" ... "employee_n"]。但问题是,它针对给定数量的参数单独运行每个测试。但是我希望为每个参数运行整个测试套件。
示例:
当我执行 py.test -v -s 时,对于 params 中的每个员工,我都想做这样的事情。
预期执行顺序:
员工 1:
test_login.py
test_personal_information.py
test_education_information.py
test_work_experience_information.py
test_logout.py
员工 2:
test_login.py
test_personal_information.py
test_education_information.py
test_work_experience_information.py
test_logout.py
员工 3:
test_login.py
test_personal_information.py
test_education_information.py
test_work_experience_information.py
test_logout.py
实际执行顺序:
test_login.py (For Employee 1)
test_login.py (For Employee 2)
test_login.py (For Employee 3)
test_personal_information.py (For Employee 1)
test_personal_information.py (For Employee 2)
test_personal_information.py (For Employee 3)
。 . .
my_test_data = {
"employee_1": {
"personal_information": "...",
"education_information": "...",
"work_experience_information": "...",
},
"employee_2": {"..."}
# Till exmployee n
}
################ conftest.py ################
@pytest.fixture(scope="session", params=["employee_1", "employee_2", "employee_3", "employee_n"]) # Till employee n
def test_setup(request):
driver = webdriver.Chrome()
driver.get(url)
session_id = []
yield driver, request.param, session_id
driver.quit()
################ test_login.py ################
@pytest.mark.usefixtures("test_setup")
class TestPersonalInformation(unittest.TestCase):
# Login action is performed here
pass
################ test_personal_information.py ################
@pytest.mark.usefixtures("test_setup")
class TestPersonalInformation(unittest.TestCase):
@pytest.fixture(autouse=True)
def class_setup(self, set_driver):
self.personal_information_page = PersonalInformationPage(drier=test_setup[0])
self.test_Data = test_setup[1]
def test_personal_information_page(self):
self.personal_information_page.send_first_name(self.test_Data["personal_information"]["first_name"])
self.personal_information_page.send_first_name(self.test_Data["personal_information"]["last_name"])
# Few other personal informations are also sent
self.personal_information_page.click_on_next_button()
################ test_education_information.py ################
@pytest.mark.usefixtures("test_setup")
class TestEducationInformation(unittest.TestCase):
@pytest.fixture(autouse=True)
def class_setup(self, set_driver):
self.education_information_page = EducationInformationPage(drier=test_setup[0])
self.test_Data = test_setup[1]
def test_education_information_page(self):
self.education_information_page.send_highest_degree(self.test_Data["education_information"]["highest_degree"])
self.education_information_page.send_university_name(self.test_Data["education_information"]["university_name"])
# Few other education informations are also sent
self.education_information_page.click_on_next_button()
################ test_work_experience_information.py ################
@pytest.mark.usefixtures("test_setup")
class TestWorkExperienceInformation(unittest.TestCase):
@pytest.fixture(autouse=True)
def class_setup(self, set_driver):
self.work_experience_page = WorkExperienceInformationPage(drier=test_setup[0])
self.test_Data = test_setup[1]
def test_work_experience_page_page(self):
self.work_experience_page.send_work_experience(self.test_Data["work_experience_information"]["work_experience"])
self.work_experience_page.send_current_organization_name(self.test_Data["work_experience_information"]["current_organization_name"])
# Few other work experience informations are also sent
self.work_experience_page.submit()
################ test_logout.py ################
@pytest.mark.usefixtures("test_setup")
class TestPersonalInformation(unittest.TestCase):
# Logout action is performed here
pass
【问题讨论】: