Skip to main content

编写测试

介绍

🌐 Introduction

Playwright 的断言是专门为动态网页创建的。检查会自动重试,直到满足必要的条件。Playwright 内置了自动等待,意味着它会在执行操作前等待元素可操作。Playwright 提供了 assertThat 重载方法来编写断言。

🌐 Playwright assertions are created specifically for the dynamic web. Checks are automatically retried until the necessary conditions are met. Playwright comes with auto-wait built in meaning it waits for elements to be actionable prior to performing actions. Playwright provides assertThat overloads to write assertions.

查看下面的示例测试,了解如何使用 Web 优先断言、定位器和选择器编写测试。

🌐 Take a look at the example test below to see how to write a test using web first assertions, locators and selectors.

package org.example;

import java.util.regex.Pattern;
import com.microsoft.playwright.*;
import com.microsoft.playwright.options.AriaRole;

import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;

public class App {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch();
Page page = browser.newPage();
page.navigate("https://playwright.nodejs.cn");

// Expect a title "to contain" a substring.
assertThat(page).hasTitle(Pattern.compile("Playwright"));

// create a locator
Locator getStarted = page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("Get Started"));

// Expect an attribute "to be strictly equal" to the value.
assertThat(getStarted).hasAttribute("href", "/docs/intro");

// Click the get started link.
getStarted.click();

// Expects page to have a heading with the name of Installation.
assertThat(page.getByRole(AriaRole.HEADING,
new Page.GetByRoleOptions().setName("Installation"))).isVisible();
}
}
}

断言

🌐 Assertions

Playwright 提供了 assertThat 重载,它将等待直到满足预期条件。

🌐 Playwright provides assertThat overloads which will wait until the expected condition is met.

import java.util.regex.Pattern;
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;

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

定位器

🌐 Locators

定位器 是 Playwright 自动等待和重试功能的核心部分。定位器表示一种在任意时刻查找页面元素的方法,并用于对元素执行操作,例如 .click.fill 等。可以使用 Page.locator() 方法创建自定义定位器。

import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;

Locator getStarted = page.locator("text=Get Started");

assertThat(getStarted).hasAttribute("href", "/docs/intro");
getStarted.click();

Playwright 支持多种不同的定位器,例如 roletexttest id 等。了解更多可用的定位器以及如何选择合适定位器,请参阅这篇 深入指南

🌐 Playwright supports many different locators like role text, test id and many more. Learn more about available locators and how to pick one in this in-depth guide.

import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;

assertThat(page.locator("text=Installation")).isVisible();

测试隔离

🌐 Test Isolation

Playwright 有一个 BrowserContext 的概念,它是一个内存中的独立浏览器配置文件。建议为每个测试创建一个新的 BrowserContext,以确保测试之间不会互相干扰。

🌐 Playwright has the concept of a BrowserContext which is an in-memory isolated browser profile. It's recommended to create a new BrowserContext for each test to ensure they don't interfere with each other.

Browser browser = playwright.chromium().launch();
BrowserContext context = browser.newContext();
Page page = context.newPage();

下一步是什么

🌐 What's Next