如何解决量角器中的承诺

Posted

技术标签:

【中文标题】如何解决量角器中的承诺【英文标题】:How to resolve the promise in protractor 【发布时间】:2021-12-14 05:06:05 【问题描述】:

我在量角器上创建了一个简单的脚本,但对我来说,该脚本运行不正确。请找到代码和输出

 it('Test#5.5 - Select top 5 Email Dumps', async () => 
    let testId = 5.5;
    let timeKey = 'Select top 5 Email Dumps';
    console.time(timeKey);
    log.info('\n', 'Selecting top 5 Email Dumps one after one')
    // let email_dumps_list = await element.all(by.xpath('//*[contains(@data-selenium,"email_dump_item")]')).count();
    // log.info('list of email dumps are ' + email_dumps_list);
    for (let i = 0; i < 5; i++) 
      let email_dump_item = await element.all(by.xpath('//*[contains(@data-selenium,"email_dump_item")]/div/span')).get(i);
      selectElementAndCheckText(email_dump_item, testId + '_' + i)

);

 async function selectElementAndCheckText(obj, testId) 
    // const email_dump_0 = await element.all(by.xpath('//*[contains(@data-selenium,"email_dump_item")]/div/span')).get(element);
    let email_dump_name = await obj.getText()
    log.info('Clicking on ' + email_dump_name + ' email dump')
    obj.click().then(async function()
    const email_header_name = element(by.css('div[data-testid="data-dump-page"] > * .MuiToolbar-gutters'));
    if (browser.wait(EC.visibilityOf(email_header_name), 5000)) 
      checkText(email_dump_name, await email_header_name.getText(), 'Email details shown doesn\'t match with the selected Dump', testId)
      log.info("Pass")
      return true;
    
    else 
      log.error('Email dump details not loaded')
      return false;
    
  );
  

 async function checkText(actualText, expectedText, errorMessage, testId) 
    try 
      assert.equal(actualText, expectedText, errorMessage, '\n')
     catch (error) 
      console.log(error);
    
  

输出

Selecting top 5 Email Dumps one after one
[1028/234735.013:INFO:CONSOLE(1)] "true SORT", source: https://webapp-dev-raven.ocp-nonprod.com/static/js/main.88ddd1a0.chunk.js (1)

Clicking on Selenium email dump email dump
Clicking on 10k email upload test email dump
Clicking on 191 emails test email dump
Clicking on another 100 emails email dump
Clicking on simulated 100 emails email dump

AssertionError [ERR_ASSERTION] [ERR_ASSERTION]: Email details shown doesn't match with the selected Dump
    at checkText (C:\Users\webapp\test\e2e\email-dumps-test.js:270:14)
    at C:\Users\webapp\test\e2e\email-dumps-test.js:114:7
    at processTicksAndRejections (internal/process/task_queues.js:95:5) 
  generatedMessage: false,
  code: 'ERR_ASSERTION',
  actual: 'Selenium email dump',
  expected: 'simulated 100 emails',
  operator: '=='

Pass

AssertionError [ERR_ASSERTION] [ERR_ASSERTION]: Email details shown doesn't match with the selected Dump
    at checkText (C:\Users\webapp\test\e2e\email-dumps-test.js:270:14)
    at C:\Users\webapp\test\e2e\email-dumps-test.js:114:7
    at processTicksAndRejections (internal/process/task_queues.js:95:5) 
  generatedMessage: false,
  code: 'ERR_ASSERTION',
  actual: '10k email upload test',
  expected: 'simulated 100 emails',
  operator: '=='

Pass

AssertionError [ERR_ASSERTION] [ERR_ASSERTION]: Email details shown doesn't match with the selected Dump
    at checkText (C:\Users\webapp\test\e2e\email-dumps-test.js:270:14)
    at C:\Users\webapp\test\e2e\email-dumps-test.js:114:7
    at processTicksAndRejections (internal/process/task_queues.js:95:5) 
  generatedMessage: false,
  code: 'ERR_ASSERTION',
  actual: '191 emails test',
  expected: 'simulated 100 emails',
  operator: '=='

Pass

如果您注意到输出,则 for 循环在进入 obj.click().then 函数之前首先执行,因此它会打印日志,然后进行实际测试。如果我在这里做错了什么,请告诉我。

【问题讨论】:

【参考方案1】:

因为你的函数selectElementAndCheckText()selectElementAndCheckText ()是异步的,所以你需要在调用它们的时候加上前缀await

await 应添加到以下位置:

for (let i = 0; i < 5; i++) 
   let email_dump_item = await element.all(by.xpath('//*[contains(@data-selenium,"email_dump_item")]/div/span')).get(i);
   await selectElementAndCheckText(email_dump_item, testId + '_' + i)

....

    if (browser.wait(EC.visibilityOf(email_header_name), 5000)) 
      await checkText(email_dump_name, await email_header_name.getText(), 'Email details shown doesn\'t match with the selected Dump', testId)

【讨论】:

感谢您的回复。添加等待后它起作用了。同样,当 browser.wait 由于元素不可见而返回 false 时,脚本不会进入 else 条件。并跳过测试用例。在控制台中,我看到的只是“FWaiting for page to load”。如何确保元素是否可见,以便我可以继续进行 您的 if/else 不起作用,因为这不是使用 ExpectedConditon 的正确方法。它不返回布尔值,而是返回一个承诺。这个承诺决定了条件是否真的成立。它只是等到条件变为真或达到超时。您应该改用element.isVisible()element.isDisplayed()

以上是关于如何解决量角器中的承诺的主要内容,如果未能解决你的问题,请参考以下文章

量角器 - 如何将一组承诺的结果放入另一个数组

量角器 - 处理“then”承诺使它们更具可读性

如何在 node.js 中的特定持续时间后强制解决承诺? [复制]

量角器中的“defaultTimeoutInterval”何时重置?

如何解决反应身份验证中的未捕获(承诺中)TypeError?

如何在 node.js 中解决可变数量的承诺