PlaywrightAssertions
Playwright 为你提供了 Web 优先断言以及创建断言的便捷方法,这些断言将等待并重试,直到满足预期条件。
¥Playwright gives you Web-First Assertions with convenience methods for creating assertions that will wait and retry until the expected condition is met.
考虑以下示例:
¥Consider the following example:
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
public class TestExample {
// ...
@Test
void statusBecomesSubmitted() {
// ...
page.locator("#submit-button").click();
assertThat(page.locator(".status")).hasText("Submitted");
}
}
Playwright 将使用选择器 .status
重新测试节点,直到获取的节点具有 "Submitted"
文本。它将重新获取节点并一遍又一遍地检查它,直到满足条件或达到超时。你可以将此超时作为一个选项传递。
¥Playwright will be re-testing the node with the selector .status
until fetched Node has the "Submitted"
text. It will be re-fetching the node and checking it over and over, until the condition is met or until the timeout is reached. You can pass this timeout as an option.
默认情况下,断言超时设置为 5 秒。
¥By default, the timeout for assertions is set to 5 seconds.
方法
¥Methods
assertThat(response)
Added in: v1.18为给定的 APIResponse 创建一个 APIResponseAssertions 对象。
¥Creates a APIResponseAssertions object for the given APIResponse.
用法
¥Usage
PlaywrightAssertions.assertThat(response).isOK();
参数
¥Arguments
response
APIResponse#
用于断言的 APIResponse 对象。
¥APIResponse object to use for assertions.
返回
¥Returns
assertThat(locator)
Added in: v1.18为给定的 Locator 创建一个 LocatorAssertions 对象。
¥Creates a LocatorAssertions object for the given Locator.
用法
¥Usage
PlaywrightAssertions.assertThat(locator).isVisible();
参数
¥Arguments
用于断言的 Locator 对象。
¥Locator object to use for assertions.
返回
¥Returns
assertThat(page)
Added in: v1.18为给定的 Page 创建一个 PageAssertions 对象。
¥Creates a PageAssertions object for the given Page.
用法
¥Usage
PlaywrightAssertions.assertThat(page).hasTitle("News");
参数
¥Arguments
用于断言的 Page 对象。
¥Page object to use for assertions.
返回
¥Returns
setDefaultAssertionTimeout
Added in: v1.25将 Playwright 断言的默认超时时间从 5 秒更改为指定值。
¥Changes default timeout for Playwright assertions from 5 seconds to the specified value.
用法
¥Usage
PlaywrightAssertions.setDefaultAssertionTimeout(30_000);
参数
¥Arguments
超时(以毫秒为单位)。
¥Timeout in milliseconds.