1

我使用 afterEach 注销,但我的测试尝试在 afterEach 完成注销之前登录。

我试图避免使用睡眠语句(真的减慢了速度)。

如何让测试等待前一个 beforeEach 完成?

代码片段:

afterEach(function() {
    AccountBlock.logout().then(function(){
        browser.sleep(2000); // I want to get rid of this
    });
});

logout() 方法(来自 AccountBlock 对象):

this.logout = function() {
    var logoutLink = this.logoutLink;
    var userName = this.userName;
    return logoutLink.isDisplayed().then(function(isDisplayed){
        if (!isDisplayed) {
            return userName.click().then (function() {
                return logoutLink.click();
            }); // open the account menu before logout
        } else { // account menu already displayed
            return logoutLink.click();
        }
    });
}
this.userName = element(by.css('a[ng-bind="userName"]'));
this.logoutLink = element(by.css('a[ng-click^="logout"]'));
4

2 回答 2

1

browser.wait 绝对是要走的路,但不是直接调用元素来询问它们是否存在或显示量角器具有内置功能,称为ExpectedConditions。您可以使用这个让量角器强制浏览器等待,直到它满足它自己的预期条件。

下面我正在查找元素,使用预期条件来检测元素的存在,然后使用 not 运算符来检测元素何时不再存在。这应该强制 browser.wait 暂停未来的承诺,直到该条件为真。

afterEach(function(){
    var ec = protractor.ExpectedConditions;
    var logout = element(by.css('a[ng-click^="logout"]'));
    var logoutLink = ec.presenceOf(logout);
    var logoutLinkNotThereAnymore = ec.not(logoutLink);

    //do logout
    browser.wait(logoutLinkNotThereAnymore, 10000, "Waited 10 seconds");
});

beforeEach(function(){
    //do login
});
于 2016-11-30T19:41:33.197 回答
0

我使用 browser.wait() 来避免 sleep(),在你的登录代码块中你可以这样做

   var doLogin = function(){
           browser.wait(element(by.id('yourLoginBoxId')).isDisplayed); //this will wait until the html with the form to login is in your DOM
          //your code to login here
      };

请注意 isDisplayed 没有 ()。

于 2015-01-13T15:14:24.270 回答