Ugurcan
Ugurcan

Reputation: 81

Cypress - Waiting for an element which has same attributes with other elements already existed

This problem that I'm facing is common for me and I want to learn for about best practices.

My problem is:

I have to wait a text which has an attribute of ".title" class and the text involves the statement of: "Hello". Before triggering this element to come to surface, we have an element already have attributes of ".title" which have a text of "StatementX" as well (At the end of the process, I have 2 ".title" class items on screen).

When I tried to wait for the element "Hello", I write:

`cy.get('.title').contains('Hello').should('be.visible')
`

Since "StatementX" is already on the screen, Cypress finds ".title" class and does not check "contains" part. What is the best practice to handle such cases?

Thank you so much

Upvotes: 1

Views: 215

Answers (2)

BlueReddington
BlueReddington

Reputation: 79

If you don't have any difference at all in those elements you will have a return of an array of elements.

In that part .eq(NUMBER OF THE ARRAY) you can validate the option that you want.

Also you can use XPATH for those cases. Not recommended since it's volatile but since is old stuff that is not changed it should have an higher change of stability.

Upvotes: 0

Chloe
Chloe

Reputation: 151

If you move the class .title into command .contains() it will focus solely on the element you wish to test, i.e two criteria will be tested in one command call and it will find the specific element.

cy.contains('.title', 'Hello').should('be.visible')

Upvotes: 4

Related Questions