playwright+pytest+python怎么写循环
时间: 2023-08-10 18:02:57 浏览: 47
在使用 Pytest 和 Playwright 编写测试用例时,可以使用 Python 的循环语句来简化重复的测试步骤。以下是一个示例:
```python
import pytest
from playwright.sync_api import Playwright, sync_playwright
@pytest.mark.parametrize('url', [
'https://www.example.com/page1',
'https://www.example.com/page2',
'https://www.example.com/page3',
])
def test_my_test_case(playwright: Playwright, url: str):
with playwright.chromium.launch() as browser:
with browser.new_context() as context:
page = context.new_page()
page.goto(url)
# 执行测试步骤
# ...
```
在这个示例中,使用了 Pytest 的 `@pytest.mark.parametrize` 装饰器来传入不同的 `url` 参数值,然后使用 Python 的 `with...as...` 语句来创建 Playwright 的浏览器和页面对象,并在每个测试用例中循环执行测试步骤。