简体   繁体   English

nightwatchjs - 如何等待ajax呼叫完成

[英]nightwatchjs - how to wait till ajax call is completed

I am using nightwatchJS for browser automation. 我正在使用nightwatchJS进行浏览器自动化。 One common usecase I am seeing is, most of the content in my webpage are updated through data from ajax call. 我看到的一个常见用例是,我网页中的大部分内容都是通过ajax调用的数据进行更新的。 So, in my testing, i am looking for a way to hold my testing until i get result from Ajax. 所以,在我的测试中,我正在寻找一种方法来保持我的测试,直到我从Ajax获得结果。 I could not find any api in nightwatch or selenium for this. 我没能在夜间观察或硒中找到任何api。

I have tried with waitForElementVisible , but I feel this will not be suffice. 我尝试过waitForElementVisible ,但我觉得这还不够。 What will happen if my ajax call doesn't return any data. 如果我的ajax调用没有返回任何数据,会发生什么。

Has anybody tried this before? 以前有人试过这个吗?

If you know ajax path here is the way how it can be solved, the idea is to attach 'ajaxComplete' event to client and match executed request path: 如果您知道这里的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
        }
    );

Here is command named 'ajaxWait' created from the code above: 这是从上面的代码创建的名为'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();
    });
};

and call should look like this: 并且调用应该如下所示:

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
})

We have a custom command available here: https://github.com/mobify/nightwatch-commands 我们在这里有一个自定义命令: https//github.com/mobify/nightwatch-commands

We have been working to decouple our commands from adaptive.js specific requirements so anyone can use them in their projects. 我们一直在努力将我们的命令与adaptive.js的特定要求分离,以便任何人都可以在他们的项目中使用它们。

You will need to include nightwatch-commands: 1.6.0 in your package.json and reference the custom commands in your settings.json 您需要在package.json中包含nightwatch命令:1.6.0,并在settings.json中引用自定义命令

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

I am facing similar problems and I see two possible solutions: 我面临类似的问题,我看到两种可能的解决方案:

1) Do a pause statement before you want to click/check everything that comes after the ajax request. 1)在您想要单击/检查ajax请求之后的所有内容之前,请执行暂停语句。 Most of the time the test fails because the ajax request is not yet complete and something is missing. 大多数情况下测试失败是因为ajax请求尚未完成且缺少某些内容。

client.pause(5000); should to the trick. 应该诀窍。

2) adaptive.js seems to have a waitForAjax implementation for nightwatchJS. 2) adaptive.js似乎有一个nightwatchJS的waitForAjax实现。 Anyhow it seems to be closed code and I can not tell you anything about it. 无论如何它似乎是封闭的代码,我不能告诉你任何关于它。

3) Use the --verbose option to verify what is happening. 3)使用--verbose选项验证发生了什么。 Verbose can tell you the unhandled error message and this can lead you to a tricky small problem that you can solve. 详细可以告诉您未处理的错误消息,这可能会导致您解决一个棘手的小问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM