|
| 1 | +/** |
| 2 | + * Copyright (c) Microsoft Corporation. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import type { Page } from 'playwright-core'; |
| 18 | +import type { ExpectMatcherState } from '../../types/test'; |
| 19 | +import { EXPECTED_COLOR, printReceived } from '../common/expectBundle'; |
| 20 | +import { matcherHint, type MatcherResult } from './matcherHint'; |
| 21 | +import { constructURLBasedOnBaseURL, urlMatches } from 'playwright-core/lib/utils'; |
| 22 | +import { colors } from 'playwright-core/lib/utilsBundle'; |
| 23 | +import { printReceivedStringContainExpectedResult, printReceivedStringContainExpectedSubstring } from './expect'; |
| 24 | + |
| 25 | +export async function toHaveURL( |
| 26 | + this: ExpectMatcherState, |
| 27 | + page: Page, |
| 28 | + expected: string | RegExp | ((url: URL) => boolean), |
| 29 | + options?: { ignoreCase?: boolean; timeout?: number }, |
| 30 | +): Promise<MatcherResult<string | RegExp, string>> { |
| 31 | + const matcherName = 'toHaveURL'; |
| 32 | + const expression = 'page'; |
| 33 | + const matcherOptions = { |
| 34 | + isNot: this.isNot, |
| 35 | + promise: this.promise, |
| 36 | + }; |
| 37 | + |
| 38 | + if ( |
| 39 | + !(typeof expected === 'string') && |
| 40 | + !(expected && 'test' in expected && typeof expected.test === 'function') && |
| 41 | + !(typeof expected === 'function') |
| 42 | + ) { |
| 43 | + throw new Error( |
| 44 | + [ |
| 45 | + // Always display `expected` in expectation place |
| 46 | + matcherHint(this, undefined, matcherName, expression, undefined, matcherOptions), |
| 47 | + `${colors.bold('Matcher error')}: ${EXPECTED_COLOR('expected')} value must be a string, regular expression, or predicate`, |
| 48 | + this.utils.printWithType('Expected', expected, this.utils.printExpected,), |
| 49 | + ].join('\n\n'), |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + const timeout = options?.timeout ?? this.timeout; |
| 54 | + const baseURL: string | undefined = (page.context() as any)._options.baseURL; |
| 55 | + let conditionSucceeded = false; |
| 56 | + let lastCheckedURLString: string | undefined = undefined; |
| 57 | + try { |
| 58 | + await page.mainFrame().waitForURL( |
| 59 | + url => { |
| 60 | + lastCheckedURLString = url.toString(); |
| 61 | + |
| 62 | + if (options?.ignoreCase) { |
| 63 | + return ( |
| 64 | + !this.isNot === |
| 65 | + urlMatches( |
| 66 | + baseURL?.toLocaleLowerCase(), |
| 67 | + lastCheckedURLString.toLocaleLowerCase(), |
| 68 | + typeof expected === 'string' |
| 69 | + ? expected.toLocaleLowerCase() |
| 70 | + : expected, |
| 71 | + ) |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + return ( |
| 76 | + !this.isNot === urlMatches(baseURL, lastCheckedURLString, expected) |
| 77 | + ); |
| 78 | + }, |
| 79 | + { timeout }, |
| 80 | + ); |
| 81 | + |
| 82 | + conditionSucceeded = true; |
| 83 | + } catch (e) { |
| 84 | + conditionSucceeded = false; |
| 85 | + } |
| 86 | + |
| 87 | + if (conditionSucceeded) |
| 88 | + return { name: matcherName, pass: !this.isNot, message: () => '' }; |
| 89 | + |
| 90 | + return { |
| 91 | + name: matcherName, |
| 92 | + pass: this.isNot, |
| 93 | + message: () => |
| 94 | + toHaveURLMessage( |
| 95 | + this, |
| 96 | + matcherName, |
| 97 | + expression, |
| 98 | + typeof expected === 'string' |
| 99 | + ? constructURLBasedOnBaseURL(baseURL, expected) |
| 100 | + : expected, |
| 101 | + lastCheckedURLString, |
| 102 | + this.isNot, |
| 103 | + true, |
| 104 | + timeout, |
| 105 | + ), |
| 106 | + actual: lastCheckedURLString, |
| 107 | + timeout, |
| 108 | + }; |
| 109 | +} |
| 110 | + |
| 111 | +function toHaveURLMessage( |
| 112 | + state: ExpectMatcherState, |
| 113 | + matcherName: string, |
| 114 | + expression: string, |
| 115 | + expected: string | RegExp | Function, |
| 116 | + received: string | undefined, |
| 117 | + pass: boolean, |
| 118 | + didTimeout: boolean, |
| 119 | + timeout: number, |
| 120 | +): string { |
| 121 | + const matcherOptions = { |
| 122 | + isNot: state.isNot, |
| 123 | + promise: state.promise, |
| 124 | + }; |
| 125 | + const receivedString = received || ''; |
| 126 | + const messagePrefix = matcherHint(state, undefined, matcherName, expression, undefined, matcherOptions, didTimeout ? timeout : undefined); |
| 127 | + |
| 128 | + let printedReceived: string | undefined; |
| 129 | + let printedExpected: string | undefined; |
| 130 | + let printedDiff: string | undefined; |
| 131 | + if (typeof expected === 'function') { |
| 132 | + printedExpected = `Expected predicate to ${!state.isNot ? 'succeed' : 'fail'}`; |
| 133 | + printedReceived = `Received string: ${printReceived(receivedString)}`; |
| 134 | + } else { |
| 135 | + if (pass) { |
| 136 | + if (typeof expected === 'string') { |
| 137 | + printedExpected = `Expected string: not ${state.utils.printExpected(expected)}`; |
| 138 | + const formattedReceived = printReceivedStringContainExpectedSubstring(receivedString, receivedString.indexOf(expected), expected.length); |
| 139 | + printedReceived = `Received string: ${formattedReceived}`; |
| 140 | + } else { |
| 141 | + printedExpected = `Expected pattern: not ${state.utils.printExpected(expected)}`; |
| 142 | + const formattedReceived = printReceivedStringContainExpectedResult(receivedString, typeof expected.exec === 'function' ? expected.exec(receivedString) : null); |
| 143 | + printedReceived = `Received string: ${formattedReceived}`; |
| 144 | + } |
| 145 | + } else { |
| 146 | + const labelExpected = `Expected ${typeof expected === 'string' ? 'string' : 'pattern'}`; |
| 147 | + printedDiff = state.utils.printDiffOrStringify(expected, receivedString, labelExpected, 'Received string', false); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + const resultDetails = printedDiff ? printedDiff : printedExpected + '\n' + printedReceived; |
| 152 | + return messagePrefix + resultDetails; |
| 153 | +} |
0 commit comments