Search code examples
automated-testscypressui-automationfixturescypress-intercept

How to update the fixture for an existing intercept


I added one fixture in before each and now I need to get the updated results of the same fixture file which I added in the test script. Is this possible in Cypress? Could anyone tell me that how to do it properly If this kind of scenario is possible in Cypress?

context('Style File', () => {
  describe('Style File - Account Manager', () => {
    beforeEach(() => {
        
         cy.intercept("GET", "**/api/inquiries/*/style_file_sub_task_status", {

        fixture: "style_file_sub_task_status.json"

      }).as('getStyleFileSubTaskStatus');

    });

      it('Sub task Un-Approving', function () {

        cy.intercept("GET", "**/api/inquiries/*/style_file_sub_task_status", {

            fixture: "style_file_sub_task_status_already_approved.json"

         }).as('getStyleFileSubTaskStatus');

      cy.xpath('(//span[normalize-space()=\'Approved tech pack\'])[1]')
        .click();
      cy.get('#status').click();
      cy.findByText("Approved tech pack sub task status updated successfully")
        .should('be.visible');
    });


  });
});

In the above scenario first I added ("style_file_sub_task_status.json") fixture in the before each and now I need to get an updated result of the same route and same fixture ("style_file_sub_task_status_already_approved.json") added in the test script


Solution

  • The fixture can be dynamic (from variable) if you use a (req) => {...} callback.

    Each time the intercept fires, the fixture name is checked in the callback.

    context('Style File', () => {
    
      let fixtureFilename = 'style_file_sub_task_status.json'
    
      describe('Style File - Account Manager', () => {
    
        beforeEach(() => {
          cy.intercept("**/api/inquiries/*/style_file_sub_task_status", (req) => {
            req.reply({fixture: fixtureFilename})
          })
          .as('getStyleFileSubTaskStatus');
        });
    
        it('Sub task Un-Approving', function () {
    
          fixtureFilename = 'style_file_sub_task_status_already_approved.json'
          // existing intercept will do
          
          cy.xpath('(//span[normalize-space()=\'Approved tech pack\'])[1]')
            .click();
          cy.get('#status').click();
    
          cy.wait('@getStyleFileSubTaskStatus')     // don't miss this out!
    
          cy.findByText("Approved tech pack sub task status updated successfully")
            .should('be.visible');
        });
      });
    });