Cypress: Add "Start-with" to Cypress in order to get DOM elements

Created on 8 Oct 2018  路  2Comments  路  Source: cypress-io/cypress

Feature

Other frameworks like Selenium or Katalon have an option to search IDs, classes or elements not only by their whole text but parts from them using the "startWith" command.

This is really helpfull when dealing with dynamic IDs.

In my particular case, I am creating a document in one of my test cases, and the ID of that test changes on every iteration of the test suite. At the moment I cannot control it with the tools available, however, if I could search by startWith('row_') I could filter the search so it only offers one result and the search would be a success.

Current behavior:

There is no native way to search a DOM element by a startWith command and you are required to make a perfect match.

Desired behavior:

Add a command that allows to search elements by a part of their ID or class, ideally startWith.

Most helpful comment

You can use a CSS attribute selector to achieve this:

cy.get('[id^=row_]')

will select an element with an id that starts with row_.

You can create a custom command to abstract it into something like cy.startsWith('id', 'row_') if you'd like.

All 2 comments

You can use a CSS attribute selector to achieve this:

cy.get('[id^=row_]')

will select an element with an id that starts with row_.

You can create a custom command to abstract it into something like cy.startsWith('id', 'row_') if you'd like.

Thanks a lot, that will definetly help, specially the custom commands for future cases!

Was this page helpful?
0 / 5 - 0 ratings