1

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

Achtung
  • 199
  • 9
Ravi
  • 29
  • 6
  • 1
    I don't see how you are using the alias for each stubbed network request. You can rename the second `getStyleFileSubTaskStatus` to something else. – jjhelguero Mar 24 '23 at 15:05
  • 1
    That works fine - what is your question? Cypress will change the fixture according to the last `cy.intercept()` you define. – Achtung Mar 25 '23 at 07:07
  • 1
    But you must add `cy.wait('@getStyleFileSubTaskStatus')` to prevent flaky testing, as shown in Ged's answer – Achtung Mar 25 '23 at 07:11
  • @Achtung Yeah i also saw Cypress can do it. Tried but could not get a result. Instead of that it took the initial fixture value. – Ravi Mar 25 '23 at 14:22
  • What version of Cypress are you using? It works with the latest version, but note that only recently the intercept command was "fixed" to allow override of an existing intercept with the same matcher. – Achtung Mar 25 '23 at 20:45
  • @Achtung I am using version 12. I think I should try again. – Ravi Mar 27 '23 at 16:10

1 Answers1

5

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');
    });
  });
});
  • Thanks for your reply. Here, Should I add this "fixtureFilename" always in the first place of the test script starting? If I added it like that before I click the element the data is loaded. I have some confusion here. Could you please explain me? – Ravi Mar 29 '23 at 07:12
  • 1
    Yes it should be in the order shown - first `let fixtureFilename = the-first-value`, then later to change the fixture `fixtureFilename = the-second-value` . The line starting `let fixtureFilename ` must be in-scope for both times you want to set it's value. – Ged.Delaney Mar 31 '23 at 05:16