javascript的递归调用中的问题

时间:2016-05-31 12:55:07

标签: javascript node.js jasmine protractor

说明:我想从网页上读取一个特定的标签,其值变为“已开始”,“进行中”,“成功”,“错误”之一。一旦标签值变为“成功”或“错误”,就不会有任何进一步的变化。

问题:当我在量角器中使用javascript读取标签值时,标签的文本值不会返回给调用函数;相反,它返回'undefined'。下面是我的代码,请看一下,让我问一下这个问题。

CheckColor_Test.js

var commonFunctions = require('../pages/CommonFunctions.js');
describe("Run Test", function () {
    it("should stop once the status reached Success or Error", function () {
        var processStatus = commonFunctions.refreshTillProcessFinish();
        expect(processStatus).toContain('Success','Error');
    });
});

CommonFunctions.js

Var CommonFunctions = function(){
var label = element(by.id('Status'));
var refreshStatusBtn = element(by.css('[ng-click="getJob()"]'));
    this.getStatusValue = function () {
        return label.then(function (headers) {
            return headers.getText();
        });
    };
    this.refreshTillRefreshFinish = function () {
        var refreshStatusMonitor = function (currentStatus) {
            return currentStatus.then(function (Status) {
                if (Status == 'Success' || Status.includes("Error")) {
                    console.log(Status);
                    return Status;
                } else {
                    refreshStatusBtn.click();
                    console.log(Status);
                    browser.sleep(2000);
                    refreshStatusMonitor (currentStatus);
                }
            });
        };
        return refreshStatusMonitor (this.getStatusValue);
    };
}
module.exports = new CommonFunctions();

在量角器中执行: 我在Webstorm中配置了量角器,因此我曾经使用过它。

预期结果 测试应该成功并通过

实际结果 测试失败并出现以下错误。

"C:\Program Files (x86)\JetBrains\WebStorm 2016.1.1\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" node_modules\protractor\built\cli.js D:\Somesh_HDD\WebstormProjects\ProjectUBET\conf.js
[22:19:59] I/direct - Using ChromeDriver directly...
[22:19:59] I/launcher - Running 1 instances of WebDriver
Spec started
Started
InProgress
Success

  Run Test
    ? should stop once the status reached Success or Error
      - Expected undefined to contain 'Success', 'Error'.

**************************************************
*                    Failures                    *
**************************************************

1) Run Test should stop once the status reached Success or Error
  - Expected undefined to contain 'Success', 'Error'.

Executed 1 of 1 spec (1 FAILED) in 33 secs.
[22:20:36] I/launcher - 0 instance(s) of WebDriver still running
[22:20:36] I/launcher - chrome #01 failed 1 test(s)
[22:20:36] I/launcher - overall: 1 failed spec(s)
[22:20:36] E/launcher - Process exited with error code 1

Process finished with exit code 1

2 个答案:

答案 0 :(得分:1)

以下返回值:

return currentStatus.then(...);

不是此声明返回的值:

return Status;

实际上,后者返回到refreshStatusMonitor的一个递归调用,它不会被捕获到任何地方。

因为这是涉及promises的异步代码,currentStatus的返回值也应该是一个promise,它会通过refreshStatusMonitorrefreshTillRefreshFinish冒充到你的测试中,然后需要适应等待承诺在做任何事情之前完成。

我还建议不要使用browser.sleep(...),因为它会完全阻止您的JavaScript环境。您可以改为使用setTimeout(...)

以下是一些基于这些想法的未经测试的代码:

this.refreshTillRefreshFinish = function () {
    // create a promise
    var deferred = protractor.promise.defer();
    var refreshStatusMonitor = function (currentStatus) {
        currentStatus.then(function refresh(Status) {
            if (Status == 'Success' || Status.includes("Error")) {
                // Signal the completion via the promise. 
                // This triggers the `then` callback in your revised test
                deferred.fulfill(Status); 
            } else {
                refreshStatusBtn.click();
                console.log(Status);
                // Use setTimeout so JavaScript is not blocked here:
                setTimeout(function () {
                    refreshStatusMonitor(currentStatus);
                }, 2000);
            }
        });
    };
    refreshStatusMonitor(this.getStatusValue);
    // Don't wait for the result to happen while blocking everything, 
    // instead return a custom-made promise immediately 
    return deferred.promise;
};

然后,您的测试还应考虑到您正在处理承诺:

it("should stop once the status reached Success or Error", function () {
    var processStatus = commonFunctions.refreshTillProcessFinish().then(function () {
        expect(processStatus).toContain('Success','Error');
        done();
    });
}, 20000); // set timeout to 20 seconds

请注意,Jasmine的默认超时时间为2秒,因此您需要在结尾处提供额外的参数。

注意:这种异步测试不适合运行批量的单元测试。

答案 1 :(得分:0)

您的脚本是否能够递归点击刷新按钮?

我通过在递归方法中引入promises对您现有的脚本做了一些更改。只是试一试。

var CommonFunctions = function(){
    var label = element(by.id('Status'));
    var refreshStatusBtn = element(by.css('[ng-click="getJob()"]'));
    this.refreshTillRefreshFinish = function () {
        var defer = protractor.promise().defer();
        var refreshStatusMonitor = function () {
             label.getText().then(function (Status) {
                if (Status == 'Success' || Status.includes("Error")) {
                    defer.fulfill(Status);
                } else {
                    refreshStatusBtn.click();
                    browser.sleep(2000);
                    refreshStatusMonitor ();
                }
            });

            return defer.promise;
        };
        return refreshStatusMonitor ();
    };
}
module.exports = new CommonFunctions();