找不到间歇性"元素"在量角器

时间:2015-06-23 00:31:31

标签: angularjs protractor meanjs

在量角器中,我正在尝试编写一个函数来模拟单击响应式导航菜单项(即菜单是顶部的下拉栏或移动时的汉堡包链接)。条形结构由MEANJS定义,其中有一些id =""属性混合在一起。

当我一次运行一个规范时,它似乎完全符合我的要求,就像这样:

protractor app/tests/e2e/conf.js --suite mySuite

但是当我使用完整测试(其中只有4个测试)时,就像这样:

protractor app/tests/e2e/conf.js

我开始间歇地得到这个错误(错误来源位于以下2个地方):

Failed: element not visible

这是我的功能

commonPOs.clickNavBar = function(mainTab, linkUrl) {
  var deferred = protractor.promise.defer();

  var hamburger = element(by.id('nav-hamburger'));
  var linkCssExpression = 'a[href*="' + linkUrl + '"]';

  hamburger.isDisplayed().then(function(result) {
    if ( result ) {
      hamburger.click().then(function() {
        var navBar = hamburger
          .element(by.xpath('../..'))
          .element(by.id('myapp-navbar'));
          return clickItNow(mainTab, linkUrl, navBar);
      });
    } else {
      return clickItNow(mainTab, linkUrl, element(by.id('myapp-navbar')));
    }
  });

  return deferred.promise;

  function clickItNow(mainTab, linkUrl, navBar) {
    var link;
    if(mainTab) {
      // if mainTab was  passed, need to
      // click the parent first to expose the link
      var parentLink;

      if (mainTab == 'ACCTADMIN') {
        parentLink = navBar.element(by.id('account-admin-menu'));
      }
      else {
        parentLink = navBar.element(by.linkText(mainTab));
      }
      expect(parentLink.isPresent()).toBeTruthy();
      parentLink.click();   // FIRST PLACE ERROR HAPPENS
      link = parentLink.element(by.xpath('..')).element(by.css(linkCssExpression));
    } 
    else {
      link = navBar.element(by.css(linkCssExpression));
    }

    expect(link.isPresent()).toBeTruthy();
    link.click();     // SECOND PLACE ERROR HAPPENS
    return deferred.fulfill();
  }
};

我试图使用这两种方法,但都不起作用:

browser.sleep(500);
browser.driver.wait(protractor.until.elementIsVisible(parentLink));

我做了什么NOOB异步错误?

1 个答案:

答案 0 :(得分:2)

自我回答......但任何更好的答案都会赢得绿色复选标记!!!

已编辑 - 遇到更多问题后,找到了一个不错的'waitReady()' contribution from elgalu

我的原始代码有一些明显的问题,但修复它们并没有解决问题。经过我的努力,解决方案似乎是我添加的 expect(navBar.waitReady())。toBeTruthy(); 行。我还必须拆分嵌套函数。无论如何,这是现在似乎正在运行的新代码。更好(全面)的答案将获得绿色选中标记!我很确定这里有一个缺陷或2个。

commonPOs.clickNavBar = function(mainTab, linkUrl) {
  var deferred = protractor.promise.defer();

  var hamburger = element(by.id('nav-hamburger'));

  hamburger.isDisplayed().then(function(result) {
    if ( result ) {
      hamburger.click().then(function() {
        var navBar = hamburger
          .element(by.xpath('../..'))
          .element(by.id('myapp-navbar'));
          return clickItNow(mainTab, linkUrl, navBar, deferred);
      });
    } else {
      return clickItNow(mainTab, linkUrl, element(by.id('myapp-navbar')), deferred);
    }
  });

  return deferred.promise;
};

function clickItNow(mainTab, linkUrl, navBar, deferred) {
  var targetLink;
  var linkCssExpression = 'a[href*="' + linkUrl + '"]';

  expect(navBar.waitReady()).toBeTruthy();

  if(mainTab) {
    // if mainTab was  passed, neet to
    // click the parent first to expose the link
    var parentTabLink;

    if (mainTab == 'ACCTADMIN') {
      parentTabLink = navBar.element(by.id('account-admin-menu'));
    }
    else {
      parentTabLink = navBar.element(by.id('main-menu-' + mainTab));
    }
    // expect(parentTabLink.isDisplayed()).toBeTruthy();
    expect(parentTabLink.waitReady()).toBeTruthy();
    parentTabLink.click();
    targetLink = parentTabLink.element(by.xpath('..')).element(by.css(linkCssExpression));
  }
  else {
    targetLink = navBar.element(by.css(linkCssExpression));
  }

  expect(targetLink.isDisplayed()).toBeTruthy();
  targetLink.click().then(function() {
    return deferred.fulfill();
  });
}