验证
介绍
¥Introduction
Playwright 在称为 浏览器上下文 的隔离环境中执行测试。这种隔离模型提高了可重复性并防止级联测试失败。测试可以加载现有的经过身份验证的状态。这消除了在每个测试中进行身份验证的需要,并加快了测试执行速度。
¥Playwright executes tests in isolated environments called browser contexts. This isolation model improves reproducibility and prevents cascading test failures. Tests can load existing authenticated state. This eliminates the need to authenticate in every test and speeds up test execution.
核心理念
¥Core concepts
无论你选择哪种身份验证策略,你都可能将经过身份验证的浏览器状态存储在文件系统上。
¥Regardless of the authentication strategy you choose, you are likely to store authenticated browser state on the file system.
我们建议创建 playwright/.auth
目录并将其添加到你的 .gitignore
中。你的身份验证例程将生成经过身份验证的浏览器状态并将其保存到此 playwright/.auth
目录中的文件中。稍后,测试将重用此状态并开始已通过身份验证的操作。
¥We recommend to create playwright/.auth
directory and add it to your .gitignore
. Your authentication routine will produce authenticated browser state and save it to a file in this playwright/.auth
directory. Later on, tests will reuse this state and start already authenticated.
浏览器状态文件可能包含敏感 Cookie 和标头,这些 Cookie 和标头可用于冒充你或你的测试账户。我们强烈建议不要将它们检入私有或公共存储库。
¥The browser state file may contain sensitive cookies and headers that could be used to impersonate you or your test account. We strongly discourage checking them into private or public repositories.
- Bash
- PowerShell
- Batch
mkdir -p playwright/.auth
echo $'\nplaywright/.auth' >> .gitignore
New-Item -ItemType Directory -Force -Path playwright\.auth
Add-Content -path .gitignore "`r`nplaywright/.auth"
md playwright\.auth
echo. >> .gitignore
echo "playwright/.auth" >> .gitignore
每次测试前登录
¥Signing in before each test
Playwright API 可以使用登录表单进行 自动化交互 测试。
¥The Playwright API can automate interaction with a login form.
以下示例登录到 GitHub。执行这些步骤后,浏览器上下文将被验证。
¥The following example logs into GitHub. Once these steps are executed, the browser context will be authenticated.
- Sync
- Async
page = context.new_page()
page.goto('https://github.com/login')
# Interact with login form
page.get_by_label("Username or email address").fill("username")
page.get_by_label("Password").fill("password")
page.get_by_role("button", name="Sign in").click()
# Continue with the test
page = await context.new_page()
await page.goto('https://github.com/login')
# Interact with login form
await page.get_by_label("Username or email address").fill("username")
await page.get_by_label("Password").fill("password")
await page.get_by_role("button", name="Sign in").click()
# Continue with the test
每次测试都重新登录会降低测试执行速度。要缓解这种情况,请改用现有的身份验证状态。
¥Redoing login for every test can slow down test execution. To mitigate that, reuse existing authentication state instead.
重用登录状态
¥Reusing signed in state
Playwright 提供了一种在测试中重用登录状态的方法。这样你只需登录一次,然后跳过所有测试的登录步骤。
¥Playwright provides a way to reuse the signed-in state in the tests. That way you can log in only once and then skip the log in step for all of the tests.
Web 应用使用基于 Cookie 或基于令牌的身份验证,其中经过身份验证的状态存储为 cookies、本地存储 或 IndexedDB。Playwright 提供 browser_context.storage_state() 方法,可用于从已验证的上下文中检索存储状态,然后使用预填充状态创建新的上下文。
¥Web apps use cookie-based or token-based authentication, where authenticated state is stored as cookies, in local storage or in IndexedDB. Playwright provides browser_context.storage_state() method that can be used to retrieve storage state from authenticated contexts and then create new contexts with prepopulated state.
Cookie、本地存储和 IndexedDB 状态可在不同的浏览器中使用。它们依赖于应用的身份验证模型,该模型可能需要 Cookie、本地存储或 IndexedDB 的某种组合。
¥Cookies, local storage and IndexedDB state can be used across different browsers. They depend on your application's authentication model which may require some combination of cookies, local storage or IndexedDB.
以下代码片段从已验证的上下文中检索状态,并使用该状态创建一个新的上下文。
¥The following code snippet retrieves state from an authenticated context and creates a new context with that state.
- Sync
- Async
# Save storage state into the file.
storage = context.storage_state(path="state.json")
# Create a new context with the saved storage state.
context = browser.new_context(storage_state="state.json")
# Save storage state into the file.
storage = await context.storage_state(path="state.json")
# Create a new context with the saved storage state.
context = await browser.new_context(storage_state="state.json")
高级场景
¥Advanced scenarios
会话存储
¥Session storage
重用经过身份验证的状态涵盖基于 cookies、本地存储 和 IndexedDB 的身份验证。会话存储 很少用于存储与登录状态相关的信息。会话存储特定于特定域,并且不会在页面加载时保留。Playwright 不提供 API 来保存会话存储,但以下代码片段可用于保存/加载会话存储。
¥Reusing authenticated state covers cookies, local storage and IndexedDB based authentication. Rarely, session storage is used for storing information associated with the signed-in state. Session storage is specific to a particular domain and is not persisted across page loads. Playwright does not provide API to persist session storage, but the following snippet can be used to save/load session storage.
- Sync
- Async
import os
# Get session storage and store as env variable
session_storage = page.evaluate("() => JSON.stringify(sessionStorage)")
os.environ["SESSION_STORAGE"] = session_storage
# Set session storage in a new context
session_storage = os.environ["SESSION_STORAGE"]
context.add_init_script("""(storage => {
if (window.location.hostname === 'example.com') {
const entries = JSON.parse(storage)
for (const [key, value] of Object.entries(entries)) {
window.sessionStorage.setItem(key, value)
}
}
})('""" + session_storage + "')")
import os
# Get session storage and store as env variable
session_storage = await page.evaluate("() => JSON.stringify(sessionStorage)")
os.environ["SESSION_STORAGE"] = session_storage
# Set session storage in a new context
session_storage = os.environ["SESSION_STORAGE"]
await context.add_init_script("""(storage => {
if (window.location.hostname === 'example.com') {
const entries = JSON.parse(storage)
for (const [key, value] of Object.entries(entries)) {
window.sessionStorage.setItem(key, value)
}
}
})('""" + session_storage + "')")