123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.toBeAttached = toBeAttached;
- exports.toBeChecked = toBeChecked;
- exports.toBeDisabled = toBeDisabled;
- exports.toBeEditable = toBeEditable;
- exports.toBeEmpty = toBeEmpty;
- exports.toBeEnabled = toBeEnabled;
- exports.toBeFocused = toBeFocused;
- exports.toBeHidden = toBeHidden;
- exports.toBeInViewport = toBeInViewport;
- exports.toBeOK = toBeOK;
- exports.toBeVisible = toBeVisible;
- exports.toContainText = toContainText;
- exports.toHaveAttribute = toHaveAttribute;
- exports.toHaveCSS = toHaveCSS;
- exports.toHaveClass = toHaveClass;
- exports.toHaveCount = toHaveCount;
- exports.toHaveId = toHaveId;
- exports.toHaveJSProperty = toHaveJSProperty;
- exports.toHaveText = toHaveText;
- exports.toHaveTitle = toHaveTitle;
- exports.toHaveURL = toHaveURL;
- exports.toHaveValue = toHaveValue;
- exports.toHaveValues = toHaveValues;
- exports.toPass = toPass;
- var _utilsBundle = require("playwright-core/lib/utilsBundle");
- var _util = require("../util");
- var _toBeTruthy = require("./toBeTruthy");
- var _toEqual = require("./toEqual");
- var _toMatchText = require("./toMatchText");
- var _utils = require("playwright-core/lib/utils");
- var _globals = require("../common/globals");
- var _testInfo = require("../worker/testInfo");
- /**
- * 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.
- */
- function toBeAttached(locator, options) {
- const attached = !options || options.attached === undefined || options.attached === true;
- const expected = attached ? 'attached' : 'detached';
- const unexpected = attached ? 'detached' : 'attached';
- const arg = attached ? '' : '{ attached: false }';
- return _toBeTruthy.toBeTruthy.call(this, 'toBeAttached', locator, 'Locator', expected, unexpected, arg, async (isNot, timeout) => {
- return await locator._expect(attached ? 'to.be.attached' : 'to.be.detached', {
- isNot,
- timeout
- });
- }, options);
- }
- function toBeChecked(locator, options) {
- const checked = !options || options.checked === undefined || options.checked === true;
- const expected = checked ? 'checked' : 'unchecked';
- const unexpected = checked ? 'unchecked' : 'checked';
- const arg = checked ? '' : '{ checked: false }';
- return _toBeTruthy.toBeTruthy.call(this, 'toBeChecked', locator, 'Locator', expected, unexpected, arg, async (isNot, timeout) => {
- return await locator._expect(checked ? 'to.be.checked' : 'to.be.unchecked', {
- isNot,
- timeout
- });
- }, options);
- }
- function toBeDisabled(locator, options) {
- return _toBeTruthy.toBeTruthy.call(this, 'toBeDisabled', locator, 'Locator', 'disabled', 'enabled', '', async (isNot, timeout) => {
- return await locator._expect('to.be.disabled', {
- isNot,
- timeout
- });
- }, options);
- }
- function toBeEditable(locator, options) {
- const editable = !options || options.editable === undefined || options.editable === true;
- const expected = editable ? 'editable' : 'readOnly';
- const unexpected = editable ? 'readOnly' : 'editable';
- const arg = editable ? '' : '{ editable: false }';
- return _toBeTruthy.toBeTruthy.call(this, 'toBeEditable', locator, 'Locator', expected, unexpected, arg, async (isNot, timeout) => {
- return await locator._expect(editable ? 'to.be.editable' : 'to.be.readonly', {
- isNot,
- timeout
- });
- }, options);
- }
- function toBeEmpty(locator, options) {
- return _toBeTruthy.toBeTruthy.call(this, 'toBeEmpty', locator, 'Locator', 'empty', 'notEmpty', '', async (isNot, timeout) => {
- return await locator._expect('to.be.empty', {
- isNot,
- timeout
- });
- }, options);
- }
- function toBeEnabled(locator, options) {
- const enabled = !options || options.enabled === undefined || options.enabled === true;
- const expected = enabled ? 'enabled' : 'disabled';
- const unexpected = enabled ? 'disabled' : 'enabled';
- const arg = enabled ? '' : '{ enabled: false }';
- return _toBeTruthy.toBeTruthy.call(this, 'toBeEnabled', locator, 'Locator', expected, unexpected, arg, async (isNot, timeout) => {
- return await locator._expect(enabled ? 'to.be.enabled' : 'to.be.disabled', {
- isNot,
- timeout
- });
- }, options);
- }
- function toBeFocused(locator, options) {
- return _toBeTruthy.toBeTruthy.call(this, 'toBeFocused', locator, 'Locator', 'focused', 'inactive', '', async (isNot, timeout) => {
- return await locator._expect('to.be.focused', {
- isNot,
- timeout
- });
- }, options);
- }
- function toBeHidden(locator, options) {
- return _toBeTruthy.toBeTruthy.call(this, 'toBeHidden', locator, 'Locator', 'hidden', 'visible', '', async (isNot, timeout) => {
- return await locator._expect('to.be.hidden', {
- isNot,
- timeout
- });
- }, options);
- }
- function toBeVisible(locator, options) {
- const visible = !options || options.visible === undefined || options.visible === true;
- const expected = visible ? 'visible' : 'hidden';
- const unexpected = visible ? 'hidden' : 'visible';
- const arg = visible ? '' : '{ visible: false }';
- return _toBeTruthy.toBeTruthy.call(this, 'toBeVisible', locator, 'Locator', expected, unexpected, arg, async (isNot, timeout) => {
- return await locator._expect(visible ? 'to.be.visible' : 'to.be.hidden', {
- isNot,
- timeout
- });
- }, options);
- }
- function toBeInViewport(locator, options) {
- return _toBeTruthy.toBeTruthy.call(this, 'toBeInViewport', locator, 'Locator', 'in viewport', 'outside viewport', '', async (isNot, timeout) => {
- return await locator._expect('to.be.in.viewport', {
- isNot,
- expectedNumber: options === null || options === void 0 ? void 0 : options.ratio,
- timeout
- });
- }, options);
- }
- function toContainText(locator, expected, options = {}) {
- if (Array.isArray(expected)) {
- return _toEqual.toEqual.call(this, 'toContainText', locator, 'Locator', async (isNot, timeout) => {
- const expectedText = (0, _toMatchText.toExpectedTextValues)(expected, {
- matchSubstring: true,
- normalizeWhiteSpace: true,
- ignoreCase: options.ignoreCase
- });
- return await locator._expect('to.contain.text.array', {
- expectedText,
- isNot,
- useInnerText: options.useInnerText,
- timeout
- });
- }, expected, {
- ...options,
- contains: true
- });
- } else {
- return _toMatchText.toMatchText.call(this, 'toContainText', locator, 'Locator', async (isNot, timeout) => {
- const expectedText = (0, _toMatchText.toExpectedTextValues)([expected], {
- matchSubstring: true,
- normalizeWhiteSpace: true,
- ignoreCase: options.ignoreCase
- });
- return await locator._expect('to.have.text', {
- expectedText,
- isNot,
- useInnerText: options.useInnerText,
- timeout
- });
- }, expected, options);
- }
- }
- function toHaveAttribute(locator, name, expected, options) {
- if (!options) {
- // Update params for the case toHaveAttribute(name, options);
- if (typeof expected === 'object' && !(0, _utils.isRegExp)(expected)) {
- options = expected;
- expected = undefined;
- }
- }
- if (expected === undefined) {
- return _toBeTruthy.toBeTruthy.call(this, 'toHaveAttribute', locator, 'Locator', 'have attribute', 'not have attribute', '', async (isNot, timeout) => {
- return await locator._expect('to.have.attribute', {
- expressionArg: name,
- isNot,
- timeout
- });
- }, options);
- }
- return _toMatchText.toMatchText.call(this, 'toHaveAttribute', locator, 'Locator', async (isNot, timeout) => {
- var _options;
- const expectedText = (0, _toMatchText.toExpectedTextValues)([expected], {
- ignoreCase: (_options = options) === null || _options === void 0 ? void 0 : _options.ignoreCase
- });
- return await locator._expect('to.have.attribute.value', {
- expressionArg: name,
- expectedText,
- isNot,
- timeout
- });
- }, expected, options);
- }
- function toHaveClass(locator, expected, options) {
- if (Array.isArray(expected)) {
- return _toEqual.toEqual.call(this, 'toHaveClass', locator, 'Locator', async (isNot, timeout) => {
- const expectedText = (0, _toMatchText.toExpectedTextValues)(expected);
- return await locator._expect('to.have.class.array', {
- expectedText,
- isNot,
- timeout
- });
- }, expected, options);
- } else {
- return _toMatchText.toMatchText.call(this, 'toHaveClass', locator, 'Locator', async (isNot, timeout) => {
- const expectedText = (0, _toMatchText.toExpectedTextValues)([expected]);
- return await locator._expect('to.have.class', {
- expectedText,
- isNot,
- timeout
- });
- }, expected, options);
- }
- }
- function toHaveCount(locator, expected, options) {
- return _toEqual.toEqual.call(this, 'toHaveCount', locator, 'Locator', async (isNot, timeout) => {
- return await locator._expect('to.have.count', {
- expectedNumber: expected,
- isNot,
- timeout
- });
- }, expected, options);
- }
- function toHaveCSS(locator, name, expected, options) {
- return _toMatchText.toMatchText.call(this, 'toHaveCSS', locator, 'Locator', async (isNot, timeout) => {
- const expectedText = (0, _toMatchText.toExpectedTextValues)([expected]);
- return await locator._expect('to.have.css', {
- expressionArg: name,
- expectedText,
- isNot,
- timeout
- });
- }, expected, options);
- }
- function toHaveId(locator, expected, options) {
- return _toMatchText.toMatchText.call(this, 'toHaveId', locator, 'Locator', async (isNot, timeout) => {
- const expectedText = (0, _toMatchText.toExpectedTextValues)([expected]);
- return await locator._expect('to.have.id', {
- expectedText,
- isNot,
- timeout
- });
- }, expected, options);
- }
- function toHaveJSProperty(locator, name, expected, options) {
- return _toEqual.toEqual.call(this, 'toHaveJSProperty', locator, 'Locator', async (isNot, timeout) => {
- return await locator._expect('to.have.property', {
- expressionArg: name,
- expectedValue: expected,
- isNot,
- timeout
- });
- }, expected, options);
- }
- function toHaveText(locator, expected, options = {}) {
- if (Array.isArray(expected)) {
- return _toEqual.toEqual.call(this, 'toHaveText', locator, 'Locator', async (isNot, timeout) => {
- const expectedText = (0, _toMatchText.toExpectedTextValues)(expected, {
- normalizeWhiteSpace: true,
- ignoreCase: options.ignoreCase
- });
- return await locator._expect('to.have.text.array', {
- expectedText,
- isNot,
- useInnerText: options === null || options === void 0 ? void 0 : options.useInnerText,
- timeout
- });
- }, expected, options);
- } else {
- return _toMatchText.toMatchText.call(this, 'toHaveText', locator, 'Locator', async (isNot, timeout) => {
- const expectedText = (0, _toMatchText.toExpectedTextValues)([expected], {
- normalizeWhiteSpace: true,
- ignoreCase: options.ignoreCase
- });
- return await locator._expect('to.have.text', {
- expectedText,
- isNot,
- useInnerText: options === null || options === void 0 ? void 0 : options.useInnerText,
- timeout
- });
- }, expected, options);
- }
- }
- function toHaveValue(locator, expected, options) {
- return _toMatchText.toMatchText.call(this, 'toHaveValue', locator, 'Locator', async (isNot, timeout) => {
- const expectedText = (0, _toMatchText.toExpectedTextValues)([expected]);
- return await locator._expect('to.have.value', {
- expectedText,
- isNot,
- timeout
- });
- }, expected, options);
- }
- function toHaveValues(locator, expected, options) {
- return _toEqual.toEqual.call(this, 'toHaveValues', locator, 'Locator', async (isNot, timeout) => {
- const expectedText = (0, _toMatchText.toExpectedTextValues)(expected);
- return await locator._expect('to.have.values', {
- expectedText,
- isNot,
- timeout
- });
- }, expected, options);
- }
- function toHaveTitle(page, expected, options = {}) {
- const locator = page.locator(':root');
- return _toMatchText.toMatchText.call(this, 'toHaveTitle', locator, 'Locator', async (isNot, timeout) => {
- const expectedText = (0, _toMatchText.toExpectedTextValues)([expected], {
- normalizeWhiteSpace: true
- });
- return await locator._expect('to.have.title', {
- expectedText,
- isNot,
- timeout
- });
- }, expected, options);
- }
- function toHaveURL(page, expected, options) {
- const baseURL = page.context()._options.baseURL;
- expected = typeof expected === 'string' ? (0, _utils.constructURLBasedOnBaseURL)(baseURL, expected) : expected;
- const locator = page.locator(':root');
- return _toMatchText.toMatchText.call(this, 'toHaveURL', locator, 'Locator', async (isNot, timeout) => {
- const expectedText = (0, _toMatchText.toExpectedTextValues)([expected]);
- return await locator._expect('to.have.url', {
- expectedText,
- isNot,
- timeout
- });
- }, expected, options);
- }
- async function toBeOK(response) {
- const matcherName = 'toBeOK';
- (0, _util.expectTypes)(response, ['APIResponse'], matcherName);
- const contentType = response.headers()['content-type'];
- const isTextEncoding = contentType && (0, _utils.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
- }) + (0, _util.callLogText)(log) + (text === null ? '' : `\nResponse text:\n${_utilsBundle.colors.dim((text === null || text === void 0 ? void 0 : text.substring(0, 1000)) || '')}`);
- const pass = response.ok();
- return {
- message,
- pass
- };
- }
- async function toPass(callback, options = {}) {
- const testInfo = (0, _globals.currentTestInfo)();
- const timeout = options.timeout !== undefined ? options.timeout : 0;
- const {
- deadline,
- timeoutMessage
- } = testInfo ? testInfo._deadlineForMatcher(timeout) : _testInfo.TestInfoImpl._defaultDeadlineForMatcher(timeout);
- const result = await (0, _utils.pollAgainstDeadline)(async () => {
- if (testInfo && (0, _globals.currentTestInfo)() !== testInfo) return {
- continuePolling: false,
- result: undefined
- };
- try {
- await callback();
- return {
- continuePolling: !!this.isNot,
- result: undefined
- };
- } catch (e) {
- return {
- continuePolling: !this.isNot,
- result: e
- };
- }
- }, deadline, options.intervals || [100, 250, 500, 1000]);
- if (result.timedOut) {
- const message = result.result ? [result.result.message, '', `Call Log:`, `- ${timeoutMessage}`].join('\n') : timeoutMessage;
- return {
- message: () => message,
- pass: !!this.isNot
- };
- }
- return {
- pass: !this.isNot,
- message: () => ''
- };
- }
|