I am looking for a solution to fetch data from a tag which is having id as a unique attr and that id keeps on changing at run time
Here is the image of the tag I want to fetch value from and this keeps on changing and the tag has no other attributes to use in Cypress.
I tried xpaths and cy.get() but none is helping.
CodePudding user response:
If the element is part of a chart, there is little point in searching for the data-unique-id
attribute, there are probably lots of the same element on the page.
One approach is to use traversal commands to work down from the chart root element - can be tricky to work out, but will be reliable.
There's some example tests here bahmutov/chart-testing-example
Another approach is to make your data available to the test by putting it into a property of the window (called App Actions). This saves you having to "screen-scrape" the elements.
CodePudding user response:
If the structure of the id is consistent, you can use a regex with cy.get()
. In this case, I'm assuming the id
is always something like chart_XXXX_X
.
// including a g tag
cy.get(/g\[data-unique-id="chart_\d{4}_\d"\]/)
// not including a g tag
cy.get(/\[data-unique-id="chart_\d{4}_\d"\]/)