该项目使用 NodeJS、Cucumber、Gherkin、Selenium。
我正在尝试传递一个存储的值,或者现在,本例中的值将是通过使用正则表达式从特征文件到步骤定义文件的 URL。
我想使用的功能示例(< 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("[email protected]");
next();
};
我相信我的步骤定义文件是正确的,但我并不肯定。
如果您需要有关我的项目的更多信息,请让我知道我全天都在堆栈溢出方面很活跃,并希望尽快实现这一目标。
预先感谢您的支持, 杰克
我会尝试将 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("[email protected]");
next();
});
编辑: 您可以在此处找到有关此特定问题的所有文档。
下面的简短回答 - 可能对即将到来的访客有用!
通用正则表达式模式适用于黄瓜中所有类型的数据 -
([^"]*)
这就是完成步骤 def- 的方式
@Given("^I open the page with the ([^"]*)$") //See note below *
public void yourMethodName(yourdatatType yourDataVar) {
Your Method(s) implementation using (yourDataVar)
...
}
// * Note- '^' and '$' symbols will be added automatically by cucumber in the beginning and end of any step definition respectively.