跳到主要内容

测试运行器

简介

使用几行代码,您可以将 Playwright 连接到您最喜欢的 Java 测试运行器。

Playwright 和浏览器实例可以在测试之间重复使用以提高性能。我们建议在新的 BrowserContext 中运行每个测试用例,这样浏览器状态将在测试之间隔离。

JUnit

JUnit 中,您可以在 @BeforeAll 方法中初始化 Playwright浏览器,并在 @AfterAll 中销毁它们。在下面的示例中,所有三个测试方法都使用相同的 浏览器。每个测试都使用自己的 BrowserContext页面

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());
}
}

查看实验性的 JUnit 集成,以自动初始化 Playwright 对象等等。

并行运行测试

默认情况下,JUnit 会在单个线程上顺序运行所有测试。从 JUnit 5.3 开始,您可以更改此行为以并行运行测试以加快执行速度(参见 此页面)。由于在没有额外同步的情况下从多个线程使用相同的 Playwright 对象是不安全的,我们建议您为每个线程创建一个 Playwright 实例,并在该线程上独占使用它。以下是如何并行运行多个测试类的示例。

使用 @TestInstance(TestInstance.Lifecycle.PER_CLASS) 注释,使 JUnit 为该类中的所有测试方法创建一个类的实例(默认情况下,每个 JUnit 将为每个测试方法创建一个新的类的实例)。将 Playwright浏览器 对象存储在实例字段中。它们将在测试之间共享。类的每个实例将使用自己的 Playwright 副本。

// Subclasses will inherit PER_CLASS behavior.
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class TestFixtures {
// Shared between all tests in the class.
Playwright playwright;
Browser browser;

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

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

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

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

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

class Test1 extends TestFixtures {
@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());
}
}

class Test2 extends TestFixtures {
@Test
void shouldReturnInnerHTML() {
page.setContent("<div>hello</div>");
assertEquals("hello", page.innerHTML("css=div"));
}

@Test
void shouldClickButton() {
Page popup = page.waitForPopup(() -> {
page.evaluate("window.open('about:blank');");
});
assertEquals("about:blank", popup.url());
}
}

配置 JUnit 以顺序运行每个类中的测试,并在并行线程上运行多个类(线程最大数量等于 CPU 内核数量的一半)

junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = same_thread
junit.jupiter.execution.parallel.mode.classes.default = concurrent
junit.jupiter.execution.parallel.config.strategy=dynamic
junit.jupiter.execution.parallel.config.dynamic.factor=0.5

使用 Gradle

您可以使用用 Groovy 或 Kotlin 编写的 Gradle 构建配置脚本。

plugins {
application
id 'java'
}

repositories {
mavenCentral()
}

dependencies {
implementation 'com.microsoft.playwright:playwright:1.48.0'
}

application {
mainClass = 'org.example.App'
}

// Usage: ./gradlew playwright --args="help"
task playwright(type: JavaExec) {
classpath sourceSets.test.runtimeClasspath
mainClass = 'com.microsoft.playwright.CLI'
}

test {
useJUnitPlatform()
}

然后可以按如下方式启动测试

./gradlew run

此外,Playwright 命令行工具可以使用以下命令运行:

./gradlew playwright --args="help"

TestNG

TestNG 中,您可以在 @BeforeClass 方法中初始化 Playwright浏览器,并在 @AfterClass 中销毁它们。在下面的示例中,所有三个测试方法都使用相同的 浏览器。每个测试都使用自己的 BrowserContext页面

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.testng.annotations.*;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

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

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

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

@AfterClass
void closeBrowser() {
playwright.close();
}

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

@AfterMethod
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());
}
}