【问题标题】:afterEach not waiting to complete before continuingafterEach 在继续之前不等待完成
【发布时间】:2014-12-23 16:21:01
【问题描述】:

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

我试图避免使用 sleep 语句(真的会减慢速度)。

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

代码 sn-ps:

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"]'));

【问题讨论】:

  • 请包含一些代码以便于回答。
  • 作为代码 sn-ps 添加到原始帖子中
  • Protractor 推荐handling log in with an onPrepare,因此您只需登录您的应用程序一次,然后再运行所有测试。您真的需要在每次测试后退出吗?
  • 如果他需要在每个测试用例@user29之后退出...我们应该尝试回答手头的问题。
  • @HenrikBechmann 因为您没有为userName.click(); 添加return,所以不要期望及时解决该承诺。如果你解决了这个问题并且仍然看到问题,我想我有一个解决方案。

标签: angularjs testing jasmine protractor e2e-testing


【解决方案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
});

【讨论】:

    【解决方案2】:

    我使用 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 没有 ()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      • 2020-02-24
      • 1970-01-01
      • 2019-03-12
      • 2021-12-26
      相关资源
      最近更新 更多