隔离
介绍
¥Introduction
使用 Playwright 编写的测试在称为浏览器上下文的隔离的全新环境中执行。这种隔离模型提高了可重复性并防止级联测试失败。
¥Tests written with Playwright execute in isolated clean-slate environments called browser contexts. This isolation model improves reproducibility and prevents cascading test failures.
什么是测试隔离?
¥What is Test Isolation?
测试隔离是指每个测试与另一个测试完全隔离。每个测试都独立于任何其他测试运行。这意味着每个测试都有自己的本地存储、会话存储、cookie 等。Playwright 使用 BrowserContext 来实现这一点,这相当于隐身式配置文件。它们的创建速度快、成本低,并且完全隔离,即使在单个浏览器中运行也是如此。Playwright 为每个测试创建一个上下文,并在该上下文中提供默认的 Page。
¥Test Isolation is when each test is completely isolated from another test. Every test runs independently from any other test. This means that each test has its own local storage, session storage, cookies etc. Playwright achieves this using BrowserContexts which are equivalent to incognito-like profiles. They are fast and cheap to create and are completely isolated, even when running in a single browser. Playwright creates a context for each test, and provides a default Page in that context.
为什么测试隔离很重要?
¥Why is Test Isolation Important?
-
无故障残余。如果一项测试失败,不会影响另一项测试。
¥No failure carry-over. If one test fails it doesn't affect the other test.
-
易于调试错误或不稳定,因为你可以根据需要多次运行单个测试。
¥Easy to debug errors or flakiness, because you can run just a single test as many times as you'd like.
-
并行运行、分片等时不必考虑顺序。
¥Don't have to think about the order when running in parallel, sharding, etc.
测试隔离的两种方法
¥Two Ways of Test Isolation
测试隔离有两种不同的策略:从头开始或在中间进行清理。在测试之间进行清理的问题在于,很容易忘记清理,并且有些东西是不可能清理的,例如 "访问过的链接"。一个测试的状态可能会泄漏到下一个测试中,这可能会导致你的测试失败,并使调试变得更加困难,因为问题来自另一个测试。从头开始意味着一切都是新的,因此如果测试失败,你只需查看该测试即可进行调试。
¥There are two different strategies when it comes to Test Isolation: start from scratch or cleanup in between. The problem with cleaning up in between tests is that it can be easy to forget to clean up and some things are impossible to clean up such as "visited links". State from one test can leak into the next test which could cause your test to fail and make debugging harder as the problem comes from another test. Starting from scratch means everything is new, so if the test fails you only have to look within that test to debug.
Playwright 如何实现测试隔离
¥How Playwright Achieves Test Isolation
Playwright 使用浏览器上下文来实现测试隔离。每个测试都有自己的浏览器上下文。每次运行测试都会创建一个新的浏览器上下文。使用 Playwright 作为测试运行程序时,默认情况下会创建浏览器上下文。否则,你可以手动创建浏览器上下文。
¥Playwright uses browser contexts to achieve Test Isolation. Each test has its own Browser Context. Running the test creates a new browser context each time. When using Playwright as a Test Runner, browser contexts are created by default. Otherwise, you can create browser contexts manually.
- Test
- Library
import { test } from '@playwright/test';
test('example test', async ({ page, context }) => {
// "context" is an isolated BrowserContext, created for this specific test.
// "page" belongs to this context.
});
test('another test', async ({ page, context }) => {
// "context" and "page" in this second test are completely
// isolated from the first test.
});
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
浏览器上下文还可用于模拟涉及移动设备、权限、区域设置和配色方案的多页面场景。请查看我们的 模拟 指南了解更多详细信息。
¥Browser contexts can also be used to emulate multi-page scenarios involving mobile devices, permissions, locale and color scheme. Check out our Emulation guide for more details.
单个测试中的多个上下文
¥Multiple Contexts in a Single Test
Playwright 可以在单个场景中创建多个浏览器上下文。当你想要测试多用户功能(例如聊天)时,这非常有用。
¥Playwright can create multiple browser contexts within a single scenario. This is useful when you want to test for multi-user functionality, like a chat.
- Test
- Library
import { test } from '@playwright/test';
test('admin and user', async ({ browser }) => {
// Create two isolated browser contexts
const adminContext = await browser.newContext();
const userContext = await browser.newContext();
// Create pages and interact with contexts independently
const adminPage = await adminContext.newPage();
const userPage = await userContext.newPage();
});
const { chromium } = require('playwright');
// Create a Chromium browser instance
const browser = await chromium.launch();
// Create two isolated browser contexts
const userContext = await browser.newContext();
const adminContext = await browser.newContext();
// Create pages and interact with contexts independently
const adminPage = await adminContext.newPage();
const userPage = await userContext.newPage();