Search code examples
javascriptcypress

Assertion failing when trying to assert an ID number in a URL Path


I am trying to do the following:
Assert the URL path after being redirected.

Here is an example of the Url: https://www.hello.com/test/test-result?testId=100

cy.location("pathname").should("eq", "/test/test-result"
cy.location("search").should("eq","/\?testId=\/\d+/)

The first assertion passes, but the second one fails.

The error I get is the following: expected ?testId=100 to equal /?testId=/\d+/

Can someone tell me what I am doing wrong? I thought using d+ would be good since it will match any number.


Solution

  • I think you should change the pattern matching ?testId=100 as follows:

    cy.location("search").should("match", /\?testId=\d+/)
    

    cy.location("search") should return the string: ?testId=100,
    which can be match against regex /\?testId=\d+/.