Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress filter selected ellements by severar included text

I have a select query of several lines something like this:

cy.get('#test-container').find('.row')

I want to find one, that contains some text (for example 'test title' and 'test value') in different subellements together. Or at least test that this row exist. Something like this:

cy.get('#test-container').find('.row').filter('include.text', 'test title').filter('include.text', 'test value')
like image 693
Atterratio Avatar asked Sep 11 '18 18:09

Atterratio


People also ask

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

How do I get an element's text contents? Cypress commands yield jQuery objects, so you can call methods on them. If the text contains a non-breaking space entity   then use the Unicode character \u00a0 instead of   . Tip: watch the Confirming the text with non breaking space entity video.

How do you search for text in Cypress?

Find multiple elements with text The command cy. contains yields the first element. If you want to find multiple elements that contain the given text, use jQuery selector :contains . Note that the selector text is case sensitive.

How do you filter tests in Cypress?

Tests may be filtered through the Cypress Module API by creating a JavaScript file containing Cypress run parameters. As an example, we will create a module which runs all tests within the Elements page module.


2 Answers

Use correct selector.

Ex:

cy
  .get('#test-container')
  .find('.row')
  .filter(':contains("test title")')

Try this!

like image 84
Mateus Moura Avatar answered Oct 12 '22 22:10

Mateus Moura


Cypress filter takes a selector as its parameter, it does not appear to match on text content of the DOM element.

Instead, you could use cy.contains with a regular expression.

cy.get('#test-container').find('.row').contains(/(?:test title|test value)/)

The parentheses and question mark is to indicate a non-capturing group, which matches on any of the items on either side of the pipe. MDN RegEx docs give more info.

like image 1
Craig Avatar answered Oct 12 '22 21:10

Craig