- 
                Notifications
    You must be signed in to change notification settings 
- Fork 4.8k
feat: toHaveURL predicate matcher #34413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from 9 commits
      Commits
    
    
            Show all changes
          
          
            16 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      afd19da
              
                Beginning of toHaveURL with predicate
              
              
                agg23 26d3a77
              
                Normalize error "expected" text
              
              
                agg23 efd3ab0
              
                Properly handle .not in toHaveURL
              
              
                agg23 883bb5f
              
                Working ignoreCase
              
              
                agg23 7ddbad2
              
                Testing error messages for consistency
              
              
                agg23 e83e203
              
                Improved diffing to match toMatchText
              
              
                agg23 f1f2bde
              
                Remove testing imports
              
              
                agg23 8f1a683
              
                Update docs
              
              
                agg23 2902480
              
                Type update
              
              
                agg23 ee91e2e
              
                Undo refactor and more closely follow other APIs
              
              
                agg23 b0133a6
              
                Fix docs according to PR comments
              
              
                agg23 f3b5838
              
                Remove accidental diff
              
              
                agg23 ded2fe2
              
                Remove unnecessary async
              
              
                agg23 a7559f7
              
                Fix tests
              
              
                agg23 c433224
              
                Remove now unused injectedScript command
              
              
                agg23 7dae7d1
              
                Fixed timeout test
              
              
                agg23 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /** | ||
|         
                  agg23 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| * Copyright (c) Microsoft Corporation. | ||
| * | ||
| * 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 type { ExpectMatcherState } from '../../types/test'; | ||
| import { kNoElementsFoundError, matcherHint } from './matcherHint'; | ||
| import { colors } from 'playwright-core/lib/utilsBundle'; | ||
| import type { Locator } from 'playwright-core'; | ||
| import { EXPECTED_COLOR, printReceived } from '../common/expectBundle'; | ||
| import { printReceivedStringContainExpectedResult, printReceivedStringContainExpectedSubstring } from './expect'; | ||
| import { callLogText } from '../util'; | ||
|  | ||
| export function toMatchExpectedStringOrPredicateVerification( | ||
| state: ExpectMatcherState, | ||
| matcherName: string, | ||
| receiver: Locator | undefined, | ||
| expression: string | Locator | undefined, | ||
| expected: string | RegExp | Function, | ||
| supportsPredicate: boolean = false | ||
| ): void { | ||
| const matcherOptions = { | ||
| isNot: state.isNot, | ||
| promise: state.promise, | ||
| }; | ||
|  | ||
| if ( | ||
| !(typeof expected === 'string') && | ||
| !(expected && 'test' in expected && typeof expected.test === 'function') && | ||
| !(supportsPredicate && typeof expected === 'function') | ||
| ) { | ||
| // Same format as jest's matcherErrorMessage | ||
| const message = supportsPredicate ? 'string, regular expression, or predicate' : 'string or regular expression'; | ||
|  | ||
| throw new Error([ | ||
| // Always display `expected` in expectation place | ||
| matcherHint(state, receiver, matcherName, expression, undefined, matcherOptions), | ||
| `${colors.bold('Matcher error')}: ${EXPECTED_COLOR('expected',)} value must be a ${message}`, | ||
| state.utils.printWithType('Expected', expected, state.utils.printExpected) | ||
| ].join('\n\n')); | ||
| } | ||
| } | ||
|  | ||
| export function textMatcherMessage(state: ExpectMatcherState, matcherName: string, receiver: Locator | undefined, expression: string, expected: string | RegExp | Function, received: string | undefined, callLog: string[] | undefined, stringName: string, pass: boolean, didTimeout: boolean, timeout: number): string { | ||
|         
                  agg23 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| const matcherOptions = { | ||
| isNot: state.isNot, | ||
| promise: state.promise, | ||
| }; | ||
| const receivedString = received || ''; | ||
| const messagePrefix = matcherHint(state, receiver, matcherName, expression, undefined, matcherOptions, didTimeout ? timeout : undefined); | ||
| const notFound = received === kNoElementsFoundError; | ||
|  | ||
| let printedReceived: string | undefined; | ||
| let printedExpected: string | undefined; | ||
| let printedDiff: string | undefined; | ||
| if (typeof expected === 'function') { | ||
| printedExpected = `Expected predicate to ${!state.isNot ? 'succeed' : 'fail'}`; | ||
| printedReceived = `Received string: ${printReceived(receivedString)}`; | ||
| } else { | ||
| if (pass) { | ||
| if (typeof expected === 'string') { | ||
| if (notFound) { | ||
| printedExpected = `Expected ${stringName}: not ${state.utils.printExpected(expected)}`; | ||
| printedReceived = `Received: ${received}`; | ||
| } else { | ||
| printedExpected = `Expected ${stringName}: not ${state.utils.printExpected(expected)}`; | ||
| const formattedReceived = printReceivedStringContainExpectedSubstring(receivedString, receivedString.indexOf(expected), expected.length); | ||
| printedReceived = `Received string: ${formattedReceived}`; | ||
| } | ||
| } else { | ||
| if (notFound) { | ||
| printedExpected = `Expected pattern: not ${state.utils.printExpected(expected)}`; | ||
| printedReceived = `Received: ${received}`; | ||
| } else { | ||
| printedExpected = `Expected pattern: not ${state.utils.printExpected(expected)}`; | ||
| const formattedReceived = printReceivedStringContainExpectedResult(receivedString, typeof expected.exec === 'function' ? expected.exec(receivedString) : null); | ||
| printedReceived = `Received string: ${formattedReceived}`; | ||
| } | ||
| } | ||
| } else { | ||
| const labelExpected = `Expected ${typeof expected === 'string' ? stringName : 'pattern'}`; | ||
| if (notFound) { | ||
| printedExpected = `${labelExpected}: ${state.utils.printExpected(expected)}`; | ||
| printedReceived = `Received: ${received}`; | ||
| } else { | ||
| printedDiff = state.utils.printDiffOrStringify(expected, receivedString, labelExpected, 'Received string', false); | ||
| } | ||
| } | ||
| } | ||
|  | ||
| const resultDetails = printedDiff ? printedDiff : printedExpected + '\n' + printedReceived; | ||
| return messagePrefix + resultDetails + callLogText(callLog); | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.