注意:使用MEAN - 应用程序的Mocha,Express,Angular和Node
我正在学习如何进行e2e测试,我构建了一个小应用程序来测试。它做的一件事是每当你执行一个动作时,它会显示一条消息X秒然后消失。我正在使用angulars $ timeout。
$scope.displayMessage = function(messageIn, borderColor){
var timeoutTime = 5000; //clear the message after x seconds
$scope.borderType = "1px solid " + borderColor;
$scope.messages = messageIn;
$scope.visibility = true; //reveal the div
$timeout.cancel($scope.timeout); //cancel any previous timers
$scope.timeout = $timeout(function(){ //Set and start new timer.
$scope.visibility = false;
$scope.messages = "";
}, timeoutTime);
};
现在我正在尝试测试这个。目前我有
it("should display message", function(done){
var button = element(by.id("getStudents"));
console.log("clicking button");
var messageElem = element(by.binding("messages"));
button.click().then(function () {
console.log("button clicked");
messageElem.getText().then(function(text){
console.log("The text should equal: " + text);
});
//expect(messageElem.getText()).toBe("Students retrieved");
console.log("test executed");
});
done();
});
但似乎要等到超时后才能执行messageElem.getText().then()
,此时div已被清除。 “测试执行”日志将在超时期间打印,但不会打印“getText()”函数。我猜这与角度生命周期有关?
哦,这是HTML(翡翠格式)
div(ng-model="messages",ng-show = "visibility", style = "clear:both;padding-top:3em;border:{{borderType}};text-align:center;")
h3
{{messages}}
忽略我的风格,我懒得为这个测试应用制作一个css文件:D
答案 0 :(得分:4)
答案 1 :(得分:1)
我使用下面的代码块来解决这个问题。我有来自第三方节点包(ng-notifications-bar)的通知栏,它使用$ timeout而不是$ interval,但需要预期错误文本是某个值。我使用了一个简短的sleep()来显示通知栏动画,将ignoreSynchronization切换为true,因此Protractor不会等待$ timeout结束,设置我的expect(),并将ignoreSynchronization切换回false,这样量角器可以在常规的AngularJS节奏中继续测试。我知道睡眠并不理想,但它们很短暂。
browser.sleep(500);
browser.ignoreSynchronization = true;
expect(page.notification.getText()).toContain('The card was declined.');
browser.sleep(500);
browser.ignoreSynchronization = false;