Home > Mobile >  Manipulate an element in cypress using text in the row
Manipulate an element in cypress using text in the row

Time:05-25

How can I access from the code below 'switch__obj' using content from row?

For example I want to manipulate with check()/uncheck() that 'switch__obj', which is a slider button, but find it with the help of content, kind of like cy.get('row').contains('Text wherewith find that').find('.switch__obj').uncheck({force:true})... ?

I tried with parent(), parents(), find(), children() but I couldn't find it.

 <div _ngcontent-iil-c662="" >
       <div _ngcontent-iil-c662="" ><label _ngcontent-iil-c662="" for="chkOnlineBooking" >Allow patients to book appointments online</label></div>
       <div _ngcontent-iil-c662="" >
          <div _ngcontent-iil-c662="" >
             <div _ngcontent-iil-c662="" ><label _ngcontent-iil-c662="" ><input _ngcontent-iil-c662="" type="checkbox" ng-reflect-model="true" ng-reflect-is-disabled="false" ng-reflect-options="[object Object]" ><span _ngcontent-iil-c662="" data-off="No" data-on="Yes" ></span></label><label _ngcontent-iil-c662="" for="chkOnlineBooking"></label></div>
          </div>
       </div>
    </div>

I hope I have given all the necessary information. Thank you!

Note: I used this code and working, but i want to find it based on text, and not with eq() option cy.get('.switch__obj').eq(0).parent().find('input').uncheck({force:true})

CodePudding user response:

You can do something likle this:

cy.contains('div', 'Allow patients to book appointments online')
  .parent() //Moves to parent div row
  .within(() => { //scopes the commands within the above div row
    cy.get('.switch__obj').click()
  })

CodePudding user response:

Identify the row with correct text using contains(), specifying .row as the element to return.

Then just .find() the input directly in that row.

cy.get('.row', 'Allow patients to book appointments online')
  .find('input[type="checkbox"]')
  .check()

You can actually omit [type="checkbox"], but it reads better when later reviewing your test - you know that .check() is the correct action, not .click().

  • Related