AngularJS量角器 - 如何测试AJAX登录呼叫? [英] AngularJS Protractor - How Do You Test an AJAX Login Call?
问题描述
我有一个按钮,一旦点击就会发出Angular的 $ promise
格式的AJAX调用。登录成功后,会更改 $ scope
变量,并显示如下所示的元素:
I have a button that once clicked sends out an AJAX call in Angular's $promise
format. When the login is successful, a $scope
variable is changed and an element that looks like:
<section id="logged-in-section" ng-if="auth.user !== null">
// Section to display if logged in
</section>
。我目前正在使用以下内容测试上述内容:
is displayed. I am currently testing the above with the following:
loginButton.click();
browser.sleep(2000);
expect($("#logged-in-section").isDisplayed()).toBeTruthy();
browser.sleep(2000)
空闲在Proractor检查是否显示登录区
之前,浏览器持续两秒钟。如果我拿出 browser.sleep(2000)
,则测试失败,因为在点击登录按钮和从服务器返回的响应之间存在延迟。
browser.sleep(2000)
idles the browser for two seconds before Protractor checks to see if logged-in-section
has been displayed. If I take out browser.sleep(2000)
, the test fails since there is a lag between hitting the login button and the response returned from the server.
将登录按钮链接到 expect
语句的语法是什么,以便Protractor只检查<$ c返回 $ promise
后,$ c>#logged-in-section ?
What's the syntax to chain the login button to the expect
statement so that Protractor is only checking for #logged-in-section
after the $promise
is returned?
推荐答案
你可以这样做:
loginButton.click().then(function(){
browser.waitForAngular().then(function (){ //this should wait for the angular digest process to finish
expect($("#logged-in-section").isDisplayed()).toBeTruthy();
})
});
或者这应该适用于:
loginButton.click().then(function(){
browser.wait(element(by.id("logged-in-section")).isDisplayed);
expect($("#logged-in-section").isDisplayed()).toBeTruthy();
});
这篇关于AngularJS量角器 - 如何测试AJAX登录呼叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!