如何使用正则表达式将数据从特征文件传递到步骤定义文件?

时间:2018-09-20 09:18:50

标签: javascript node.js selenium cucumber

该项目正在使用NodeJS,黄瓜,小黄瓜,硒。

我正在尝试传递存储的值,或者现在,在此示例中,该值将是通过使用正则表达式从功能文件到步骤定义文件的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(); 
};

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

如果您需要有关我的项目的更多信息,请让我知道我整日都在堆栈溢出方面很活跃,并希望尽快进行这项工作。

在此先感谢您的支持, 杰克

2 个答案:

答案 0 :(得分: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函数现在将收到两个变量,urlport。将它们登录到控制台,您将看到

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的所有文档。

答案 1 :(得分:0)

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

通用RegEx模式适用于黄瓜is-([^“] *)中的所有类型的数据 这就是完成完整步骤def-

的过程

@Given(“ ^我用([^”] *)$“)打开页面//请参见下面的注释* 公共无效yourMethodName(yourdatatType yourDataVar){

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

}

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