Home > Blockchain >  Check if URL contains this OR this in Cypress
Check if URL contains this OR this in Cypress

Time:08-04

In Cypress 10, how can I check that a URL contains something, or something, or something? I know how to check a URL for one thing:

cy.url().should('contains', '/inventory.html');

But how do I modify this to check if the url contains /inventory.html OR /cheese.html OR /sunshine.html, without using a bunch of if else statements?

CodePudding user response:

You can do like this:

cy.url().then((url) => {
  expect(url).to.contain.oneOf(['/inventory.html', '/something.html'])
})

CodePudding user response:

Use contain.oneOf like this:

cy.url().should('contain.oneOf', ['/inventory.html', '/cheese.html'])

If you generally want to find an assertion syntax look here, e.g oneOf.

Cypress gives you shortcut of the same syntax with .should() but more important, it will retry the assertion.

If you use .then() there is no retry.

  • Related