Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cy.contains match with regex?

Tags:

cypress

I am trying to match part of a url http://www.mywebsite.com/get-stuff in cypress and haven't been able to figure out how to code a regex match.

I tried: cy.contains('http.*get-stuff') and don't find a match for do some things

like image 444
washingon Avatar asked Mar 06 '18 00:03

washingon


People also ask

How do I get an element's text contents in Cypress?

Cypress can validate the text on an element with the help of jQuery text() method. This method shall help us to fetch the text content on the selected element. We can also put assertions on the text content of the element. cy.

How do you use contain in Cypress?

contains() is chained off of a command that yielded the <button> , Cypress will look inside of the <button> for the new content. In other words, Cypress will look inside of the <button> containing "Delete User" for the content: "Yes, Delete!", which is not what we intended.

How do you match a number in regex?

\d for single or multiple digit numbers To match any number from 0 to 9 we use \d in regex. It will match any single digit number from 0 to 9. \d means [0-9] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [0-9] where [] is used for character range.


2 Answers

If you are trying to see if some content on your website has the text http://www.mywebsite.com/get-stuff using regex, you will need to pass in a valid Regular Expression. Your argument is attempting to match using a glob expression.

If you are trying to see if the url of your website is navigated to http://www.mywebsite.com/get-stuff, you likely want to write an assertion off of the cy.url() command like so:

cy.url().should('match', /myregexp/)
like image 193
Jennifer Shehane Avatar answered Oct 04 '22 02:10

Jennifer Shehane


I know it's been quite a long, but for those who are still looking for a solution ( just like me ), you can make use of cy.url().should('contain', /regex/) if the accepted solution didn't work for you.

like image 22
Arun Ramachandran Avatar answered Oct 04 '22 04:10

Arun Ramachandran