Skip to main content

运行和调试测试

介绍

¥Introduction

Playwright 测试可以通过多种方式运行。我们建议将其连接到你最喜欢的测试运行器(例如 JUnit),因为它使你能够并行运行测试、运行单个测试等。

¥Playwright tests can be run in a variety of ways. We recommend hooking it up to your favorite test runner, e.g., JUnit, since it gives you the ability to run tests in parallel, run single test, etc.

你可以运行单个测试、一组测试或所有测试。测试可以在一个或多个浏览器上运行。默认情况下,测试以无头模式运行,这意味着运行测试时不会打开浏览器窗口,结果将显示在终端中。如果你愿意,你可以使用 launch(new BrowserType.LaunchOptions().setHeadless(false)) 选项在有头模式下运行测试。

¥You can run a single test, a set of tests or all tests. Tests can be run on one browser or multiple browsers. By default tests are run in a headless manner meaning no browser window will be opened while running the tests and results will be seen in the terminal. If you prefer, you can run your tests in headed mode by using the launch(new BrowserType.LaunchOptions().setHeadless(false)) option.

JUnit 中,你可以在 @BeforeAll 方法中初始化 PlaywrightBrowser,并在 @AfterAll 方法中销毁它们。在下面的示例中,所有三个测试方法都使用同一个 Browser。每个测试使用自己的 BrowserContextPage

¥In JUnit, you can initialize Playwright and Browser in @BeforeAll method and destroy them in @AfterAll. In the example below, all three test methods use the same Browser. Each test uses its own BrowserContext and Page.

package org.example;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import org.junit.jupiter.api.*;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestExample {
// Shared between all tests in this class.
static Playwright playwright;
static Browser browser;

// New instance for each test method.
BrowserContext context;
Page page;

@BeforeAll
static void launchBrowser() {
playwright = Playwright.create();
browser = playwright.chromium().launch();
}

@AfterAll
static void closeBrowser() {
playwright.close();
}

@BeforeEach
void createContextAndPage() {
context = browser.newContext();
page = context.newPage();
}

@AfterEach
void closeContext() {
context.close();
}

@Test
void shouldClickButton() {
page.navigate("data:text/html,<script>var result;</script><button onclick='result=\"Clicked\"'>Go</button>");
page.locator("button").click();
assertEquals("Clicked", page.evaluate("result"));
}

@Test
void shouldCheckTheBox() {
page.setContent("<input id='checkbox' type='checkbox'></input>");
page.locator("input").check();
assertTrue((Boolean) page.evaluate("() => window['checkbox'].checked"));
}

@Test
void shouldSearchWiki() {
page.navigate("https://www.wikipedia.org/");
page.locator("input[name=\"search\"]").click();
page.locator("input[name=\"search\"]").fill("playwright");
page.locator("input[name=\"search\"]").press("Enter");
assertEquals("https://en.wikipedia.org/wiki/Playwright", page.url());
}
}

有关如何并行运行测试等的更多详细信息,请参阅 此处

¥See here for further details on how to run tests in parallel, etc.

请参阅实验性 JUnit 集成,了解如何自动初始化 Playwright 对象等。

¥See experimental JUnit integration to automatically initialize Playwright objects and more.

在 Head 模式下运行测试

¥Run tests in headed mode

如果你愿意,你可以使用 launch(new BrowserType.LaunchOptions().setHeadless(false)) 选项在有头模式下运行测试。

¥If you prefer, you can run your tests in headed mode by using the launch(new BrowserType.LaunchOptions().setHeadless(false)) option.

下一步是什么

¥What's Next