说明:我想从网页中读取特定的标签,其值更改为“开始”,“进行中”,“成功”,“错误”之一。标签值一旦更改为“成功”或“错误”,就不会再进行任何更改。
问题:当我在量角器中使用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
最佳答案
返回值如下:
return currentStatus.then(...);
不是此语句返回的值:
return Status;
实际上,后者被返回到
refreshStatusMonitor
的递归调用之一,该递归调用未在任何地方捕获。因为这是涉及承诺的异步代码,所以
currentStatus
的返回值也应该是一个承诺,它将通过refreshStatusMonitor
,refreshTillRefreshFinish
冒泡到您的测试中,然后还需要对其进行调整以等待承诺在期望什么之前实现。我也建议不要使用
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秒,因此您需要在末尾提供该额外的参数。
注意:这种异步测试不太适合运行批量单元测试。
关于javascript - javascript递归调用中的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37546381/