Power of “Wrap” in Cypress
Let’s discuss something with the “Wrap” method in Cypress Framework.
Wrap yields the object passed into it…
Any one of the objects, that pass within the “Wrap” method, will actually get its actual object properties & members out from it.
I will use simple examples to get an idea of this method.
Example 1:
If you want to verify if the value that you have got from a JSON / HTTP request/ XHR, there we can verify whether it's JSON property the name has some value.
Here validating the name “niluka” in a particular JSON file, and then you found get all the value out from it. So we can easily do with “Wrap” method.
Verify value from property.
cy.wrap({name : 'niluka'}).should('have.property', 'name').and('eq', 'niluka');
Example 2 :
Getting the table, and finding the “tr” with all the “td”s, and then we are getting all the “td”s properties/ UI element properties using the “Wrap” method.
cy.get(".table").find("tr > td").then(($td) => {cy.wrap($td).contains("niluka").invoke("wrap").parent().contains("names").click();})