Skip to main content

Use of for Loop & return in Cypress Automation



 Use of  For Loop & return method in Cypress automation :

describe(" Use Of For Loop and return method ",function(){
 
it('Test Case 15'function() {
cy.visit('www.exapmle.com/',{timeout:1000});
 
    cy.get('.tab-tools > .icon > .svg-inline--fa').click({force:true});
// Now this method will fetch the text which is already displayed & will save it.
    cy.get('.last-updated-date > span').then(($txt=> { const txt = $txt.text()
    cy.log(txt)
        
        // For loop will run 50 times & after each 3 seconds
// it will check Visibility of element.
// If it gets newly appeared element/Text It will be terminated before 50 runs

var i;
        for (i = 0i < 50i++) {
            cy.wait(3000)
            
            cy.get('.last-updated-date > span').each(($text=> {
 const lastupdate = $text.text()
             cy.log(lastupdate)

// If the New text/element is appeared then Compare it with first element
// and return/exit for loop.

            if ($text.text() ==! txt) {
                cy.wrap($text)
                cy.log('Test Case is Failed')
                return;
            }
            else {
                return;
            }
            return false;
        }) 
      }
// The page will be reload when for loop is terminated
// You can also Skip this one.
         cy.reload();
         cy.log('Test Case is Passed !')     
    });
       
});
});

Comments

Popular posts from this blog

The terminal process "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" terminated with exit code: 259.

Integrated terminal in VS code crashes PowerShell: Solution of this Problem:- 1) Open your Project's/file location. 2)Click in this bar after opening the exact location of your project.                                                                                                                                         3)Now, Write 'cmd' in this bar.                                                                                                                     4)Your Terminal will be open as command, Now you can give any command here related to your Project. 5) Your project will be Executed/Start Normally in Terminal                                                                            6) You can ask any question.

Test files sequence in Cypress Or Test file Order in Cypress

  In any case, I used the  testFiles  config in  cypress.json  to set the spec file run order:   You can use your file names. Only the files will be shown in cypress runner which you will define here "New_Api" is a Sub-folder in integration folder. All other are spec files. {      "baseUrl" : "https://exapmle.com" , "testFiles" :  [          "New_Api/*" ,          "Add_Product.spec.js" ,          "Add_Client.spec.js" ,          "Seller.spec.js" ,          "Deal_Status.spec.js" ,          "Buyer_Offer.spec.js" ,          "Buyer_2.spec.js" ,          "Deal_Status.spec.js"            ], }