首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cucumberjs regex

Cucumberjs regex
EN

Stack Overflow用户
提问于 2017-05-02 22:30:20
回答 2查看 1.7K关注 0票数 1

所以我在cucumberjs中有这个特性

代码语言:javascript
复制
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是这样的:

代码语言:javascript
复制
this.Then(/^that the user uploads [a-zA-Z]+/, ( option ) => {
   console.log( option );
} );

但选项显示function: finish。如何获取上传单词后的字符串?

EN

回答 2

Stack Overflow用户

发布于 2018-11-07 23:27:40

你不再需要RegEx了!哇!见下文。

代码语言:javascript
复制
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中的字符串

代码语言:javascript
复制
this.Then(Upload an {string}, ( stringSwitcher ,option ) => {

这就是启动JS的代码行的工作方式,您可以将stringSwitcher命名为任何您想要的名称,但这存储了场景中您独特的部分,本质上是在场景中上传一个"blablabla“,它将能够使用javascript来传递它。

我希望这是有意义的,但我已经走上了使用regEx的道路,你真的不再需要了。

票数 3
EN

Stack Overflow用户

发布于 2017-05-05 15:47:08

为什么不试着这样做呢:

代码语言:javascript
复制
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 (对于语法上的
    • an??使n成为可选项-捕获单词无效,而valid
    • (\w+) -捕获任何字符a-zA-Z0-9

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

或者,使用switch语句也可以。

代码语言:javascript
复制
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;
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43740503

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档