1

in my automation, I'm trying to find an element with a specific text on a page.
to be more specific: every page is a movie info page and has a list of actors and I'm trying to find a specific actor.
if found I will count it - count ++.
if not I will move to the next page and try to find it there.
again and again, until I searched all of the pages.
The problem I encountered is how to get the text of the elements, and whenever the automation does not find the text it crashes.

the element : <a data-testid="title-cast-item__actor" href="/name/nm2794962?ref_=tt_cl_t_1" class="StyledComponents__ActorName-y9ygcu-1 eyqFnv">Hailee Steinfeld</a>

the one thing that separates the identification of the elements is the inner text (the name of the actor)

1

1 Answer 1

1

Something like this:

let x = 0;
cy.visit('/your/pages')
cy.get('[data-testid="title-cast-item__actor"]')
  .contains('Hailee Steinfeld')
  .then(() => cy.log(`Count: ${++x}`));

or with visiting all your pages:

let x = 0;
const pages = ['/page1', '/page2'];
cy.wrap(pages).each((page) => {
  cy.visit(page);
  cy.get('[data-testid="title-cast-item__actor"]')
  .invoke('text')
  .then((text) => {
    cy.log(`Actor: ${text}`);
    if (text === 'Hailee Steinfeld') {
      x++;
    }
    return cy.wrap(x);
  })
}).should('be.greaterThan', 0);

But be aware that conditional testing is something that is not recommended by cypress. See https://docs.cypress.io/guides/core-concepts/conditional-testing#The-problem

Sign up to request clarification or add additional context in comments.

1 Comment

@yuv: If this solves your problem, please mark this answer as accepted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.