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.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
import { Locator, Page } from '../../..';
|
2021-07-28 22:30:37 -07:00
|
|
|
import type { Expect } from '../types';
|
|
|
|
|
import { toBeTruthy } from './toBeTruthy';
|
|
|
|
|
import { toEqual } from './toEqual';
|
|
|
|
|
import { toMatchText } from './toMatchText';
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toBeChecked(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
locator: Locator,
|
|
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2021-07-29 07:33:19 -07:00
|
|
|
return toBeTruthy.call(this, 'toBeChecked', locator, 'Locator', async timeout => {
|
2021-07-28 22:30:37 -07:00
|
|
|
return await locator.isChecked({ timeout });
|
|
|
|
|
}, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toBeDisabled(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
locator: Locator,
|
|
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2021-07-29 07:33:19 -07:00
|
|
|
return toBeTruthy.call(this, 'toBeDisabled', locator, 'Locator', async timeout => {
|
2021-07-28 22:30:37 -07:00
|
|
|
return await locator.isDisabled({ timeout });
|
|
|
|
|
}, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toBeEditable(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
locator: Locator,
|
|
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2021-07-29 07:33:19 -07:00
|
|
|
return toBeTruthy.call(this, 'toBeEditable', locator, 'Locator', async timeout => {
|
2021-07-28 22:30:37 -07:00
|
|
|
return await locator.isEditable({ timeout });
|
|
|
|
|
}, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toBeEmpty(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
locator: Locator,
|
|
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2021-07-29 07:33:19 -07:00
|
|
|
return toBeTruthy.call(this, 'toBeEmpty', locator, 'Locator', async timeout => {
|
2021-07-28 22:30:37 -07:00
|
|
|
return await locator.evaluate(element => {
|
|
|
|
|
if (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA')
|
|
|
|
|
return !(element as HTMLInputElement).value;
|
|
|
|
|
return !element.textContent?.trim();
|
|
|
|
|
}, { timeout });
|
|
|
|
|
}, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toBeEnabled(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
locator: Locator,
|
|
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2021-07-29 07:33:19 -07:00
|
|
|
return toBeTruthy.call(this, 'toBeEnabled', locator, 'Locator', async timeout => {
|
2021-07-28 22:30:37 -07:00
|
|
|
return await locator.isEnabled({ timeout });
|
|
|
|
|
}, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toBeFocused(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
locator: Locator,
|
|
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2021-07-29 07:33:19 -07:00
|
|
|
return toBeTruthy.call(this, 'toBeFocused', locator, 'Locator', async timeout => {
|
2021-07-28 22:30:37 -07:00
|
|
|
return await locator.evaluate(element => {
|
|
|
|
|
return document.activeElement === element;
|
|
|
|
|
}, { timeout });
|
|
|
|
|
}, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toBeHidden(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
locator: Locator,
|
|
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2021-07-29 07:33:19 -07:00
|
|
|
return toBeTruthy.call(this, 'toBeHidden', locator, 'Locator', async timeout => {
|
2021-07-28 22:30:37 -07:00
|
|
|
return await locator.isHidden({ timeout });
|
|
|
|
|
}, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toBeSelected(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
locator: Locator,
|
|
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2021-07-29 07:33:19 -07:00
|
|
|
return toBeTruthy.call(this, 'toBeSelected', locator, 'Locator', async timeout => {
|
2021-07-28 22:30:37 -07:00
|
|
|
return await locator.evaluate(element => {
|
|
|
|
|
return (element as HTMLOptionElement).selected;
|
|
|
|
|
}, { timeout });
|
|
|
|
|
}, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toBeVisible(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
locator: Locator,
|
|
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2021-07-29 07:33:19 -07:00
|
|
|
return toBeTruthy.call(this, 'toBeVisible', locator, 'Locator', async timeout => {
|
2021-07-28 22:30:37 -07:00
|
|
|
return await locator.isVisible({ timeout });
|
|
|
|
|
}, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toContainText(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
locator: Locator,
|
|
|
|
|
expected: string,
|
|
|
|
|
options?: { timeout?: number, useInnerText?: boolean },
|
|
|
|
|
) {
|
2021-07-29 07:33:19 -07:00
|
|
|
return toMatchText.call(this, 'toContainText', locator, 'Locator', async timeout => {
|
2021-07-28 22:30:37 -07:00
|
|
|
if (options?.useInnerText)
|
|
|
|
|
return await locator.innerText({ timeout });
|
|
|
|
|
return await locator.textContent() || '';
|
|
|
|
|
}, expected, { ...options, matchSubstring: true });
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toHaveAttr(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
locator: Locator,
|
|
|
|
|
name: string,
|
|
|
|
|
expected: string | RegExp,
|
|
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2021-07-29 07:33:19 -07:00
|
|
|
return toMatchText.call(this, 'toHaveAttr', locator, 'Locator', async timeout => {
|
2021-07-28 22:30:37 -07:00
|
|
|
return await locator.getAttribute(name, { timeout }) || '';
|
|
|
|
|
}, expected, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toHaveClass(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
locator: Locator,
|
2021-07-29 07:33:19 -07:00
|
|
|
expected: string | RegExp | string[],
|
2021-07-28 22:30:37 -07:00
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2021-07-29 07:33:19 -07:00
|
|
|
if (Array.isArray(expected)) {
|
|
|
|
|
return toEqual.call(this, 'toHaveClass', locator, 'Locator', async () => {
|
|
|
|
|
return await locator.evaluateAll(ee => ee.map(e => e.className));
|
|
|
|
|
}, expected, options);
|
|
|
|
|
} else {
|
|
|
|
|
return toMatchText.call(this, 'toHaveClass', locator, 'Locator', async timeout => {
|
|
|
|
|
return await locator.evaluate(element => element.className, { timeout });
|
|
|
|
|
}, expected, options);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function toHaveCount(
|
|
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
locator: Locator,
|
|
|
|
|
expected: number,
|
|
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
|
|
|
|
return toEqual.call(this, 'toHaveCount', locator, 'Locator', async timeout => {
|
|
|
|
|
return await locator.count();
|
|
|
|
|
}, 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']>,
|
|
|
|
|
locator: Locator,
|
|
|
|
|
name: string,
|
|
|
|
|
expected: string | RegExp,
|
|
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2021-07-29 07:33:19 -07:00
|
|
|
return toMatchText.call(this, 'toHaveCSS', locator, 'Locator', async timeout => {
|
2021-07-28 22:30:37 -07:00
|
|
|
return await locator.evaluate(async (element, name) => {
|
|
|
|
|
return (window.getComputedStyle(element) as any)[name];
|
|
|
|
|
}, name, { timeout });
|
|
|
|
|
}, expected, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toHaveData(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
locator: Locator,
|
|
|
|
|
name: string,
|
|
|
|
|
expected: string | RegExp,
|
|
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2021-07-29 07:33:19 -07:00
|
|
|
return toMatchText.call(this, 'toHaveData', locator, 'Locator', async timeout => {
|
2021-07-28 22:30:37 -07:00
|
|
|
return await locator.getAttribute('data-' + name, { timeout }) || '';
|
|
|
|
|
}, 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']>,
|
|
|
|
|
locator: Locator,
|
|
|
|
|
expected: string | RegExp,
|
|
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2021-07-29 07:33:19 -07:00
|
|
|
return toMatchText.call(this, 'toHaveId', locator, 'Locator', async timeout => {
|
2021-07-28 22:30:37 -07:00
|
|
|
return await locator.getAttribute('id', { timeout }) || '';
|
|
|
|
|
}, expected, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toHaveProp(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
locator: Locator,
|
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 },
|
|
|
|
|
) {
|
2021-07-29 07:33:19 -07:00
|
|
|
return toEqual.call(this, 'toHaveProp', locator, 'Locator', async timeout => {
|
|
|
|
|
return await locator.evaluate((element, name) => (element as any)[name], name, { timeout });
|
|
|
|
|
}, 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']>,
|
|
|
|
|
locator: Locator,
|
2021-07-29 07:33:19 -07:00
|
|
|
expected: string | RegExp | string[],
|
|
|
|
|
options?: { timeout?: number, useInnerText?: boolean },
|
|
|
|
|
) {
|
|
|
|
|
if (Array.isArray(expected)) {
|
|
|
|
|
return toEqual.call(this, 'toHaveText', locator, 'Locator', async () => {
|
|
|
|
|
return locator.evaluateAll((ee, useInnerText) => {
|
|
|
|
|
return ee.map(e => useInnerText ? (e as HTMLElement).innerText : e.textContent || '');
|
|
|
|
|
}, options?.useInnerText);
|
|
|
|
|
}, expected, options);
|
|
|
|
|
} else {
|
|
|
|
|
return toMatchText.call(this, 'toHaveText', locator, 'Locator', async timeout => {
|
|
|
|
|
if (options?.useInnerText)
|
|
|
|
|
return await locator.innerText({ timeout });
|
|
|
|
|
return await locator.textContent() || '';
|
|
|
|
|
}, expected, options);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function toHaveTitle(
|
|
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
page: Page,
|
|
|
|
|
expected: string | RegExp,
|
2021-07-28 22:30:37 -07:00
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2021-07-29 07:33:19 -07:00
|
|
|
return toMatchText.call(this, 'toHaveTitle', page, 'Page', async () => {
|
|
|
|
|
return await page.title();
|
|
|
|
|
}, 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-07-29 07:33:19 -07:00
|
|
|
return toMatchText.call(this, 'toHaveURL', page, 'Page', async () => {
|
|
|
|
|
return page.url();
|
2021-07-28 22:30:37 -07:00
|
|
|
}, expected, options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 07:33:19 -07:00
|
|
|
export function toHaveValue(
|
2021-07-28 22:30:37 -07:00
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
locator: Locator,
|
|
|
|
|
expected: string | RegExp,
|
|
|
|
|
options?: { timeout?: number },
|
|
|
|
|
) {
|
2021-07-29 07:33:19 -07:00
|
|
|
return toMatchText.call(this, 'toHaveValue', locator, 'Locator', async timeout => {
|
2021-07-28 22:30:37 -07:00
|
|
|
return await locator.inputValue({ timeout });
|
|
|
|
|
}, expected, options);
|
|
|
|
|
}
|