【问题标题】:Promise chaining error in MochaMocha 中的 Promise 链接错误
【发布时间】:2018-06-30 20:04:42
【问题描述】:

尝试使用 sendkeys 键入文本或在 Mocha 中使用 typescript promise 链接的弹出窗口中单击按钮时出错。

composeNewMessage(mailCount: number, recepientsName: any, subject: string, messageContent: string,
  recipientLink?: ElementFinder, ccName?: any, bccName?: any,
  ccRecipientLink?: ElementFinder, bccRecipientLink?: ElementFinder): promise.Promise<void> {
  return browser.wait(ExpectedConditions.elementToBeClickable(this.listView.composeMsgBtn), 60000, 'Compose button to be clickable')
      .then(() => this.composeMsgBtn.click())
      .then(() => this.toButton.click())
      .then(() => util.log('To button clicked'))
      .then(() => recipientLink.click())
      .then(() => browser.wait(ExpectedConditions.invisibilityOf(this.loadingBar), 30000, 'Loading bar to be invisible'))
      .then(() => this.searchNameInput.sendKeys(recepientsName))
      .then(() => util.log('Search'))
      .then(() => this.contactList.contacts.get(0).click())
      .then(() => browser.wait(ExpectedConditions.elementToBeClickable(this.contactList.saveButton), 30000, 'Save button to be clickable'))
      .then(() => this.contactList.saveButton.click());
}

describe('Logging in', () => {
it('should', (done: MochaDone) => {
      login
        .login(user, name, USER_PASSWORD)
        .then(() => util.log('Logged in.'))
        .then(() => nav.navigate('sat'))
        .then(() => util.log('Navigated to sat.'))
        .then(() => browser.waitForAngularEnabled(true))
        .then(() => composeView.composeNewMessage(3, [predefinedStudent1, predefinedStudent2], 'Test sub', MSG_CONTENT, contactList.studentLinkInRecipients))
        .then(() => composeView.listView.composeMsgBtn.click())
        .then(() => done())
        .catch((err) => done(err));
  });
});

“To button clicked”正在控制台窗口中打印。但之后得到如下图所示的错误。 Mocha error in promise chaining

【问题讨论】:

  • 你有没有试过在最后一个 then 子句之后使用 .finally 方法?
  • 您需要将done作为参数传入,并确保在异步操作完成后调用它。如果您不确定如何执行此操作,请将您的完整代码准确地粘贴到完整的测试用例中,以便我们为您提供帮助。
  • 更新了完整代码。这是从规范中调用的页面对象方法。
  • 请避免发布错误消息/代码的屏幕截图
  • 这不可能是所有的代码。我在这里没有看到测试用例。如果您需要帮助,请真正发布您的所有代码。理想情况下,用尽可能少的代码重现它,然后发布所有这些。您很有可能会自己解决它,同时像这样缩小范围。一种策略是注释掉代码,直到您停止收到错误为止。如果您仍然在几乎没有任何代码的情况下遇到错误,那太好了!这将使我们更容易帮助您解决问题。

标签: node.js typescript promise protractor mocha.js


【解决方案1】:

增加测试超时时间

您似乎希望等待的时间超过设置的超时时间(120000 毫秒),因此您应该将超时时间设置为更长的时间。

阅读如何做到这一点here: Mocha Timeouts

分析

根据图像,您的超时设置为 120000 毫秒(2 秒)

现在,我不知道 browser.wait 是如何工作的。但根据代码,您似乎希望等待超过 120000 毫秒:

  1. 在调用 composeNewMessage 之前,等待 angular browser.waitForAngularEnabled
  2. 然后是第一个browser.wait 60000ms
  3. browser.wait 在记录“点击按钮”30000ms 后再次
  4. browser.wait 再次 30000ms

现在您没有记录“搜索”的原因可能是因为在调用 composeNewMessage 之前等待了很长时间。

【讨论】:

  • 感谢您的链接:摩卡超时。它解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 2015-02-17
  • 1970-01-01
  • 2015-05-07
  • 1970-01-01
  • 2019-06-03
  • 1970-01-01
  • 2015-05-02
  • 2015-06-18
相关资源
最近更新 更多