4

My URL contain a number that is assigned to a particular item so it might be 1, 2, ..., 999 and so on.

An example: https://www.test.com/items.889218.html

I want to make assertion like this:

cy.url().should('contain', '/items.').and('have', 'number')

I tried e.g.:

cy.url().invoke('text').should('match', /^[0-9]*$/)

or

cy.location().should((loc) => {
  expect(loc.pathname).to.contain(/^[0-9]*$/);
});

also to provide this kind of path:

"/items\.+[0-9]+.html/"

But both examples do not work. Any idea how to handle this kind of case?

Mag
  • 207
  • 1
  • 8

3 Answers3

4

The simplest regex for your example https://www.test.com/items.889218.html would be

cy.url().should('match', /https:\/\/www\.test\.com\/items.[0-9]+\.html/)

but if you're not concerned about the numbers this should also work

cy.url()
  .should('satisfy', (url) => url.startsWith('https://www.test.com/items.'))
  .and('satisfy', (url) => url.endsWith('.html'))

Ref reference TypeError: expect(...).to.startsWith is not a function

Ine Wilmann
  • 156
  • 6
0

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
Rohan Büchner
  • 5,333
  • 4
  • 62
  • 106
  • Hi, it desnt work. I have something like this in assertion: expected /items.8655.html to include /items./[0-9]+/ FAIL – Mag Dec 01 '22 at 08:11
  • The example you just posted is different to the original test URL. You'd need to alter the pattern to include the `.` between items and number. I'll post and example of this, but the best advice I can give for regex is to write it in a testing tool, and then validate your pattern with a few samples. https://regex101.com/r/bDyFJf/1 In this example I replaced the `/` with a conditional `(\/|\.)+` – Rohan Büchner Dec 01 '22 at 08:24
  • @Mag The pattern you provided would pass with a URL like `/items./8655.html` – Rohan Büchner Dec 01 '22 at 08:32
  • Still it dosnt work ;x https://www.test.com/items.8655.html to include /items(/|.)+[0-9]+.html/ FAIL My code: /items(/|.)+[0-9]+.html/ – Mag Dec 01 '22 at 10:18
  • I replaced (/|.) to \. and its looks better, but still failed: test.com/items.8655.html to include /items.+[0-9]+.html/ FAIL – Mag Dec 01 '22 at 10:22
0

You do not need to invoke the text content of the value yielded from cy.url, as it is already a string.

Additionally, your regex is incorrect. It is matching for only strings that begin, contain, and ends with numbers.

Fixing the above, you could have something like...

cy.url().should('match', /https:\/\/www\.test\.com\/items\/(\d*)\.html/)
agoff
  • 5,818
  • 1
  • 7
  • 20