如何在黄瓜方案中写数字 [英] How to write numbers in cucumber scenarios
问题描述
我想在黄瓜页面上写一个数字。请让我知道我该怎么写。
I would like to write a number in cucumber page. Please let me know How can I write this.
方案概述:输入无效的网址
Scenario Outline: Enter an invalid URL
Given the context "Invalid URL" is open on "Market"
When user is on the error 404 page
Then page not found message is displayed
但是我观察到404被作为参数。
But i have observed that 404 is taken as a parameter.
推荐答案
当然,您只需要在步骤定义中将其作为正则表达式(regex)处理即可。您的步骤定义应如下所示:
Sure, you just have to handle it as a regular expression (regex) in your step definitions. Your step definition could read as following:
@When("^When user is on the error \"(\\d+)\" page$")
public void When_user_is_on_the_error_page(int errorNum) throws Throwable {
...
}
可以处理1个或多个数字。 \d代表一个数字,而+号代表 1或更多。
That'll handle 1 or more digits. The \d represents a digit and the + sign represents "1 or more".
我个人喜欢将参数用双引号()括起来,因为这样IDE理解这是一个参数,可以帮助您完成自动填充等操作。
I personally like wrapping my parameters in double quotes ("") because then the IDE understands it's a parameter and can help you out with autocomplete, etc.
如果您想将其限制为更具体的数字(例如仅三位数字),则可以优化正则表达式到 [\d] {3}
之类的东西。 Oracle在Java正则表达式上提供了经验教训在这里。
If you want to restrict it to more specific numbers (say only three digits) you can refine your regex to something like [\d]{3}
. Oracle have lessons on Java regex here.
这篇关于如何在黄瓜方案中写数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!