4

I am trying to go to different urls and compare screenshots with baseline image using matchImageSnapshot. Issue here is when one of my url fail with matchimagesnapshot -it is not continuing to next URL.NOTE:I tried to run with variable --env failOnSnapshotDiff=false -issue with this approach is -i wont get any clue which url failed unless i go manually and check diff folder.

Is there any way i can achieve this Say for eg i have 3 urls in my sample - i want to continue to next url even if url1 failed and i need to get a error for failed urls? Thanks for any help

beforeEach(function() {
    cy.viewport(1680,1050);


  });


  const pages=[
    "https://URL1",
    "https://URL2",
    "https://URL3",
    "https://URL4"
]



describe('screencheck', () => {
    it('scree', () => {


        cy.login().then(()=>{
            pages.forEach((page)=>{
            cy.setResolution([1680,1050]);
            cy.visit(page);
            cy.wait(30000);

        cy.get('.itl-exit-info-panel > .ng-scope').then(()=>
        {


                cy.get('.itl-exit-info-panel > .ng-scope').root().matchImageSnapshot(page);


           // })
        })  


            });

        });

    })  
})
user8710571
  • 405
  • 2
  • 12
  • 25

4 Answers4

3

Cypress uses Chai assertions. What you need is a soft assertion (continue execution even after failure). Chai doesn't support soft assertions. You would need to use a NPM library called soft-assert: https://www.npmjs.com/package/soft-assert

0

Sameer Wakude already pointed to the soft assertion. Another option can be to put every screenshot test in a seperate it(). Since an it() may fail and won't stop the rest of the it()s to stop.

Mr. J.
  • 3,499
  • 1
  • 11
  • 25
0
if anyone is still facing issues with this. here is how I handled it.

I am using cucumber in my project
Scenario: "...."
Given step 1
When step 2
Then step 3

Step 1 has a corresponding step definition that calls the below functions
//here I am only saving a boolean value if the element is found or not. though the element is not found this code won't stop the execution
assertElementExists(newIndPartyStoredData: bisIndividualParty) {
    let elementExist = false
    cy.get('body').then($body => {
        if ($body.find("identifier").length > 0) {
            cy.get("identifier").then($val => {
                partyNameCell = true;
                expect("Some text").to.equal($val.text().trim());
            });
        }
//this wraps the boolean value to be used later
        cy.wrap(partyNameCell).as("partyNameCellVisible");
    });
}

Step 2 calls the following function
clickExitBISIntegrityRequestBtn(){
cy.get("some button").click()
}

Step 3 calls the below function 
assertElementSearchIsSuccessful() {
    cy.get("@partyNameCellVisible").then($visibility =>{
        if (!$visibility){
        throw new Error("Party name could not be found in BIS table.")
        }
    })
}

We are returning a boolean value in step 1 if we have a failure. Step 2 continues to execute and an error is thrown based on the boolean value in step 3
Raghu Ram.k
  • 255
  • 3
  • 8
-2

Besides the options mentioned by the guys above. You also can do the following. First, in the cypress command file, create a function like this to verify if the element is present.

Cypress.Commands.add('isElementVisible', (element) => {
    cy.get('body').then($body => {
        if ($body.find(element).length) {
            return true
        } else {
            return false
        }
    })
})

Next, in your test file you can define two arrays, one with the values that you expect to have in the end of the test when finishes. And other to save those values when your condition is met.

const qExpected = ['q1Passed', 'q2Passed', 'q3Passed']
const qPassed = []

 // isElementVisible receives the css locator required to find the element, in this case saved into the variable fixture.errorAlert. Then if the element is present you put one element of the first array within the second one.
 if (cy.isElementVisible(fixture.errorAlert)) {
        qPassed.push(qExpected[0])
 }

// Thereafter, you repeat the same condition as many time as needed and according the number of element defined in your array.

// Finally, you if compare the first array contains all the element that you were expected. So that your test does not fail until this point.
expect(qPassed).to.have.members(qExpected)
Vict01
  • 300
  • 1
  • 10
  • Did you test this? – user16695029 Jun 18 '22 at 20:25
  • Yes. In addition you can also check this answer related to the use of soft-assert as the other guys mentioned, but here below I think I put it clearer and with example included. https://stackoverflow.com/a/72673547/12936130 – Vict01 Jun 19 '22 at 00:23
  • 1
    I did too, and it **does not work**. `if (cy.isElementVisible(fixture.errorAlert)) {` will never be false. – user16695029 Jun 19 '22 at 00:32
  • Did you try in this way and by following all the steps mentioned in my link before about soft-assert? // Replace .site-description by your own css locator cy.isTheElementPresent('.site-description').then(isTrue => { cy.softAssert(isTrue, true, "expected this element to be present!"); }) – Vict01 Jun 19 '22 at 00:40
  • I think what have here is erroneous. Hoping you will fix it. – user16695029 Jun 19 '22 at 01:05