Cypress object vs JQuery object, role of cy.wrap function

Viewed 1295

I am new to cypress and learning it by testing different methods. If any one can help me with the following questions

1.I have learn that most of the cypress object used the jquery objects for DOM but what is exactly the difference between them and what difference it will have in performance? 2.I have used cy.wrap function so i can use should method for the assertion , else i have to use expect method , any leads when to use which one ?? because with then() function i cannot use should method

for cy.wrap , according to cypress documentation the definition is "Invoke the function on the subject in wrap and return the new value" , what exactly the role of cy.wrap is ??

In my sample code below i have used both should() and expect() for assertion

Thanks in advance.

     cy.contains('nb-card','Using the Grid').then(firstForm =>{
            const emailLabelFirst = firstForm.find('[for="inputEmail1"]').text()
            const passwordLabelFirst = firstForm.find('[for="inputPassword2"]').text()
            // jquery object assertion is done cia expect method
            expect(emailLabelFirst).to.equals('Email')
            expect(passwordLabelFirst).to.equals('Password')

            cy.contains('nb-card','Basic form').then(secondForm=>{

                const passwordLabelSecond = secondForm.find('[for="exampleInputPassword1"]').text()
                expect(passwordLabelFirst).to.equal(passwordLabelSecond)
               // cy.log('This is test logging message')

               // with cypress object assertion is done via should method
               cy.wrap(secondForm).find('[for="exampleInputPassword1"]').should('contain','Password')
                
            })
           
            
           
        })
1 Answers

You asked a lot of questions, I don't even know if I understood them all well, but let's see some of the answers.

.should() vs .expect()

.should() is considered an implicit assertion and .expect() explicit. It's more in detail explained here. .should() is great in that it waits and retries, which you can't achieve with .expect(), nor with .then().

Typically, you use .expect() when you want to perform multiple assertions on the same subject. Like you want to assert that a response status code is 200 and that there's some particular property in the response body:

cy
  .request(url)
  .then(res => {
    expect(res.statusCode).to.equal(200);
    expect(response.body).to.have.property('name', 'pavel');
  });

cy.wrap()

cy.wrap() really allows you to keep chaining Cypress commands, just like you did with .find(). There're examples in the documentation, have a look.

Related