2021-07-28 22:30:37 -07:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { Locator, Page, APIResponse } from 'playwright-core';
|
2022-03-25 07:43:29 -08:00
|
|
|
import type { FrameExpectOptions } from 'playwright-core/lib/client/types';
|
2022-07-29 11:46:48 -07:00
|
|
|
import { colors } from 'playwright-core/lib/utilsBundle';
|
2022-08-30 17:53:00 -07:00
|
|
|
import { constructURLBasedOnBaseURL, isRegExp } from 'playwright-core/lib/utils';
|
2021-07-28 22:30:37 -07:00
|
|
|
import type { Expect } from '../types';
|
2022-03-11 23:40:28 -07:00
|
|
|
import { expectTypes, callLogText } from '../util';
|
2021-07-28 22:30:37 -07:00
|
|
|
import { toBeTruthy } from './toBeTruthy';
|
|
|
|
|
import { toEqual } from './toEqual';
|
2022-02-23 14:17:37 -07:00
|
|
|
import { toExpectedTextValues, toMatchText } from './toMatchText';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { ParsedStackTrace } from 'playwright-core/lib/utils/stackTrace';
|
2022-07-29 11:46:48 -07:00
|
|
|
import { isTextualMimeType } from 'playwright-core/lib/utils/mimeType';
|
2021-09-24 11:06:30 -07:00
|
|
|
|
|
|
|
|
interface LocatorEx extends Locator {
|
2022-03-14 19:01:13 -06:00
|
|
|
_expect(customStackTrace: ParsedStackTrace, expression: string, options: Omit<FrameExpectOptions, 'expectedValue'> & { expectedValue?: any }): Promise<{ matches: boolean, received?: any, log?: string[] }>;
|
2021-09-24 11:06:30 -07:00
|
|
|
}
|
2021-07-28 22:30:37 -07:00
|
|
|
|
2021-11-30 18:12:19 -08:00
|
|
|
interface APIResponseEx extends APIResponse {
|
|
|
|
|
_fetchLog(): Promise<string[]>;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toBeChecked(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
2021-09-24 11:06:30 -07:00
|
|
|
locator: LocatorEx,
|
2021-12-02 10:31:26 -08:00
|
|
|
options?: { checked?: boolean, timeout?: number },
|
2021-07-28 22:30:37 -07:00
|
|
|
) {
|
2022-03-14 19:01:13 -06:00
|
|
|
return toBeTruthy.call(this, 'toBeChecked', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
2021-12-02 10:31:26 -08:00
|
|
|
const checked = !options || options.checked === undefined || options.checked === true;
|
2022-03-14 19:01:13 -06:00
|
|
|
return await locator._expect(customStackTrace, checked ? 'to.be.checked' : 'to.be.unchecked', { isNot, timeout });
|
2021-07-28 22:30:37 -07:00
|
|
|
}, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toBeDisabled(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
2021-09-24 11:06:30 -07:00
|
|
|
locator: LocatorEx,
|
2021-07-28 22:30:37 -07:00
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2022-03-14 19:01:13 -06:00
|
|
|
return toBeTruthy.call(this, 'toBeDisabled', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
|
|
|
|
return await locator._expect(customStackTrace, 'to.be.disabled', { isNot, timeout });
|
2021-07-28 22:30:37 -07:00
|
|
|
}, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toBeEditable(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
2021-09-24 11:06:30 -07:00
|
|
|
locator: LocatorEx,
|
2022-09-06 12:50:45 -07:00
|
|
|
options?: { editable?: boolean, timeout?: number },
|
2021-07-28 22:30:37 -07:00
|
|
|
) {
|
2022-03-14 19:01:13 -06:00
|
|
|
return toBeTruthy.call(this, 'toBeEditable', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
2022-09-06 12:50:45 -07:00
|
|
|
const editable = !options || options.editable === undefined || options.editable === true;
|
|
|
|
|
return await locator._expect(customStackTrace, editable ? 'to.be.editable' : 'to.be.readonly', { isNot, timeout });
|
2021-07-28 22:30:37 -07:00
|
|
|
}, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toBeEmpty(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
2021-09-24 11:06:30 -07:00
|
|
|
locator: LocatorEx,
|
2021-07-28 22:30:37 -07:00
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2022-03-14 19:01:13 -06:00
|
|
|
return toBeTruthy.call(this, 'toBeEmpty', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
|
|
|
|
return await locator._expect(customStackTrace, 'to.be.empty', { isNot, timeout });
|
2021-07-28 22:30:37 -07:00
|
|
|
}, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toBeEnabled(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
2021-09-24 11:06:30 -07:00
|
|
|
locator: LocatorEx,
|
2022-09-06 11:40:34 -07:00
|
|
|
options?: { enabled?: boolean, timeout?: number },
|
2021-07-28 22:30:37 -07:00
|
|
|
) {
|
2022-03-14 19:01:13 -06:00
|
|
|
return toBeTruthy.call(this, 'toBeEnabled', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
2022-09-06 11:40:34 -07:00
|
|
|
const enabled = !options || options.enabled === undefined || options.enabled === true;
|
|
|
|
|
return await locator._expect(customStackTrace, enabled ? 'to.be.enabled' : 'to.be.disabled', { isNot, timeout });
|
2021-07-28 22:30:37 -07:00
|
|
|
}, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toBeFocused(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
2021-09-24 11:06:30 -07:00
|
|
|
locator: LocatorEx,
|
2021-07-28 22:30:37 -07:00
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2022-03-14 19:01:13 -06:00
|
|
|
return toBeTruthy.call(this, 'toBeFocused', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
|
|
|
|
return await locator._expect(customStackTrace, 'to.be.focused', { isNot, timeout });
|
2021-07-28 22:30:37 -07:00
|
|
|
}, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toBeHidden(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
2021-09-24 11:06:30 -07:00
|
|
|
locator: LocatorEx,
|
2021-07-28 22:30:37 -07:00
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2022-03-14 19:01:13 -06:00
|
|
|
return toBeTruthy.call(this, 'toBeHidden', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
|
|
|
|
return await locator._expect(customStackTrace, 'to.be.hidden', { isNot, timeout });
|
2021-07-28 22:30:37 -07:00
|
|
|
}, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toBeVisible(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
2021-09-24 11:06:30 -07:00
|
|
|
locator: LocatorEx,
|
2022-09-09 08:33:23 -07:00
|
|
|
options?: { visible?: boolean, timeout?: number },
|
2021-07-28 22:30:37 -07:00
|
|
|
) {
|
2022-03-14 19:01:13 -06:00
|
|
|
return toBeTruthy.call(this, 'toBeVisible', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
2022-09-09 08:33:23 -07:00
|
|
|
const visible = !options || options.visible === undefined || options.visible === true;
|
|
|
|
|
return await locator._expect(customStackTrace, visible ? 'to.be.visible' : 'to.be.hidden', { isNot, timeout });
|
2021-07-28 22:30:37 -07:00
|
|
|
}, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toContainText(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
2021-09-24 11:06:30 -07:00
|
|
|
locator: LocatorEx,
|
2021-09-27 11:14:35 -07:00
|
|
|
expected: string | RegExp | (string | RegExp)[],
|
2022-06-02 05:52:53 -07:00
|
|
|
options: { timeout?: number, useInnerText?: boolean, ignoreCase?: boolean } = {},
|
2021-07-28 22:30:37 -07:00
|
|
|
) {
|
2021-09-27 11:14:35 -07:00
|
|
|
if (Array.isArray(expected)) {
|
2022-03-14 19:01:13 -06:00
|
|
|
return toEqual.call(this, 'toContainText', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
2022-06-02 05:52:53 -07:00
|
|
|
const expectedText = toExpectedTextValues(expected, { matchSubstring: true, normalizeWhiteSpace: true, ignoreCase: options.ignoreCase });
|
|
|
|
|
return await locator._expect(customStackTrace, 'to.contain.text.array', { expectedText, isNot, useInnerText: options.useInnerText, timeout });
|
2021-09-27 11:14:35 -07:00
|
|
|
}, expected, { ...options, contains: true });
|
|
|
|
|
} else {
|
2022-03-14 19:01:13 -06:00
|
|
|
return toMatchText.call(this, 'toContainText', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
2022-06-02 05:52:53 -07:00
|
|
|
const expectedText = toExpectedTextValues([expected], { matchSubstring: true, normalizeWhiteSpace: true, ignoreCase: options.ignoreCase });
|
|
|
|
|
return await locator._expect(customStackTrace, 'to.have.text', { expectedText, isNot, useInnerText: options.useInnerText, timeout });
|
2021-09-27 11:14:35 -07:00
|
|
|
}, expected, options);
|
|
|
|
|
}
|
2021-07-28 22:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-06 16:58:42 -07:00
|
|
|
export function toHaveAttribute(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
2021-09-24 11:06:30 -07:00
|
|
|
locator: LocatorEx,
|
2021-07-28 22:30:37 -07:00
|
|
|
name: string,
|
2022-08-30 17:53:00 -07:00
|
|
|
expected: string | RegExp | undefined | { timeout?: number},
|
2021-07-28 22:30:37 -07:00
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2022-08-30 17:53:00 -07:00
|
|
|
if (!options) {
|
|
|
|
|
// Update params for the case toHaveAttribute(name, options);
|
|
|
|
|
if (typeof expected === 'object' && !isRegExp(expected)) {
|
|
|
|
|
options = expected;
|
|
|
|
|
expected = undefined;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-25 05:28:34 -07:00
|
|
|
if (expected === undefined) {
|
|
|
|
|
return toBeTruthy.call(this, 'toHaveAttribute', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
|
|
|
|
return await locator._expect(customStackTrace, 'to.have.attribute', { expressionArg: name, isNot, timeout });
|
|
|
|
|
}, options);
|
|
|
|
|
}
|
2022-03-14 19:01:13 -06:00
|
|
|
return toMatchText.call(this, 'toHaveAttribute', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
2022-08-30 17:53:00 -07:00
|
|
|
const expectedText = toExpectedTextValues([expected as (string | RegExp)]);
|
2022-08-25 05:28:34 -07:00
|
|
|
return await locator._expect(customStackTrace, 'to.have.attribute.value', { expressionArg: name, expectedText, isNot, timeout });
|
2022-08-30 17:53:00 -07:00
|
|
|
}, expected as (string | RegExp), options);
|
2021-07-28 22:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toHaveClass(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
2021-09-24 11:06:30 -07:00
|
|
|
locator: LocatorEx,
|
2021-09-02 15:48:04 -07:00
|
|
|
expected: string | RegExp | (string | RegExp)[],
|
2021-07-28 22:30:37 -07:00
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2021-07-29 07:33:19 -07:00
|
|
|
if (Array.isArray(expected)) {
|
2022-03-14 19:01:13 -06:00
|
|
|
return toEqual.call(this, 'toHaveClass', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
2021-09-24 11:06:30 -07:00
|
|
|
const expectedText = toExpectedTextValues(expected);
|
2022-03-14 19:01:13 -06:00
|
|
|
return await locator._expect(customStackTrace, 'to.have.class.array', { expectedText, isNot, timeout });
|
2021-07-29 07:33:19 -07:00
|
|
|
}, expected, options);
|
|
|
|
|
} else {
|
2022-03-14 19:01:13 -06:00
|
|
|
return toMatchText.call(this, 'toHaveClass', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
2021-09-24 11:06:30 -07:00
|
|
|
const expectedText = toExpectedTextValues([expected]);
|
2022-03-14 19:01:13 -06:00
|
|
|
return await locator._expect(customStackTrace, 'to.have.class', { expectedText, isNot, timeout });
|
2021-07-29 07:33:19 -07:00
|
|
|
}, expected, options);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function toHaveCount(
|
|
|
|
|
this: ReturnType<Expect['getState']>,
|
2021-09-24 11:06:30 -07:00
|
|
|
locator: LocatorEx,
|
2021-07-29 07:33:19 -07:00
|
|
|
expected: number,
|
|
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2022-03-14 19:01:13 -06:00
|
|
|
return toEqual.call(this, 'toHaveCount', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
|
|
|
|
return await locator._expect(customStackTrace, 'to.have.count', { expectedNumber: expected, isNot, timeout });
|
2021-07-29 07:33:19 -07:00
|
|
|
}, expected, options);
|
2021-07-28 22:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toHaveCSS(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
2021-09-24 11:06:30 -07:00
|
|
|
locator: LocatorEx,
|
2021-07-28 22:30:37 -07:00
|
|
|
name: string,
|
|
|
|
|
expected: string | RegExp,
|
|
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2022-03-14 19:01:13 -06:00
|
|
|
return toMatchText.call(this, 'toHaveCSS', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
2021-09-24 11:06:30 -07:00
|
|
|
const expectedText = toExpectedTextValues([expected]);
|
2022-03-14 19:01:13 -06:00
|
|
|
return await locator._expect(customStackTrace, 'to.have.css', { expressionArg: name, expectedText, isNot, timeout });
|
2021-07-28 22:30:37 -07:00
|
|
|
}, expected, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toHaveId(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
2021-09-24 11:06:30 -07:00
|
|
|
locator: LocatorEx,
|
2021-07-28 22:30:37 -07:00
|
|
|
expected: string | RegExp,
|
|
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2022-03-14 19:01:13 -06:00
|
|
|
return toMatchText.call(this, 'toHaveId', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
2021-09-24 11:06:30 -07:00
|
|
|
const expectedText = toExpectedTextValues([expected]);
|
2022-03-14 19:01:13 -06:00
|
|
|
return await locator._expect(customStackTrace, 'to.have.id', { expectedText, isNot, timeout });
|
2021-07-28 22:30:37 -07:00
|
|
|
}, expected, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-06 16:58:42 -07:00
|
|
|
export function toHaveJSProperty(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
2021-09-24 11:06:30 -07:00
|
|
|
locator: LocatorEx,
|
2021-07-29 07:33:19 -07:00
|
|
|
name: string,
|
2021-08-06 14:02:41 -07:00
|
|
|
expected: any,
|
2021-07-28 22:30:37 -07:00
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2022-03-14 19:01:13 -06:00
|
|
|
return toEqual.call(this, 'toHaveJSProperty', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
|
|
|
|
return await locator._expect(customStackTrace, 'to.have.property', { expressionArg: name, expectedValue: expected, isNot, timeout });
|
2021-07-29 07:33:19 -07:00
|
|
|
}, expected, options);
|
2021-07-28 22:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toHaveText(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
2021-09-24 11:06:30 -07:00
|
|
|
locator: LocatorEx,
|
2021-09-02 15:48:04 -07:00
|
|
|
expected: string | RegExp | (string | RegExp)[],
|
2022-06-02 05:52:53 -07:00
|
|
|
options: { timeout?: number, useInnerText?: boolean, ignoreCase?: boolean } = {},
|
2021-07-29 07:33:19 -07:00
|
|
|
) {
|
|
|
|
|
if (Array.isArray(expected)) {
|
2022-03-14 19:01:13 -06:00
|
|
|
return toEqual.call(this, 'toHaveText', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
2022-06-02 05:52:53 -07:00
|
|
|
const expectedText = toExpectedTextValues(expected, { normalizeWhiteSpace: true, ignoreCase: options.ignoreCase });
|
2022-03-14 19:01:13 -06:00
|
|
|
return await locator._expect(customStackTrace, 'to.have.text.array', { expectedText, isNot, useInnerText: options?.useInnerText, timeout });
|
2021-09-24 11:06:30 -07:00
|
|
|
}, expected, options);
|
2021-07-29 07:33:19 -07:00
|
|
|
} else {
|
2022-03-14 19:01:13 -06:00
|
|
|
return toMatchText.call(this, 'toHaveText', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
2022-06-02 05:52:53 -07:00
|
|
|
const expectedText = toExpectedTextValues([expected], { normalizeWhiteSpace: true, ignoreCase: options.ignoreCase });
|
2022-03-14 19:01:13 -06:00
|
|
|
return await locator._expect(customStackTrace, 'to.have.text', { expectedText, isNot, useInnerText: options?.useInnerText, timeout });
|
2021-09-24 11:06:30 -07:00
|
|
|
}, expected, options);
|
2021-07-29 07:33:19 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-18 00:46:30 +01:00
|
|
|
export function toHaveValue(
|
|
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
locator: LocatorEx,
|
2022-06-02 16:01:34 -04:00
|
|
|
expected: string | RegExp,
|
2021-11-18 00:46:30 +01:00
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2022-06-02 16:01:34 -04:00
|
|
|
return toMatchText.call(this, 'toHaveValue', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
|
|
|
|
const expectedText = toExpectedTextValues([expected]);
|
|
|
|
|
return await locator._expect(customStackTrace, 'to.have.value', { expectedText, isNot, timeout });
|
|
|
|
|
}, expected, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function toHaveValues(
|
|
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
locator: LocatorEx,
|
|
|
|
|
expected: (string | RegExp)[],
|
|
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
|
|
|
|
return toEqual.call(this, 'toHaveValues', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
|
|
|
|
const expectedText = toExpectedTextValues(expected);
|
|
|
|
|
return await locator._expect(customStackTrace, 'to.have.values', { expectedText, isNot, timeout });
|
|
|
|
|
}, expected, options);
|
2021-11-18 00:46:30 +01:00
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toHaveTitle(
|
|
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
page: Page,
|
|
|
|
|
expected: string | RegExp,
|
2021-09-14 19:24:29 -07:00
|
|
|
options: { timeout?: number } = {},
|
2021-07-28 22:30:37 -07:00
|
|
|
) {
|
2021-09-24 11:06:30 -07:00
|
|
|
const locator = page.locator(':root') as LocatorEx;
|
2022-03-14 19:01:13 -06:00
|
|
|
return toMatchText.call(this, 'toHaveTitle', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
2021-09-24 11:06:30 -07:00
|
|
|
const expectedText = toExpectedTextValues([expected], { normalizeWhiteSpace: true });
|
2022-03-14 19:01:13 -06:00
|
|
|
return await locator._expect(customStackTrace, 'to.have.title', { expectedText, isNot, timeout });
|
2021-09-24 11:06:30 -07:00
|
|
|
}, expected, options);
|
2021-07-28 22:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toHaveURL(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
2021-07-29 07:33:19 -07:00
|
|
|
page: Page,
|
2021-07-28 22:30:37 -07:00
|
|
|
expected: string | RegExp,
|
2021-07-29 07:33:19 -07:00
|
|
|
options?: { timeout?: number },
|
2021-07-28 22:30:37 -07:00
|
|
|
) {
|
2021-09-21 21:41:24 +02:00
|
|
|
const baseURL = (page.context() as any)._options.baseURL;
|
2021-09-23 16:46:46 -07:00
|
|
|
expected = typeof expected === 'string' ? constructURLBasedOnBaseURL(baseURL, expected) : expected;
|
2021-09-24 11:06:30 -07:00
|
|
|
const locator = page.locator(':root') as LocatorEx;
|
2022-03-14 19:01:13 -06:00
|
|
|
return toMatchText.call(this, 'toHaveURL', locator, 'Locator', async (isNot, timeout, customStackTrace) => {
|
2021-09-24 11:06:30 -07:00
|
|
|
const expectedText = toExpectedTextValues([expected]);
|
2022-03-14 19:01:13 -06:00
|
|
|
return await locator._expect(customStackTrace, 'to.have.url', { expectedText, isNot, timeout });
|
2021-09-23 16:46:46 -07:00
|
|
|
}, expected, options);
|
2021-07-28 22:30:37 -07:00
|
|
|
}
|
2021-11-30 18:12:19 -08:00
|
|
|
|
|
|
|
|
export async function toBeOK(
|
|
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
response: APIResponseEx
|
|
|
|
|
) {
|
|
|
|
|
const matcherName = 'toBeOK';
|
2022-03-11 23:40:28 -07:00
|
|
|
expectTypes(response, ['APIResponse'], matcherName);
|
2022-07-29 11:46:48 -07:00
|
|
|
|
|
|
|
|
const contentType = response.headers()['content-type'];
|
|
|
|
|
const isTextEncoding = contentType && isTextualMimeType(contentType);
|
|
|
|
|
const [log, text] = (this.isNot === response.ok()) ? await Promise.all([
|
|
|
|
|
response._fetchLog(),
|
|
|
|
|
isTextEncoding ? response.text() : null
|
|
|
|
|
]) : [];
|
|
|
|
|
|
|
|
|
|
const message = () => this.utils.matcherHint(matcherName, undefined, '', { isNot: this.isNot }) +
|
|
|
|
|
callLogText(log) +
|
|
|
|
|
(text === null ? '' : `\nResponse text:\n${colors.dim(text?.substring(0, 1000) || '')}`);
|
|
|
|
|
|
2021-11-30 18:12:19 -08:00
|
|
|
const pass = response.ok();
|
|
|
|
|
return { message, pass };
|
|
|
|
|
}
|