1

我想知道是否有人可以提供帮助?

我目前正在为我的 UI 测试自动化使用 playwright-pytest。

在旧版本中,您可以在比赛文件中设置持久上下文,例如:

@pytest.fixture(scope="session")
def context(
        browser_type: BrowserType,
        browser_type_launch_args: Dict,
        browser_context_args: Dict
):
    context = browser_type.launch_persistent_context("./foobar", **{
        **browser_type_launch_args,
        **browser_context_args,
        "locale": "en_GB",
    })
    yield context
    context.close()

当 playwright-pytest 更新时,您现在被迫使用 scope="function" 这似乎为每个新测试加载一个新浏览器,而之前它加载了一次浏览器。如果将范围更改回会话,则会出现以下错误:

ScopeMismatch:您尝试使用“会话”范围的请求对象访问“函数”范围的夹具“browser_context_args”,涉及工厂

有没有一种方法可以让我使用函数范围只加载一次浏览器并为测试类中的每个测试创建一个新选项卡?

我的一个测试类的一个例子如下:

@pytest.mark.usefixtures("context")
class TestImportData:

    @pytest.fixture(autouse=True)
    def classSetup(self, page):
        self.home_page = HomePage(page)
        self.import_data = ImportData(page)
        self.login_page = LoginPage(page)
        self.manage_candidate = ManageCandidate(page)
        self.new_candidate = NewCandidate(page)
        self.approved_timesheets = ApprovedTimesheets(page)
        self.manage_placements = ManagePlacements(page)
        self.manage_companies = ManageCompanies(page)
        self.manage_clients = ManageClients(page)

    @pytest.mark.order(1)
    def test_import_data_page_loads(self, env, page):
        self.login_page.login_to_evertime(env, "SysAdmin")
        self.home_page.click_import_data_button()

    @pytest.mark.order(2)
    def test_import_candidate(self, env, page):
        word = string.ascii_lowercase
        cand_id = (''.join(random.choice(word) for i in range(10)))
        self.import_data.create_candidate_import_file(cand_id)
        self.login_page.login_to_evertime(env, "SysAdmin")
        self.home_page.click_import_data_button()

任何帮助将不胜感激,因为与范围为会话时相比,我的测试现在运行得如此缓慢。

4

0 回答 0