Skip to content
This repository has been archived by the owner on Oct 13, 2022. It is now read-only.

Commit

Permalink
fix: show selector in the assertion message (#118)
Browse files Browse the repository at this point in the history
Co-authored-by: JessefSpecialisterren <[email protected]>
  • Loading branch information
bahmutov and JessefSpecialisterren committed Nov 4, 2020
1 parent 082253b commit 040b298
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
12 changes: 12 additions & 0 deletions cypress/integration/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ describe('cypress-xpath', () => {
})
})

it('logs the selector when not found', (done) => {
cy.xpath('//h1') // does exist
cy.on('fail', (e) => {
if (e.message !== 'Timed out retrying: Expected to find element: `//h2`, but never found it.') {
return done(e)
}
// no errors, the error message for not found selector is correct
done()
})
cy.xpath('//h2', { timeout: 100 }) // does not exist
})

it('should not log when provided log: false', () => {
cy.spy(Cypress, 'log').log(false)

Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ const xpath = (subject, selector, options = {}) => {
return Cypress.Promise.try(getValue).then(value => {
if (!isPrimitive(value)) {
value = Cypress.$(value)
// Add the ".selector" property because Cypress uses it for error messages
value.selector = selector
}
return cy.verifyUpcomingAssertions(value, options, {
onRetry: resolveValue,
Expand Down

0 comments on commit 040b298

Please sign in to comment.