Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feat: Support intervals arg for toPass (#30037)

Hansantoa950a7b86b39
diff --git a/docs/src/test-api/class-testconfig.md b/docs/src/test-api/class-testconfig.md
index a5854ef4e3..0df43c6d0a 100644
--- a/docs/src/test-api/class-testconfig.md
+++ b/docs/src/test-api/class-testconfig.md
@@ -54,6 +54,7 @@ export default defineConfig({
- `maxDiffPixelRatio` ?<[float]> an acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1` , unset by default.
- `toPass` ?<[Object]> Configuration for the [expect(value).toPass()](../test-assertions.md#expecttopass) method.
- `timeout` ?<[int]> timeout for toPass method in milliseconds.
+ - `intervals` ?<[Array]<[int]>> probe intervals for toPass method in milliseconds.
Configuration for the `expect` assertion library. Learn more about [various timeouts](../test-timeouts.md).
diff --git a/docs/src/test-api/class-testproject.md b/docs/src/test-api/class-testproject.md
index 87e5b58aa7..2cb6a77938 100644
--- a/docs/src/test-api/class-testproject.md
+++ b/docs/src/test-api/class-testproject.md
@@ -104,6 +104,7 @@ export default defineConfig({
- `maxDiffPixelRatio` ?<[float]> an acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1` , unset by default.
- `toPass` ?<[Object]> Configuration for the [expect(value).toPass()](../test-assertions.md) method.
- `timeout` ?<[int]> timeout for toPass method in milliseconds.
+ - `intervals` ?<[Array]<[int]>> probe intervals for toPass method in milliseconds.
Configuration for the `expect` assertion library.
diff --git a/packages/playwright/src/matchers/matchers.ts b/packages/playwright/src/matchers/matchers.ts
index 01d1dfad25..c7a5ebde7a 100644
--- a/packages/playwright/src/matchers/matchers.ts
+++ b/packages/playwright/src/matchers/matchers.ts
@@ -369,6 +369,7 @@ export async function toPass(
) {
const testInfo = currentTestInfo();
const timeout = takeFirst(options.timeout, testInfo?._projectInternal.expect?.toPass?.timeout, 0);
+ const intervals = takeFirst(options.intervals, testInfo?._projectInternal.expect?.toPass?.intervals, [100, 250, 500, 1000]);
const { deadline, timeoutMessage } = testInfo ? testInfo._deadlineForMatcher(timeout) : TestInfoImpl._defaultDeadlineForMatcher(timeout);
const result = await pollAgainstDeadline<Error|undefined>(async () => {
@@ -380,7 +381,7 @@ export async function toPass(
} catch (e) {
return { continuePolling: !this.isNot, result: e };
}
- }, deadline, options.intervals || [100, 250, 500, 1000]);
+ }, deadline, intervals);
if (result.timedOut) {
const message = result.result ? [
diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.ts
index eeec7cef46..8bdfe088c8 100644
--- a/packages/playwright/types/test.d.ts
+++ b/packages/playwright/types/test.d.ts
@@ -702,6 +702,11 @@ interface TestConfig {
* timeout for toPass method in milliseconds.
*/
timeout?: number;
+
+ /**
+ * probe intervals for toPass method in milliseconds.
+ */
+ intervals?: Array<number>;
};
};
@@ -8597,6 +8602,11 @@ interface TestProject {
* timeout for toPass method in milliseconds.
*/
timeout?: number;
+
+ /**
+ * probe intervals for toPass method in milliseconds.
+ */
+ intervals?: Array<number>;
};
};
diff --git a/tests/playwright-test/expect-to-pass.spec.ts b/tests/playwright-test/expect-to-pass.spec.ts
index 2ad00e0ce7..c3d16639db 100644
--- a/tests/playwright-test/expect-to-pass.spec.ts
+++ b/tests/playwright-test/expect-to-pass.spec.ts
@@ -260,3 +260,41 @@ test('should give priority to timeout parameter over timeout in config file', as
4 | await test.expect(() => {
`.trim());
});
+
+test('should respect intervals in config file when intervals parameter is not passed', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'playwright.config.js': `module.exports = { expect: { toPass: { timeout: 2000, intervals: [100, 1000] } } }`,
+ 'a.spec.ts': `
+ import { test, expect } from '@playwright/test';
+ test('should fail', async () => {
+ let attempt = 0;
+ await test.expect(() => {
+ expect(++attempt).toBe(-1);
+ }).toPass();
+ });
+ `
+ });
+ expect(result.exitCode).toBe(1);
+ expect(result.output).toContain('Error: expect(received).toBe(expected) // Object.is equality');
+ expect(result.output).toContain('Expected: -1');
+ expect(result.output).toContain('Received: 3');
+});
+
+test('should give priority to intervals parameter over intervals in config file', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'playwright.config.js': `module.exports = { expect: { toPass: { timeout: 2000, intervals: [100] } } }`,
+ 'a.spec.ts': `
+ import { test, expect } from '@playwright/test';
+ test('should fail', async () => {
+ let attempt = 0;
+ await test.expect(() => {
+ expect(++attempt).toBe(-1);
+ }).toPass({ intervals: [100, 1000] });
+ });
+ `
+ });
+ expect(result.exitCode).toBe(1);
+ expect(result.output).toContain('Error: expect(received).toBe(expected) // Object.is equality');
+ expect(result.output).toContain('Expected: -1');
+ expect(result.output).toContain('Received: 3');
+});