我正在尝试单击一个按钮,使用 Playwright 在同一窗口中打开一个新选项卡。当我在 VS 代码中本地运行测试(标题)时,流程正常工作。然而,当我在 GitLab CI(无标题)中运行测试时,选项卡仍然打开,但它似乎截断了新选项卡 URL 的整个查询字符串参数。
测试在 Chromium 上运行
这是我运行来打开新选项卡的代码:
async cancelInsurance() {
const popupPromise = this.page.waitForEvent('popup');
await this.page.locator(Selectors.idpLink).click();
const newTab = await popupPromise;
console.log(newTab.url());
await newTab.click(Selectors.cancelButton);
}
示例网址:
gitlab 中的网址:https://test-environment.com/insurance_details.en-gb.html
本地网址(如何打开):https://test-environment.com/insurance_details.en-gb.html?&aid=100000&auth_key=12345
更新: 我运行 DEBUG=pw:api:
pw:api => locator.click started +1ms
pw:api waiting for event "popup" +8ms
pw:api checking visibility of locator(':scope') +7ms
pw:api waiting for locator('[data-testid="insurance-banner-card-view-details_testid"]') +2ms
pw:api locator resolved to <a target="_blank" aria-label="View insurance summary" data-testid="insurance-banner-card-view-details_testid" class="a83ed08757 c21c56c305 bf0537ecb5 ab98298258 af7297d90d" href="https://test-environment.com/insurance_details.en-gb.html">…</a> +2ms
pw:api attempting click action +4ms
pw:api waiting for element to be visible, enabled and stable +0ms
pw:api <= elementHandle.isHidden succeeded +2ms
pw:api element is visible, enabled and stable +29ms
pw:api scrolling into view if needed +0ms
pw:api done scrolling +1ms
pw:api performing click action +7ms
pw:api click action done +13ms
pw:api waiting for scheduled navigations to finish +0ms
pw:api navigations have finished +0ms
pw:api <= locator.click succeeded +5ms
pw:api "load" event fired +750ms
pw:api navigated to "https://ct.pinterest.com/ct.html" +6ms
pw:api "commit" event fired +496ms
pw:api "commit" event fired +6ms
pw:api navigated to "https://test-environment/insurance_details.en-gb.html" +1ms
pw:api <= page.waitForEvent succeeded +4ms
https://test-environment.com/insurance_details.en-gb.html
pw:api => page.click started +2ms
pw:api waiting for locator('button:has(span.e4adce92df:has-text("cancel"))') +14ms
pw:api "domcontentloaded" event fired +
这个解决方案在过去帮助我解决了无头打开和关闭引起的差异。让我们看看这是否也对您有帮助。 在无头 ON 模式下,浏览器代理不受尊重。因此,我必须添加以下代码,以使您的应用程序在无头和有头模式下处理相同。 在启动 url 之前尝试添加以下用户代理代码。
await context.addInitScript(() => {
Object.defineProperty(navigator, 'userAgentData', {
get: () => ({
brands: [{ brand: 'Google Chrome' }],
}),
})
})