【问题标题】:Switch case with cucumber steps and typescript带有黄瓜步骤和打字稿的开关盒
【发布时间】:2020-10-03 17:48:15
【问题描述】:

我使用 WebdriverIO6、Typescript 4 和 cucumber。
我有这种情况:

Scenario: Login without password
And I enter valid email
But I do not enter password
Then I can see "Password is required" error message  

我有这一步:

Then(/^I can see (.*) error message$/, (type: string) => {
 const headerErrorMessage = loginPage.headerErrorMessage;
 const itemErrorMessage = loginPage.itemErrorMessage;

 switch (type) {
  case 'Authentication': {
   expect(headerErrorMessage.getText()).to.be.equal(errorMessages.headerErrorMessage);
   expect(itemErrorMessage.getText()).to.be.equal(errorMessages.authenticationFailedErrorMessage);
   break;
  }
  case 'Invalid email address': {
   expect(headerErrorMessage.getText()).to.be.equal(errorMessages.headerErrorMessage);
   expect(itemErrorMessage.getText()).to.be.equal(errorMessages.invalidEmailErrorMessage);
   break;
  }
  case 'Password is required': {
   expect(headerErrorMessage.getText()).to.be.equal(errorMessages.headerErrorMessage);
   expect(itemErrorMessage.getText()).to.be.equal(errorMessages.passwordRequiredErrorMessage);
   break;
  }
  default: {
  throw new TypeError('Unsupported type of error message');
  }
}
});  

而且开关不工作。对于那种情况,它与case 'Password is required' 不匹配。它只是默认说它是Unsupported type of error message
有人知道为什么吗?
谢谢!
编辑
当我把这个Then(/^I can see "([^"]*)" error message$/, (type: string) => {(注意我有"([^"]*)"而不是(.*)),然后它工作。
有好心人能解释一下吗?
有什么不同的解释?
谢谢!

【问题讨论】:

  • "([^"]*)" 这意味着您正在抓取双引号之间的文本,在这种情况下。特征文件中的文本正在匹配。
  • 是的,我明白正则表达式的作用。但我不明白为什么 switch 在 case (.*) 中不起作用,因为它也会获取引号之间的文本...

标签: typescript cucumber webdriver-io


【解决方案1】:

括号表示 Cucumber 捕获模式:(模式)。
.* 的正则表达式匹配任何字符(换行符除外)0 或更多 次。

你能在运行时检查'type'参数的值吗?您可能正在捕获“需要密码”with 来自 Gherkin 行的引号,这与您的 switch case 不匹配。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-20
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    相关资源
    最近更新 更多