跳至主要内容

JUnit (实验性)

简介

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

JUnit中,您可以使用Playwright的fixtures来自动初始化PlaywrightBrowserBrowserContextPage。在下面的示例中,所有三个测试方法都使用相同的Browser。每个测试使用自己的BrowserContextPage

package org.example;

import com.microsoft.playwright.Page;
import com.microsoft.playwright.junit.UsePlaywright;
import org.junit.jupiter.api.Test;

import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

@UsePlaywright
public class TestExample {
@Test
void shouldClickButton(Page page) {
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 page) {
page.setContent("<input id='checkbox' type='checkbox'></input>");
page.locator("input").check();
assertEquals(true, page.evaluate("window['checkbox'].checked"));
}

@Test
void shouldSearchWiki(Page page) {
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");
assertThat(page).hasURL("https://en.wikipedia.org/wiki/Playwright");
}
}

测试固件

只需在测试类上添加JUnit注解@UsePlaywright即可启用Playwright fixtures。测试fixtures用于为每个测试建立环境,为测试提供所需的一切且仅此而已。

@UsePlaywright
public class TestExample {

@Test
void basicTest(Page page) {
page.navigate("https://playwright.dev/");

assertThat(page).hasTitle(Pattern.compile("Playwright"));
}
}

Page page参数告诉JUnit设置page夹具并将其提供给您的测试方法。

以下是预定义夹具的列表:

FixtureTypeDescription
pagePageIsolated page for this test run.
browserContextBrowserContextIsolated context for this test run. The page fixture belongs to this context as well.
browserBrowser浏览器在测试间共享以优化资源。
playwrightPlaywrightPlaywright实例在同一线程运行的测试间共享。
requestAPIRequestContext本次测试运行的独立APIRequestContext。了解如何进行API测试

自定义选项

要自定义fixture选项,您应该实现一个OptionsFactory并在@UsePlaywright()注解中指定该类。

你可以轻松覆盖BrowserType.launch()的启动选项,或者Browser.newContext()APIRequest.newContext()的上下文选项。请参考以下示例:

import com.microsoft.playwright.junit.Options;
import com.microsoft.playwright.junit.OptionsFactory;
import com.microsoft.playwright.junit.UsePlaywright;

@UsePlaywright(MyTest.CustomOptions.class)
public class MyTest {

public static class CustomOptions implements OptionsFactory {
@Override
public Options getOptions() {
return new Options()
.setHeadless(false)
.setContextOption(new Browser.NewContextOptions()
.setBaseURL("https://github.com"))
.setApiRequestOptions(new APIRequest.NewContextOptions()
.setBaseURL("https://playwright.dev"));
}
}

@Test
public void testWithCustomOptions(Page page, APIRequestContext request) {
page.navigate("/");
assertThat(page).hasURL(Pattern.compile("github"));

APIResponse response = request.get("/");
assertTrue(response.text().contains("Playwright"));
}
}

并行运行测试

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

@UsePlaywright
class Test1 {
@Test
void shouldClickButton(Page page) {
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 page) {
page.setContent("<input id='checkbox' type='checkbox'></input>");
page.locator("input").check();
assertEquals(true, page.evaluate("window['checkbox'].checked"));
}

@Test
void shouldSearchWiki(Page page) {
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");
assertThat(page).hasURL("https://en.wikipedia.org/wiki/Playwright");
}
}

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

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

配置JUnit以在每个类中顺序运行测试,并在并行线程上运行多个类(最大线程数等于CPU核心数的1/2):

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