skip to Main Content

This is the bug I’m facing I do not understand why the params are not fully gotten with the cypress wait function

thenfunction(){}
AssertionError
expected ’19/0′ to equal ’19/01/2028′

/* eslint-disable mocha/no-setup-in-describe,no-undef,mocha/no-skipped-tests */
// noinspection ES6ConvertRequireIntoImport
require('chai/register-expect');
const {basicResponseObj, bodyObjSearch2} = require("./common");


describe('Stubbed suite', function() {
    // Step 1: setup the application state
    beforeEach(function () {

        cy.clearCookies();
        cy.clearLocalStorage();
  
        cy.intercept('GET', '*/entries?usage=basic*', {body: basicResponseObj});
        cy.intercept('GET', '*/entries?usage=search*', {body: bodyObjSearch2}).as("searchResults");;
        cy.visit('search-test');
    });

    describe('Search by date range', function () {

        it("Fill filter and check query param is filled", () => {
            cy.get('#date-from').type("19/01/2028")
            cy.get('#date-to').type("19/01/2028")
            cy.get('#search-btn').click();
            cy.wait("@searchResults").then((req) => {
                expect(req.request.query.dateFrom).to.eq("19/01/2028");
              });
          });
   
          });
        })

 

2

Answers


  1. Chosen as BEST ANSWER

    How I managed to solve this is with adding the api interception to a beforeEach hook to make sure it is used by every test case and changing the first hook beforeeach to a before hook.

    describe('Stubbed suite', function() {
        // Step 1: setup the application state
        before(function () {
    
            cy.clearCookies();
            cy.clearLocalStorage();
      
            cy.intercept('GET', '*/entries?usage=basic*', {body: basicResponseObj});
            cy.intercept('GET', '*/entries?usage=search*', {body: bodyObjSearch2}).as("searchResults");;
            cy.visit('search-test');
        });
    
        describe('Search by date range', function () {
            beforeEach(function() {
                cy.intercept('GET', '*/entries?usage=search*', { body: bodyObjSearch2 }).as("searchResults");
              });
            it('Type date from and date to', () => {
                cy.get('#date-from').type("19/01/2028");
                cy.get('#date-to').type("20/01/2028");
                
            })
    
            it("Check query param is filled", () => {
                cy.get('#search-btn').click();
                cy.wait("@searchResults").then((req) => {
                    console.log(req.request.query)
                    expect(req.request.query.dateFrom).to.eq("19/01/2028");
                    expect(req.request.query.dateTo).to.eq("20/01/2028");
                  });
              });
       
              });
            })
    

  2. It’s probably not the intercept that fails, more likely the type needs a delay or some other adjustment.

    To check, add an assertion after type()

    cy.get('#date-from').type('19/01/2028')
    cy.get('#date-from').invoke('val').should('eq', '19/01/2028')
    

    then add in delay to make the .type() stable

    cy.get('#date-from').type('19/01/2028', {delay, 100})
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search