Javascript 如何使用正则表达式将数据从要素文件传递到步骤定义文件?
这个项目使用的是NodeJS、黄瓜、小黄瓜、硒 我试图传递一个存储的值,或者现在传递一个值,在本例中,它是一个URL,通过使用正则表达式从功能文件传递到步骤定义文件 我想使用的一个功能示例(Javascript 如何使用正则表达式将数据从要素文件传递到步骤定义文件?,javascript,node.js,selenium,cucumber,Javascript,Node.js,Selenium,Cucumber,这个项目使用的是NodeJS、黄瓜、小黄瓜、硒 我试图传递一个存储的值,或者现在传递一个值,在本例中,它是一个URL,通过使用正则表达式从功能文件传递到步骤定义文件 我想使用的一个功能示例(是我在其他示例中看到的猜测工作,它似乎不起作用) 我相信我的步骤定义文件是正确的,但我不是肯定的 如果你需要关于我的项目的任何更多信息,请让我知道我整天都在积极处理堆栈溢出问题,并希望能尽快解决这个问题 提前感谢您的支持, Jack我会尝试将JavaScript中的正则表达式更改为一个字符串,该字符串期望传递
Jack我会尝试将JavaScript中的正则表达式更改为一个字符串,该字符串期望传递到给定的
语句中的变量:
Given('I open the page with the url {string}'), function(next) {
//your code
}
您可以在给定的
语句中定义变量,方法是按照通常呈现的方式进行说明。例如,如果要传入字符串
:
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函数现在将接收两个变量,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();
});
编辑:
您可以找到有关此特定问题的所有文档。下面是一个简短的答案-可能对即将到来的访问者有用 通用正则表达式模式适用于cucumber is-([^“]*)中的所有类型的数据 这就是完成步骤def的方式- @给定(“^I使用([^”]*)$”打开页面//请参见下面的注释* public void yourMethodName(yourdatatType yourDataVar){ 使用(yourDataVar)实现您的方法 }
//*注意-“^”和“$”符号将分别由cucumber自动添加到任何步骤定义的开始和结束处。这是有效的,感谢您的帮助。非常感谢@blueprintChris
Scenario: 0 | A User Logging In
Given I open the page with the url "http://localhost:3000/login"
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();
});