Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress - How to wait for XHR request

I'm a newbie to cypress, so please be patient with me. ;-) I'm sure it is a simple question and I already read the documentation of cypress, but something still seems to wrong in my cypress test. I want to wait for an xhr request to be finished, when I click on a different language of the page I want to test. It works, when I use wait(5000), but I think, there is a better way to wait for the xhr request to be finished than fix wait 5 secs. This is my code. Any help or hints are appreciated:

describe('test',() => {
    it('should open homepage, page "history", click on English language, click on German language',() => {
        cy.server();
        cy.route('POST','/ajax.php').as('request');
        cy.visit('http://localhost:1234/history');
        cy.wait('@request');
        cy.get('div[class="cursorPointer flagSelect flag-icon-gb"]').click({force:true});
        cy.route('POST','/ajax.php').as('request');
        cy.wait(['@request']);
        //cy.wait(5000); // <- this works, but seems to be not the best way
        cy.get('h2').should(($res) => {
            expect($res).to.contain('History');
        })
        cy.get('.dataContainer').find('.container').should('have.length', 8);
    });
});

The last check

cy.get('.dataContainer').find('.container').should('have.length', 8);

is not successful, because the xhr request is not yet finished. The xhr request is being fired, when the click on the icon is done:

cy.get('div[class="cursorPointer flagSelect flag-icon-gb"]').click({force:true});

Here an image of the xhr request, if that helps to find the error:

enter image description here

Thanks for any help.

like image 702
dns_nx Avatar asked Jan 23 '20 06:01

dns_nx


People also ask

How do you handle XHR request in Cypress?

An object in XHR can request data from a server in the form of a response. Cypress can not only be used for front end automation, but also can control the network traffic by directly accessing the XHR objects. Then, it applies the assertions on the objects.It can mock or stub a response.

How do you wait for an element in Cypress?

Wait for API response Cypress works great with http requests. If you are waiting for some resources to be loaded in your app, you can intercept a request and then create an alias for it. That alias will then be used with . wait() command.

How do you wait for a request to finish before moving on with Cypress?

Then, right after logging into the application, I use cy. wait() , passing the alias created previously ( @getNotes ). That way, Cypress will wait for such a request to end before moving on to run the test that successfully creates a note.


1 Answers

Are you sure that this line is correct? Otherwise the cy.wait won't function as you want.

cy.route('POST','/ajax.php').as('request');

I expect something like

cy.route('GET','/endpoint').as('request');

You can lookup what route is it via developer tools (F12 in Chrome). Go to network to monitor what kind of XHRs load when you open your page.

Find out request URL and Method - example with bing.com

Also: I prefer to include the cy.server() and cy.route() command in the beforeEach. Then you only need the cy.wait() in the test itself. See https://docs.cypress.io/guides/references/best-practices.html#2-Run-shared-code-before-each-test for more information about that.

like image 166
Boyd K. Avatar answered Sep 25 '22 02:09

Boyd K.