user8710571
user8710571

Reputation: 455

Cypress:Finding parent element

I got a tree of elements and each element got a toggle icon to expand it -My intention is to click on the toggle icon corresponding to the element have a text for ex "TIME PERIODS" Currently i write my code like below , Is there a better way to do this? Please see the screenshot for my element structure.

 cy.get('.tree-node',{ timeout: 60000 }).contains('TIME PERIODS',{force: true}).parent().parent().find('.tree-node-collapsed').click()

enter image description here

Upvotes: 2

Views: 2488

Answers (3)

user8710571
user8710571

Reputation: 455

We can do like shown below also with out using .each

cy.get('.tree-node').get('.tree-item').contains('Header').parent().siblings('.tree-node-collapsed').click();

Upvotes: 0

user8710571
user8710571

Reputation: 455

I have fixed issues -working code given below

cy.get('.tree-node').each(($el, index, $list) => {
    // $el is a wrapped jQuery element
     cy.wrap($el).get('.tree-item').contains('TIME PERIODS').parent().siblings('.tree-node-collapsed').click();

Upvotes: 0

QaElite
QaElite

Reputation: 26

each() method is available in Cypress.io. Using which we can travell through tree of elements and can filter using text. Please follow below code approach:

Code

   cy
    .get('.tree-node')
    .each(($el, index, $list) => {
        // $el is a wrapped jQuery element
       $el.get('.tree-item').contains('TIME PERIODS').siblings('.tree-node- 
       collapsed').click();
     });

Upvotes: 1

Related Questions