Answer:
cy.location().should((loc) => {
expect(loc).to.match(/https:\/\/www\.test\.com\/items\/(\d+).html/)
})
// or
cy.url().should('match', /https:\/\/www\.test\.com\/items\/(\d+).html/)
Explanation:
Your regex query is looking at the end of the string as well because you're using the $
selector, but your sample URL has .html
at the end. The choices are, to add .html
to the pattern, or remove the $
selector.
https://www.test.com/items/[number].html
^^^^
Try:
(https:\/\/www\.test\.com\/items\/)(\d)+
or at least remove the $
at the end for the end of the string match
/https:\/\/www\.test\.com\/items\/([0-9]+)/
Tested on regexr
Explanation (for question in comment):
expected /items.8655.html to include /items./[0-9]+/ FAIL
This pattern includes a .
between items.number
. If you need to cover this new variant and the previous one. (\/|\.)+
will match one or more, \
and or .
cy.url().should('match', /https:\/\/www\.test\.com\/items(\/|\.)+[0-9]+.html/)
Thus above change will match URLs that look like:
https://www.test.com/items.8655.html
https://www.test.com/items/8655.html
https://www.test.com/items/.8655.html