question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

contains() not working with filter and RegExp

See original GitHub issue

Current behavior

The .contains() command currently only works when split into .get() and .contains() with Regular Expression.

Desired behavior

I’d like to use a single command to search for an element with a regular expression. The RegExp is needed to distinguish “edit & save” and “edit” f.e. without using an index. The single .contains() also works, when including the final html tag the text is within like so: main .container p

Test code to reproduce

it('on cypress website', () => {
    cy.visit('https://www.cypress.io/');
    cy.get('main .container').contains(new RegExp('^Cypress is the new standard in front-end testing that every developer and QA engineer needs.$', 'm'));
  });

works but this doesn’t:

it.only('on cypress', () => {
    cy.visit('https://www.cypress.io/');
    cy.contains('main .container', new RegExp('^Cypress is the new standard in front-end testing that every developer and QA engineer needs.$', 'm'));
  })

Cypress Version

8.7.0

Other

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mjhenkescommented, Jan 5, 2022

@Maw-Jung, thanks for your reproducible code. I’ve been able to to recreate this in cypress 9.2.0.

additionally this test case works as expected

  it('it succeeds with a string', () => {
    cy.visit('https://www.cypress.io/');
    cy.contains('main .container', 'Cypress is the new standard in front-end testing that every developer and QA engineer needs.');
  });

The Contains code is located here: https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/cy/commands/querying.ts#L447

0reactions
Maw-Jungcommented, Jul 5, 2022

@sainthkh no worries, i wasn’t in time, either. The problem occurs with the generated tree, which i have no control over. Thus i can only identify reoccuring elements by content further down the node. I’ll try to add an simplified example: ul> li> span>banana</span /li> li> span>pineapple</span /li> /ul> I hope this makes it a bit more clear, i need to grab the li-elements by identifying the correct one by it’s content. This example clearly lacks all the boilerplate dom around the described nodes, but i think it works as an example. cy.contains('ul li', new RegExp('^pineapple$', 'm')); What i want this to find is the element containing ‘pineapple’ within the subtree of ‘ul li’ to achieve a better performance with big trees. Thanks for your response, hope this makes it any clearer.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Regular expression to match a line that doesn't contain a word
The notion that regex doesn't support inverse matching is not entirely true. You can mimic this behavior by using negative look-arounds: ^((?!hede).)*$.
Read more >
Regular expressions for customizing and filtering alerts
Learn how to use regular expressions when filtering in Opsgenie and view sample definitions.
Read more >
Spark rlike() Working with Regex Matching Examples
Use regex expression with rlike() to filter rows by checking case insensitive (ignore case) and to filter rows that have only numeric/digits ...
Read more >
using regexp in where filter on an property not defined in the ...
Query with regex filter on properties not defined in model should work.
Read more >
Use regular expressions in filters and automated rules
When you set up filters or automated rules, you use pattern matching to specify ... such as "Show campaigns that contain ' -...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found