量角器似乎不适用于异步页面

时间:2019-08-06 07:46:03

标签: javascript protractor

我有一个HTML页面,该页面以javascript异步方式填充。我用量角器创建了一个集成测试,但是当测试开始时,我无法访问页面DOM的元素

在量角器测试开始时,我需要访问DOM的一个元素以检查其是否正确填充。没门。我无法访问此元素。

var EC = protractor.ExpectedConditions;
condition = EC.presenceOf($('[id=asyncContentId]'));
browser.wait(condition, 5000, "asyncContentId not ready");

期望:我需要DOM元素'asyncContentId'

不幸的是,我永远无法访问此DOM元素。

我使用茉莉花2.1 这是我的最后一个版本,它不起作用:  it(“ Test Ajax applicatif”,function(done){

      console.log("Tests Ajax");
      var EC = protractor.ExpectedConditions;
      element(by.id('btnMenu')).isDisplayed().then(function(displayed) { if (displayed) { browser.wait(EC.elementToBeClickable($('[id=btnMenu]')), 8000); element(by.id('btnMenu')).click(); } });
      browser.wait(EC.elementToBeClickable($('[id=Configuration]')), 8000);
      element(by.id('Ajax')).click().then( function(result) {
          browser.wait(protractor.until.elementLocated(by.id('asyncContentId')), 8000, "Element asyncContentId Non trouvé");
    }, function( error ) {
        console.log("error");
        // eslint-disable-next-line dot-notation
    }).finally(done);

}); });

1 个答案:

答案 0 :(得分:1)

注意:以下是一个异步/等待示例,说明如何等待元素出现。如果您不使用async / await,那么我坚持要这样做,因为selenium-webdriver暂时不赞成使用控制流。

附带说明:ExpectedConditions可能需要Angular同步,我不记得是否需要同步。

在下面的示例中,您可以编写自己的函数来检查元素是否存在。

// Turn off sychronization if we are not on an Angular page.
await browser.waitForAngularEnabled(false);

// Wait for the element to be present. This might throw an error when trying
// to find an element, in that case, return false and keep polling for the element
await browser.wait(async () => {
  try {
    return element(by.css('[id="asyncContentId"]')).isPresent();
  } catch (err) {
    // catch the thrown error when webdriver does not find the element
    // and return false so we can still continue to poll.
    return false;
  }
}, 5000, 'asyncContentId is not present.');