我的测试正在 Chromium、Firefox 和 Webkit(移动)浏览器中运行。
但是其中一些测试不应该在移动浏览器中执行。
如果 Playwright 在 Webkit 中执行测试,如何设置跳过此类测试?
我尝试使用这里的 pytest 注释https://playwright.dev/python/docs/test-runners#examples但它不起作用,所有测试都像之前一样执行。
import pytest
@pytest.mark.only_browser("firefox")
# @pytest.mark.skip_browser("webkit")
def test_selected(order):
order.open()
order.set_channel('XXX')
执行的命令是
pytest ./tests/Orders
(如果重要的话)。
为了在一些浏览器中执行,我使用
conftest.py
中的固定装置:
@fixture(scope="session",
params=['chromium', 'firefox', 'mobile'],
ids=['chromium', 'firefox', 'mobile'])
def pw(get_playwright, request):
headless = True if request.config.getini('headless').casefold() == 'true' else False
match request.param:
case 'chromium':
app = App(
get_playwright.chromium.launch(headless=headless),
{
'base_url': request.config.getini('base_url'),
'viewport': {"width": 1920, "height": 1080},
},
)
case 'firefox':
app = App(
get_playwright.firefox.launch(headless=headless),
{
'base_url': request.config.getini('base_url'),
'viewport': {"width": 1366, "height": 768}
},
)
case 'mobile':
app = App(
get_playwright.webkit.launch(headless=headless),
{
'base_url': request.config.getini('base_url'),
**get_playwright.devices['iPhone 14 Pro Max landscape']
},
)
case _: assert False, 'Wrong browser'
yield app
app.close()
您可以使用
Conditionally skip
,这可以根据情况跳过某些测试,例如:
单次测试:
test('skip this test', async ({ page, browserName }) => {
test.skip(browserName === 'firefox', 'Still working on it');
});
有条件地跳过一组测试:
例如,您可以通过传递回调来仅在 Chromium 中运行一组测试。
test.describe('chromium only', () => {
test.skip(({ browserName }) => browserName !== 'chromium', 'Chromium only!');
test.beforeAll(async () => {
// This hook is only run in Chromium.
});
test('test 1', async ({ page }) => {
// This test is only run in Chromium.
});
test('test 2', async ({ page }) => {
// This test is only run in Chromium.
});
});