【问题标题】:How to pass data from the feature file to step definition file using regular expressions?如何使用正则表达式将数据从特征文件传递到步骤定义文件?
【发布时间】:2020-06-08 19:58:42
【问题描述】:

这个项目正在使用 NodeJS、Cucumber、Gherkin、Selenium。

我正在尝试传递一个存储的值,或者暂时传递一个值,在这个例子中,它是一个通过使用正则表达式从功能文件到步骤定义文件的 URL。

我想使用的功能示例( 是我在其他示例中看到的我的猜测,但似乎不起作用)

Scenario: 0 | A User Logging In
Given I open the page with the <url> 

还有我的步骤定义文件

Given("/^I open the page with the url? (.*)$/"), function(next){

    driver.get("http://localhost:3000/login");
    pageElement = driver.findElement(By.id("email"));
    pageElement.sendKeys("example@email.co.uk");
    next(); 
};

我相信我的步骤定义文件是正确的,但我并不肯定。

如果您需要有关我的项目的更多信息,请告诉我,我全天都在使用 stack-overflow 并致力于让这项工作快速运行。

提前感谢您的支持, 杰克

【问题讨论】:

    标签: javascript node.js selenium cucumber


    【解决方案1】:

    我会尝试将您的 JavaScript 中的正则表达式更改为一个字符串,该字符串需要您传递给 Given 语句的变量:

    Given('I open the page with the url {string}'), function(next) {
      //your code
    }
    

    您可以在 Gherkin Given 语句中定义变量,方法是按照它们通常呈现的方式声明它们。例如,如果你想传入一个string:

    Scenario: 0 | A User Logging In
    Given I open the page with the url "http://localhost"
    

    将变量url 传递到您的javascript 文件并包含字符串http://localhost

    整数也是如此:

    Scenario: 0 | A User Logging In
    Given I open the page with the url "http://localhost" and port 3000
    

    您的 javascript 函数现在将接收 2 个变量,url 和 port。将它们记录到控制台,您将看到

    http://localhost
    3000
    

    所以我会重新安排您的代码,如下所示:

    小黄瓜:

    Scenario: 0 | A User Logging In
    Given I open the page with the url "http://localhost:3000/login"
    

    JavaScript:

    Given('I open the page with the url {string}', (url, next) => {
         console.log(url) // log to check the variable is being passed into the function
    
         driver.get(url);
    
         pageElement = driver.findElement(By.id("email"));
         pageElement.sendKeys("example@email.co.uk");
    
         next(); 
    });
    

    编辑: 你可以找到关于这个特定问题的所有文档here。

    【讨论】:

    • 这是有效的,感谢您的帮助,非常感谢@blueprintChris
    【解决方案2】:

    下面的简短回答 - 可能对即将到来的访客有用!

    通用正则表达式模式适用于黄瓜中的所有类型的数据是- ([^"]*) 这就是完整步骤定义的方式-

    @Given("^我用 ([^"]*)$") 打开页面 //见下面的注释 * public void yourMethodName(yourdatatType yourDataVar) {

    使用 (yourDataVar) 实现您的方法 ...

    }

    // * 注意 - '^' 和 '$' 符号将分别由黄瓜自动添加到任何步骤定义的开头和结尾。

    【讨论】:

      猜你喜欢
      • 2020-01-13
      • 1970-01-01
      • 2021-02-27
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      • 2020-04-03
      相关资源
      最近更新 更多