playwright文档说明

playwright官方文档翻译


编写测试

<h1>编写测试</h1> <p>Playwright断言是专门为动态web创建的。在满足必要条件之前,将自动重试检查。剧作家来了<a href="https://playwright.dev/python/docs/actionability">自动等待</a>内置意味着它在执行操作之前等待元素可操作。剧作家提供了<a href="https://playwright.dev/python/docs/test-assertions">期望</a>函数来编写断言。</p> <p>看看下面的示例测试,了解如何使用[locators]编写测试(<a href="https://playwright.dev/python/docs/locators)以及web第一断言">https://playwright.dev/python/docs/locators)以及web第一断言</a>。</p> <pre><code class="language-python">import re from playwright.sync_api import Page, expect def test_homepage_has_Playwright_in_title_and_get_started_link_linking_to_the_intro_page(page: Page): page.goto("https://playwright.dev/") # Expect a title "to contain" a substring. expect(page).to_have_title(re.compile("Playwright")) # create a locator get_started = page.get_by_role("link", name="Get started") # Expect an attribute "to be strictly equal" to the value. expect(get_started).to_have_attribute("href", "/docs/intro") # Click the get started link. get_started.click() # Expects the URL to contain intro. expect(page).to_have_url(re.compile(".*intro"))</code></pre> <h3>断言[​]</h3> <p>Playwright提供了expect函数,该函数将等待直到满足预期条件。</p> <pre><code class="language-python">import re from playwright.sync_api import expect expect(page).to_have_title(re.compile("Playwright"))</code></pre> <h3>定位<a href="https://playwright.dev/python/docs/writing-tests#locators" title="Direct link to heading">​</a></h3> <p><a href="https://playwright.dev/python/docs/locators">定位器</a>是Playwright自动等待和重试能力的核心部分。定位器表示一种随时在页面上查找元素的方法,用于对元素执行操作,例如“.click”“.fill”等。</p> <pre><code>from playwright.sync_api import expectget_started = page.get_by_role("link", name="Get started")expect(get_started).to_have_attribute("href", "/docs/installation")get_started.click()</code></pre> <h3>测试隔离<a href="https://playwright.dev/python/docs/writing-tests#test-isolation" title="Direct link to heading">​</a></h3> <p>Playwright Pytest插件基于测试夹具的概念,例如〔内置页面夹具〕(<a href="https://playwright.dev/python/docs/test-runners),已通过测试。由于浏览器上下文,页面在测试之间被隔离,这相当于一个全新的浏览器配置文件,即使在一个浏览器中运行多个测试,每个测试都会得到一个新的环境">https://playwright.dev/python/docs/test-runners),已通过测试。由于浏览器上下文,页面在测试之间被隔离,这相当于一个全新的浏览器配置文件,即使在一个浏览器中运行多个测试,每个测试都会得到一个新的环境</a>。</p> <pre><code>from playwright.sync_api import Pagedef test_basic_test(page: Page): # ...</code></pre> <h3>使用测试挂钩<a href="https://playwright.dev/python/docs/writing-tests#using-test-hooks" title="Direct link to heading">​</a></h3> <p>您可以使用各种[固定装置](<a href="https://docs.pytest.org/en/6.2.x/fixture.html#autouse-fixtures-fixtures-you-don-hand-to-request)在测试之前或之后执行代码并在它们之间共享对象。“函数”范围的fixture(例如,带有autouse)的行为类似于beforeEach/afterEach。具有autouse的“模块”范围的fixture的行为类似于beforeAll/afterAll,它在所有测试之前和之后运行">https://docs.pytest.org/en/6.2.x/fixture.html#autouse-fixtures-fixtures-you-don-hand-to-request)在测试之前或之后执行代码并在它们之间共享对象。“函数”范围的fixture(例如,带有autouse)的行为类似于beforeEach/afterEach。具有autouse的“模块”范围的fixture的行为类似于beforeAll/afterAll,它在所有测试之前和之后运行</a>。</p> <pre><code class="language-python">import pytest from playwright.sync_api import Page @pytest.fixture(scope="function", autouse=True) def before_each_after_each(page: Page): print("beforeEach") # Go to the starting url before each test. page.goto("https://playwright.dev/") yield print("afterEach") def test_main_navigation(page: Page): # Assertions use the expect API. expect(page).to_have_url("https://playwright.dev/")</code></pre> <pre><code></code></pre>

页面列表

ITEM_HTML