【问题标题】:Cucumberjs regexCucumberjs 正则表达式
【发布时间】:2018-11-07 23:27:40
【问题描述】:

所以我在 cucumberjs 中有这个功能

Scenario: Upload a valid pcf file into gpe
            Given that the user uploads a valid pcf file
            Then the user should see an upload success indicator

     Scenario: Upload an invalid pcf file
            Given that the user uploads an invalid pcf file
            Then the user should see an upload error message

如您所见,除了上传后的字符串外,then 几乎相同。所以我写了一个 my then 这样的:

this.Then(/^that the user uploads [a-zA-Z]+/, ( option ) => {
   console.log( option );
} );

但选项显示功能:完成。上传单词后的字符串如何获取?

【问题讨论】:

  • 这些绝对应该是两个独立的步骤。至于正则表达式,只需添加一个空格: [\w ]+

标签: regex cucumberjs


【解决方案1】:

您不再需要 RegEx!呜!见下文。

Scenario: Upload an "valid pcf file into gpe"
        Given that the user uploads a valid pcf file
        Then the user should see an upload success indicator

Scenario: Upload an "invalid pcf file"
            Given that the user uploads an invalid pcf file
            Then the user should see an upload error message

在你的场景中使用它,当你用 "" 包裹文本时,它现在是一个准备好传递给你的 JS 的字符串

this.Then(Upload an {string}, ( stringSwitcher ,option ) => {

这就是启动 JS 的行的工作方式,您可以将 stringSwitcher 命名为任何您想要的名称,但这就是存储场景中独特部分的内容,本质上是在场景中上传“blablabla”,它将能够使用用于通过它的javascript。

我希望这是有道理的,但我已经走上了使用正则表达式的路线,而你真的不再需要了。

【讨论】:

    【解决方案2】:

    为什么不试试这样的:

    this.Then(/^that the user uploads an? (valid|invalid) (\w+) file/, (validity, filetype) => {
       if (validity == "valid"){
          console.log("this " + filetype + " is valid");
       } else {
          console.log("this " + filetype + " is invalid");
       }
       return true;
    } );
    

    细分:

    • an? - 捕获 aan? 使 n 可选 - 出于语法目的)
    • (valid|invalid) - 捕获无效和有效的词
    • (\w+) - 捕获任何字符 a-z, A-Z, 0-9

    这意味着您可以根据文件的有效性和 if 语句中的文件类型来执行操作。

    另外,使用 switch 语句也可以。

    this.Then(/^that the user uploads an? (valid|invalid) (\w+) file/, (validity, filetype) => {
        switch(filetype){
            case "pcf":
                if (validity == "valid"){
                    // Do the stuff for valid
                } else {
                    // Do the stuff for invalid
                }
                break;
            default:
                throw new Error("Filetype: '" + filetype + "' is not recognised");
        }
        return true;
    });
    

    【讨论】:

      猜你喜欢
      • 2015-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-21
      相关资源
      最近更新 更多