角度$ TIMEOUTの間に要素のテキストを取得する
-
21-12-2019 - |
質問
注:App
の平均 - MOCHA、Express、Angular、およびNodeを使用する私はE2Eテストをする方法を学びます、そして私はテストするために少しアプリを構築しました。1つのことが行動を実行するときはいつでも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();
});
.
しかし、DIVがクリアされたmessageElem.getText().then()
を実行するタイムアウト後に待機しているようです。「実行されたテスト」ログはタイムアウト中に印刷されますが、「getText()」機能はありません。私はこれが角度のライフサイクルと関係があると思いますか?
OHとここにHTML(Jade形式)
div(ng-model="messages",ng-show = "visibility", style = "clear:both;padding-top:3em;border:{{borderType}};text-align:center;")
h3
{{messages}}
.
私のスタイルを無視して、このテストアプリのためのCSSファイルを作成するのは面倒でした:d
他のヒント
以下のコードブロックを使用してこれをハッキングしました。私は$ I間隔ではなく$ TIMEOUTを使用した3RDパーティノードパッケージ(NG-Notifications-Bar)から通知バーを持っていましたが、エラーテキストが特定の値であることを期待する必要があります。私は通知バーのアニメーションを表示するように短いスリープ()を使用し、IDNORESSYNORSINATIONをtrueに切り替えたため、$ TIMEOUTが終了するのを待っていない場合は、PractorをFalseに戻し、PRONTRACTORをFALSEに戻します。通常のAngularJSケイデンス内のテストを続けます。私は睡眠が理想的ではないことを知っていますが、彼らは非常に短いです。
browser.sleep(500);
browser.ignoreSynchronization = true;
expect(page.notification.getText()).toContain('The card was declined.');
browser.sleep(500);
browser.ignoreSynchronization = false;
. 所属していません StackOverflow