简体   繁体   English

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

[英]Protractor seems to doesn't work with asynchronous page

I have a HTML page which is fill in a asynchronous way in javascript. 我有一个HTML页面,该页面以javascript异步方式填充。 I create an integration test with protractor but when the test begin, I can't have access to element of the DOM of the page 我用量角器创建了一个集成测试,但是当测试开始时,我无法访问页面DOM的元素

At the beginning of the protractor test i need to have access of one element of the DOM to check if it is filled correctly. 在量角器测试开始时,我需要访问DOM的一个元素以检查其是否正确填充。 No Way. 没门。 I cannot have access to this element. 我无法访问此元素。

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

Expect : I Need the DOM element 'asyncContentId' 预期:我需要DOM元素'asyncContentId'

Unfortunatelly I never have access to this DOM element. 不幸的是,我从未访问过此DOM元素。

I Use Jasmine 2.1 This is my last version ans it doesn't work : it("Test Ajax applicatif", function(done) { 我使用的是Jasmine 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);

}); }); }); });

Note: The following is an async / await example of how you could wait for an element to be present. 注意:以下是一个异步/等待示例,说明如何等待元素出现。 If you are not using async / await, I insist that you do since the control flow has been deprecated by selenium-webdriver for a while. 如果您不使用异步/等待,那么我坚持要您这样做,因为selenium-webdriver暂时不赞成使用控制流。

Side note: ExpectedConditions might require Angular synchronization and I can't remember if it did or not. 旁注: ExpectedConditions可能需要Angular同步,我不记得是否需要同步。

In the following example, you could write your own function to check if the element exists. 在下面的示例中,您可以编写自己的函数来检查元素是否存在。

// 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.');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM