Nightwatchjs - how to wait for the end of an ajax call

I am using nightwatchJS to automate the browser. One of the common phenomena that I see, most of the content on my web page is updated through data from an ajax call. So, in my testing, I am looking for a way to test until I get the result from Ajax. I could not find any api in the nightwatch or selenium for this.

I tried with waitForElementVisible, but I feel that this will not be enough. What happens if my ajax call returns no data.

Has anyone tried this before?

+4
source share
3 answers

ajax, , , "ajaxComplete" :

client
    .timeoutsAsyncScript(15000) // set async exec timeout
    .click('.btn-submit') // ajax form submit
    .executeAsync(
        function(targetUrl, done){
            var nightwatchAjaxCallback = function(ajaxUrl) {
                if(ajaxUrl.indexOf(targetUrl) === 0) { // if url match notify script that xhr is done
                    done(true);
                }
            };

            if(!window.nightwatchAjaxInited) { // make sure ajaxComplete is attached only once

                window.nightwatchAjaxInited = true;

                $(document).ajaxComplete(function(e, res, req) {
                    nightwatchAjaxCallback(req.url);
                });
            }
        },
        ['/ajaxpath'], // expected xhr request path
        function(status){
            // executes once ajax is done
            client.verify.containsText('.entry', 'lorem ipsup...') // verify post is created
        }
    );

'ajaxWait', :

exports.command = function(targetUrl, action, callback) {

    this.timeoutsAsyncScript(15000);

    action();

    this.executeAsync(function(targetUrl, done){

        var nightwatchAjaxCallback = function(ajaxUrl) {
            if(ajaxUrl.indexOf(targetUrl) === 0) {
                done(true);
            }
        };

        if(!window.nightwatchAjaxInited) {

            window.nightwatchAjaxInited = true;

            $(document).ajaxComplete(function(e, res, req) {
                nightwatchAjaxCallback(req.url);
            });
        }

    }, [targetUrl], function(status){

        callback();
    });
};

:

client.ajaxWait('/ajaxpath', function(){
    // ajax form submit
    client.click('.btn-submit') // ajax form submit
}, function(){
    // executes once ajax is done
    client.verify.containsText('.entry', 'lorem ipsup...') // verify post is created
})
+1

, : https://github.com/mobify/nightwatch-commands

, adaptive.js, .

nightwatch: 1.6.0 .json . json

"custom_commands_path": "./node_modules/nightwatch-commands/commands",
"custom_assertions_path": "./node_modules/nightwatch-commands/assertions",
0

, :

1) , / , ajax. , ajax - .

client.pause(5000); .

2) adaptive.js, , waitForAjax nightwatchJS. , , .

3) --verbose, , . Verbose , , .

-1

Source: https://habr.com/ru/post/1609615/


All Articles