Skip to content

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

chore: relayout matchers (#7901)

Pavel Feldman9e0702997346
diff --git a/src/test/expect.ts b/src/test/expect.ts
index fbf061fa45..815d8c2c4b 100644
--- a/src/test/expect.ts
+++ b/src/test/expect.ts
@@ -24,20 +24,19 @@ import {
toBeFocused,
toBeHidden,
toBeSelected,
- toBeVisible
-} from './matchers/toBeTruthy';
-import { toHaveLength, toHaveProp } from './matchers/toEqual';
-import { toMatchSnapshot } from './matchers/toMatchSnapshot';
-import {
+ toBeVisible,
toContainText,
toHaveAttr,
- toHaveCSS,
toHaveClass,
+ toHaveCSS,
toHaveData,
toHaveId,
+ toHaveLength,
+ toHaveProp,
toHaveText,
toHaveValue
-} from './matchers/toMatchText';
+} from './matchers/matchers';
+import { toMatchSnapshot } from './matchers/toMatchSnapshot';
import type { Expect } from './types';
export const expect: Expect = expectLibrary as any;
@@ -54,8 +53,8 @@ expectLibrary.extend({
toBeVisible,
toContainText,
toHaveAttr,
- toHaveCSS,
toHaveClass,
+ toHaveCSS,
toHaveData,
toHaveId,
toHaveLength,
diff --git a/src/test/matchers/matchers.ts b/src/test/matchers/matchers.ts
new file mode 100644
index 0000000000..73ce4bf877
--- /dev/null
+++ b/src/test/matchers/matchers.ts
@@ -0,0 +1,242 @@
+/**
+ * Copyright Microsoft Corporation. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import matchers from 'expect/build/matchers';
+import { Locator } from '../../..';
+import type { Expect } from '../types';
+import { toBeTruthy } from './toBeTruthy';
+import { toEqual } from './toEqual';
+import { toMatchText } from './toMatchText';
+
+export async function toBeChecked(
+ this: ReturnType<Expect['getState']>,
+ locator: Locator,
+ options?: { timeout?: number },
+) {
+ return toBeTruthy.call(this, 'toBeChecked', locator, async timeout => {
+ return await locator.isChecked({ timeout });
+ }, options);
+}
+
+export async function toBeDisabled(
+ this: ReturnType<Expect['getState']>,
+ locator: Locator,
+ options?: { timeout?: number },
+) {
+ return toBeTruthy.call(this, 'toBeDisabled', locator, async timeout => {
+ return await locator.isDisabled({ timeout });
+ }, options);
+}
+
+export async function toBeEditable(
+ this: ReturnType<Expect['getState']>,
+ locator: Locator,
+ options?: { timeout?: number },
+) {
+ return toBeTruthy.call(this, 'toBeEditable', locator, async timeout => {
+ return await locator.isEditable({ timeout });
+ }, options);
+}
+
+export async function toBeEmpty(
+ this: ReturnType<Expect['getState']>,
+ locator: Locator,
+ options?: { timeout?: number },
+) {
+ return toBeTruthy.call(this, 'toBeEmpty', locator, async timeout => {
+ return await locator.evaluate(element => {
+ if (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA')
+ return !(element as HTMLInputElement).value;
+ return !element.textContent?.trim();
+ }, { timeout });
+ }, options);
+}
+
+export async function toBeEnabled(
+ this: ReturnType<Expect['getState']>,
+ locator: Locator,
+ options?: { timeout?: number },
+) {
+ return toBeTruthy.call(this, 'toBeEnabled', locator, async timeout => {
+ return await locator.isEnabled({ timeout });
+ }, options);
+}
+
+export async function toBeFocused(
+ this: ReturnType<Expect['getState']>,
+ locator: Locator,
+ options?: { timeout?: number },
+) {
+ return toBeTruthy.call(this, 'toBeFocused', locator, async timeout => {
+ return await locator.evaluate(element => {
+ return document.activeElement === element;
+ }, { timeout });
+ }, options);
+}
+
+export async function toBeHidden(
+ this: ReturnType<Expect['getState']>,
+ locator: Locator,
+ options?: { timeout?: number },
+) {
+ return toBeTruthy.call(this, 'toBeHidden', locator, async timeout => {
+ return await locator.isHidden({ timeout });
+ }, options);
+}
+
+export async function toBeSelected(
+ this: ReturnType<Expect['getState']>,
+ locator: Locator,
+ options?: { timeout?: number },
+) {
+ return toBeTruthy.call(this, 'toBeSelected', locator, async timeout => {
+ return await locator.evaluate(element => {
+ return (element as HTMLOptionElement).selected;
+ }, { timeout });
+ }, options);
+}
+
+export async function toBeVisible(
+ this: ReturnType<Expect['getState']>,
+ locator: Locator,
+ options?: { timeout?: number },
+) {
+ return toBeTruthy.call(this, 'toBeVisible', locator, async timeout => {
+ return await locator.isVisible({ timeout });
+ }, options);
+}
+
+export async function toContainText(
+ this: ReturnType<Expect['getState']>,
+ locator: Locator,
+ expected: string,
+ options?: { timeout?: number, useInnerText?: boolean },
+) {
+ return toMatchText.call(this, 'toContainText', locator, async timeout => {
+ if (options?.useInnerText)
+ return await locator.innerText({ timeout });
+ return await locator.textContent() || '';
+ }, expected, { ...options, matchSubstring: true });
+}
+
+export async function toHaveAttr(
+ this: ReturnType<Expect['getState']>,
+ locator: Locator,
+ name: string,
+ expected: string | RegExp,
+ options?: { timeout?: number },
+) {
+ return toMatchText.call(this, 'toHaveAttr', locator, async timeout => {
+ return await locator.getAttribute(name, { timeout }) || '';
+ }, expected, options);
+}
+
+export async function toHaveClass(
+ this: ReturnType<Expect['getState']>,
+ locator: Locator,
+ expected: string,
+ options?: { timeout?: number },
+) {
+ return toMatchText.call(this, 'toHaveClass', locator, async timeout => {
+ return await locator.evaluate(element => element.className, { timeout });
+ }, expected, { ...options, matchSubstring: true });
+}
+
+export async function toHaveCSS(
+ this: ReturnType<Expect['getState']>,
+ locator: Locator,
+ name: string,
+ expected: string | RegExp,
+ options?: { timeout?: number },
+) {
+ return toMatchText.call(this, 'toHaveCSS', locator, async timeout => {
+ return await locator.evaluate(async (element, name) => {
+ return (window.getComputedStyle(element) as any)[name];
+ }, name, { timeout });
+ }, expected, options);
+}
+
+export async function toHaveData(
+ this: ReturnType<Expect['getState']>,
+ locator: Locator,
+ name: string,
+ expected: string | RegExp,
+ options?: { timeout?: number },
+) {
+ return toMatchText.call(this, 'toHaveData', locator, async timeout => {
+ return await locator.getAttribute('data-' + name, { timeout }) || '';
+ }, expected, options);
+}
+
+export async function toHaveId(
+ this: ReturnType<Expect['getState']>,
+ locator: Locator,
+ expected: string | RegExp,
+ options?: { timeout?: number },
+) {
+ return toMatchText.call(this, 'toHaveId', locator, async timeout => {
+ return await locator.getAttribute('id', { timeout }) || '';
+ }, expected, options);
+}
+
+export async function toHaveLength(
+ this: ReturnType<Expect['getState']>,
+ locator: Locator,
+ expected: number,
+ options?: { timeout?: number },
+) {
+ if (typeof locator !== 'object' || locator.constructor.name !== 'Locator')
+ return matchers.toHaveLength.call(this, locator, expected);
+ return toEqual.call(this, 'toHaveLength', locator, async timeout => {
+ return await locator.count();
+ }, expected, { expectedType: 'number', ...options });
+}
+
+export async function toHaveProp(
+ this: ReturnType<Expect['getState']>,
+ locator: Locator,
+ name: string,
+ expected: number,
+ options?: { timeout?: number },
+) {
+ return toEqual.call(this, 'toHaveProp', locator, async timeout => {
+ return await locator.evaluate((element, name) => (element as any)[name], name, { timeout });
+ }, expected, { expectedType: 'number', ...options });
+}
+
+export async function toHaveText(
+ this: ReturnType<Expect['getState']>,
+ locator: Locator,
+ expected: string | RegExp,
+ options?: { timeout?: number, useInnerText?: boolean },
+) {
+ return toMatchText.call(this, 'toHaveText', locator, async timeout => {
+ if (options?.useInnerText)
+ return await locator.innerText({ timeout });
+ return await locator.textContent() || '';
+ }, expected, options);
+}
+
+export async function toHaveValue(
+ this: ReturnType<Expect['getState']>,
+ locator: Locator,
+ expected: string | RegExp,
+ options?: { timeout?: number },
+) {
+ return toMatchText.call(this, 'toHaveValue', locator, async timeout => {
+ return await locator.inputValue({ timeout });
+ }, expected, options);
+}
diff --git a/src/test/matchers/toBeTruthy.ts b/src/test/matchers/toBeTruthy.ts
index 0b7b708c6c..8c1b3404bd 100644
--- a/src/test/matchers/toBeTruthy.ts
+++ b/src/test/matchers/toBeTruthy.ts
@@ -23,7 +23,7 @@ import { currentTestInfo } from '../globals';
import type { Expect } from '../types';
import { expectLocator, monotonicTime, pollUntilDeadline } from '../util';
-async function toBeTruthyImpl<T>(
+export async function toBeTruthy<T>(
this: ReturnType<Expect['getState']>,
matcherName: string,
locator: Locator,
@@ -59,101 +59,3 @@ async function toBeTruthyImpl<T>(
return { message, pass };
}
-
-export async function toBeChecked(
- this: ReturnType<Expect['getState']>,
- locator: Locator,
- options?: { timeout?: number },
-) {
- return toBeTruthyImpl.call(this, 'toBeChecked', locator, async timeout => {
- return await locator.isChecked({ timeout });
- }, options);
-}
-
-export async function toBeEditable(
- this: ReturnType<Expect['getState']>,
- locator: Locator,
- options?: { timeout?: number },
-) {
- return toBeTruthyImpl.call(this, 'toBeEditable', locator, async timeout => {
- return await locator.isEditable({ timeout });
- }, options);
-}
-
-export async function toBeEnabled(
- this: ReturnType<Expect['getState']>,
- locator: Locator,
- options?: { timeout?: number },
-) {
- return toBeTruthyImpl.call(this, 'toBeEnabled', locator, async timeout => {
- return await locator.isEnabled({ timeout });
- }, options);
-}
-
-export async function toBeDisabled(
- this: ReturnType<Expect['getState']>,
- locator: Locator,
- options?: { timeout?: number },
-) {
- return toBeTruthyImpl.call(this, 'toBeDisabled', locator, async timeout => {
- return await locator.isDisabled({ timeout });
- }, options);
-}
-
-export async function toBeEmpty(
- this: ReturnType<Expect['getState']>,
- locator: Locator,
- options?: { timeout?: number },
-) {
- return toBeTruthyImpl.call(this, 'toBeEmpty', locator, async timeout => {
- return await locator.evaluate(element => {
- if (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA')
- return !(element as HTMLInputElement).value;
- return !element.textContent?.trim();
- }, { timeout });
- }, options);
-}
-
-export async function toBeHidden(
- this: ReturnType<Expect['getState']>,
- locator: Locator,
- options?: { timeout?: number },
-) {
- return toBeTruthyImpl.call(this, 'toBeHidden', locator, async timeout => {
- return await locator.isHidden({ timeout });
- }, options);
-}
-
-export async function toBeVisible(
- this: ReturnType<Expect['getState']>,
- locator: Locator,
- options?: { timeout?: number },
-) {
- return toBeTruthyImpl.call(this, 'toBeVisible', locator, async timeout => {
- return await locator.isVisible({ timeout });
- }, options);
-}
-
-export async function toBeFocused(
- this: ReturnType<Expect['getState']>,
- locator: Locator,
- options?: { timeout?: number },
-) {
- return toBeTruthyImpl.call(this, 'toBeFocused', locator, async timeout => {
- return await locator.evaluate(element => {
- return document.activeElement === element;
- }, { timeout });
- }, options);
-}
-
-export async function toBeSelected(
- this: ReturnType<Expect['getState']>,
- locator: Locator,
- options?: { timeout?: number },
-) {
- return toBeTruthyImpl.call(this, 'toBeSelected', locator, async timeout => {
- return await locator.evaluate(element => {
- return (element as HTMLOptionElement).selected;
- }, { timeout });
- }, options);
-}
diff --git a/src/test/matchers/toEqual.ts b/src/test/matchers/toEqual.ts
index d20a1fc3db..bb72df8630 100644
--- a/src/test/matchers/toEqual.ts
+++ b/src/test/matchers/toEqual.ts
@@ -15,7 +15,6 @@
*/
import { equals } from 'expect/build/jasmineUtils';
-import matchers from 'expect/build/matchers';
import {
iterableEquality
} from 'expect/build/utils';
@@ -38,7 +37,7 @@ const RECEIVED_LABEL = 'Received';
// The optional property of matcher context is true if undefined.
const isExpand = (expand?: boolean): boolean => expand !== false;
-async function toEqualImpl<T>(
+export async function toEqual<T>(
this: ReturnType<Expect['getState']>,
matcherName: string,
locator: Locator,
@@ -94,28 +93,3 @@ async function toEqualImpl<T>(
// or create a different error message
return { actual: received, expected, message, name: matcherName, pass };
}
-
-export async function toHaveLength(
- this: ReturnType<Expect['getState']>,
- locator: Locator,
- expected: number,
- options?: { timeout?: number },
-) {
- if (typeof locator !== 'object' || locator.constructor.name !== 'Locator')
- return matchers.toHaveLength.call(this, locator, expected);
- return toEqualImpl.call(this, 'toHaveLength', locator, async timeout => {
- return await locator.count();
- }, expected, { expectedType: 'number', ...options });
-}
-
-export async function toHaveProp(
- this: ReturnType<Expect['getState']>,
- locator: Locator,
- name: string,
- expected: number,
- options?: { timeout?: number },
-) {
- return toEqualImpl.call(this, 'toHaveProp', locator, async timeout => {
- return await locator.evaluate((element, name) => (element as any)[name], name, { timeout });
- }, expected, { expectedType: 'number', ...options });
-}
diff --git a/src/test/matchers/toMatchText.ts b/src/test/matchers/toMatchText.ts
index 4254625dfe..b1e8afdd1d 100644
--- a/src/test/matchers/toMatchText.ts
+++ b/src/test/matchers/toMatchText.ts
@@ -33,7 +33,7 @@ import { currentTestInfo } from '../globals';
import type { Expect } from '../types';
import { expectLocator, monotonicTime, pollUntilDeadline } from '../util';
-async function toMatchTextImpl(
+export async function toMatchText(
this: ReturnType<Expect['getState']>,
matcherName: string,
locator: Locator,
@@ -122,99 +122,3 @@ async function toMatchTextImpl(
return { message, pass };
}
-
-export async function toHaveText(
- this: ReturnType<Expect['getState']>,
- locator: Locator,
- expected: string | RegExp,
- options?: { timeout?: number, useInnerText?: boolean },
-) {
- return toMatchTextImpl.call(this, 'toHaveText', locator, async timeout => {
- if (options?.useInnerText)
- return await locator.innerText({ timeout });
- return await locator.textContent() || '';
- }, expected, options);
-}
-
-export async function toContainText(
- this: ReturnType<Expect['getState']>,
- locator: Locator,
- expected: string,
- options?: { timeout?: number, useInnerText?: boolean },
-) {
- return toMatchTextImpl.call(this, 'toContainText', locator, async timeout => {
- if (options?.useInnerText)
- return await locator.innerText({ timeout });
- return await locator.textContent() || '';
- }, expected, { ...options, matchSubstring: true });
-}
-
-export async function toHaveAttr(
- this: ReturnType<Expect['getState']>,
- locator: Locator,
- name: string,
- expected: string | RegExp,
- options?: { timeout?: number },
-) {
- return toMatchTextImpl.call(this, 'toHaveAttr', locator, async timeout => {
- return await locator.getAttribute(name, { timeout }) || '';
- }, expected, options);
-}
-
-export async function toHaveData(
- this: ReturnType<Expect['getState']>,
- locator: Locator,
- name: string,
- expected: string | RegExp,
- options?: { timeout?: number },
-) {
- return toMatchTextImpl.call(this, 'toHaveData', locator, async timeout => {
- return await locator.getAttribute('data-' + name, { timeout }) || '';
- }, expected, options);
-}
-
-export async function toHaveCSS(
- this: ReturnType<Expect['getState']>,
- locator: Locator,
- name: string,
- expected: string | RegExp,
- options?: { timeout?: number },
-) {
- return toMatchTextImpl.call(this, 'toHaveCSS', locator, async timeout => {
- return await locator.evaluate(async (element, name) => {
- return (window.getComputedStyle(element) as any)[name];
- }, name, { timeout });
- }, expected, options);
-}
-
-export async function toHaveId(
- this: ReturnType<Expect['getState']>,
- locator: Locator,
- expected: string | RegExp,
- options?: { timeout?: number },
-) {
- return toMatchTextImpl.call(this, 'toHaveId', locator, async timeout => {
- return await locator.getAttribute('id', { timeout }) || '';
- }, expected, options);
-}
-
-export async function toHaveValue(
- this: ReturnType<Expect['getState']>,
- locator: Locator,
- expected: string | RegExp,
- options?: { timeout?: number },
-) {
- return toMatchTextImpl.call(this, 'toHaveValue', locator, async timeout => {
- return await locator.inputValue({ timeout });
- }, expected, options);
-}
-export async function toHaveClass(
- this: ReturnType<Expect['getState']>,
- locator: Locator,
- expected: string,
- options?: { timeout?: number },
-) {
- return toMatchTextImpl.call(this, 'toHaveClass', locator, async timeout => {
- return await locator.evaluate(element => element.className, { timeout });
- }, expected, { ...options, matchSubstring: true });
-}
diff --git a/types/testExpect.d.ts b/types/testExpect.d.ts
index c04fbd8e67..80ef86cd13 100644
--- a/types/testExpect.d.ts
+++ b/types/testExpect.d.ts
@@ -70,84 +70,79 @@ declare global {
}): R;
/**
- * Asserts element's exact text content.
- */
- toHaveText(expected: string | RegExp, options?: { timeout?: number, useInnerText?: boolean }): Promise<R>;
-
- /**
- * Asserts element's text content matches given pattern or contains given substring.
+ * Asserts input is checked.
*/
- toContainText(expected: string, options?: { timeout?: number, useInnerText?: boolean }): Promise<R>;
+ toBeChecked(options?: { timeout?: number }): Promise<R>;
/**
- * Asserts element's attributes `name` matches expected value.
+ * Asserts input is disabled.
*/
- toHaveAttr(expected: string | RegExp, name: string, options?: { timeout?: number }): Promise<R>;
+ toBeDisabled(options?: { timeout?: number }): Promise<R>;
/**
- * Asserts element's data attribute data-`name` matches expected value.
+ * Asserts input is editable.
*/
- toHaveData(expected: string | RegExp, name: string, options?: { timeout?: number }): Promise<R>;
+ toBeEditable(options?: { timeout?: number }): Promise<R>;
/**
- * Asserts element's computed CSS property `name` matches expected value.
+ * Asserts given DOM node or input has no text content or no input value.
*/
- toHaveCSS(expected: string | RegExp, name: string, options?: { timeout?: number }): Promise<R>;
+ toBeEmpty(options?: { timeout?: number }): Promise<R>;
/**
- * Asserts element's `id` attribute matches expected value.
+ * Asserts input is enabled.
*/
- toHaveId(expected: string | RegExp, options?: { timeout?: number }): Promise<R>;
+ toBeEnabled(options?: { timeout?: number }): Promise<R>;
/**
- * Asserts input element's value.
+ * Asserts given DOM is a focused (active) in document.
*/
- toHaveValue(expected: string | RegExp, options?: { timeout?: number }): Promise<R>;
+ toBeFocused(options?: { timeout?: number }): Promise<R>;
/**
- * Asserts input is checked.
+ * Asserts given DOM node is hidden or detached from DOM.
*/
- toBeChecked(options?: { timeout?: number }): Promise<R>;
+ toBeHidden(options?: { timeout?: number }): Promise<R>;
/**
- * Asserts input is editable.
+ * Asserts given DOM node visible on the screen.
*/
- toBeEditable(options?: { timeout?: number }): Promise<R>;
+ toBeVisible(options?: { timeout?: number }): Promise<R>;
/**
- * Asserts input is enabled.
+ * Asserts given select option is selected
*/
- toBeEnabled(options?: { timeout?: number }): Promise<R>;
+ toBeSelected(options?: { timeout?: number }): Promise<R>;
/**
- * Asserts input is disabled.
+ * Asserts element's text content matches given pattern or contains given substring.
*/
- toBeDisabled(options?: { timeout?: number }): Promise<R>;
+ toContainText(expected: string, options?: { timeout?: number, useInnerText?: boolean }): Promise<R>;
/**
- * Asserts given DOM node or input has no text content or no input value.
+ * Asserts element's attributes `name` matches expected value.
*/
- toBeEmpty(options?: { timeout?: number }): Promise<R>;
+ toHaveAttr(expected: string | RegExp, name: string, options?: { timeout?: number }): Promise<R>;
/**
- * Asserts given DOM node is hidden or detached from DOM.
+ * Asserts that DOM node has a given CSS class.
*/
- toBeHidden(options?: { timeout?: number }): Promise<R>;
+ toHaveClass(className: string, options?: { timeout?: number }): Promise<R>;
/**
- * Asserts given DOM node visible on the screen.
+ * Asserts element's computed CSS property `name` matches expected value.
*/
- toBeVisible(options?: { timeout?: number }): Promise<R>;
+ toHaveCSS(expected: string | RegExp, name: string, options?: { timeout?: number }): Promise<R>;
/**
- * Asserts given DOM is a focused (active) in document.
+ * Asserts element's data attribute data-`name` matches expected value.
*/
- toBeFocused(options?: { timeout?: number }): Promise<R>;
+ toHaveData(expected: string | RegExp, name: string, options?: { timeout?: number }): Promise<R>;
/**
- * Asserts given select option is selected
+ * Asserts element's `id` attribute matches expected value.
*/
- toBeSelected(options?: { timeout?: number }): Promise<R>;
+ toHaveId(expected: string | RegExp, options?: { timeout?: number }): Promise<R>;
/**
* Asserts JavaScript object that corresponds to the Node has a property with given value.
@@ -155,9 +150,14 @@ declare global {
toHaveProp(name: string, value: any, options?: { timeout?: number }): Promise<R>;
/**
- * Asserts that DOM node has a given CSS class.
+ * Asserts element's exact text content.
*/
- toHaveClass(className: string, options?: { timeout?: number }): Promise<R>;
+ toHaveText(expected: string | RegExp, options?: { timeout?: number, useInnerText?: boolean }): Promise<R>;
+
+ /**
+ * Asserts input element's value.
+ */
+ toHaveValue(expected: string | RegExp, options?: { timeout?: number }): Promise<R>;
}
}
}