gpt4 book ai didi

java - 从 Gherkin 语句中转义数字或带引号的字符串

转载 作者:行者123 更新时间:2023-12-01 15:25:59 25 4
gpt4 key购买 nike

如果我的功能定义中有这样的子句:

Then I can see the "/relative-url-path" page

Cucumber会强加这个方法:

@When("^I can see the \"([^\"]*)\" page$")
public void I_open_the_page(String arg1) {
// Express the Regexp above with the code you wish you had
throw new PendingException();
}

如果我确实想用引号突出显示 URL 相关部分,如何强制 gherkin 解析器将 THEN close 解释为“纯字符串”。换句话说,我可以如何逃避它吗?

如果我有电话号码,也会有同样的问题吗?

最佳答案

首先,如果您使用 Ruby 作为步骤定义,我认为“何时”前面不应该有 @ 符号。这可能会给您带来问题(我不知道)。如果您不使用 Ruby,那么了解您的步骤定义使用的语言会很有帮助。

我可以告诉你我对引号中的文件路径做了什么:

When I upload invoice "C:\Ruby193\automation\myfile.txt"

然后我使用了这段代码:

When /^I upload invoice "(.*)"$/ do |filename|
@upload_invoice_page = UploadInvoicePage.new(@test_env)
@upload_invoice_page.upload_file(filename, 'BIRD, INC.')
end

按照该示例,在 Ruby 中,我将尝试为您的步骤编写此代码:

When /^ can see the "(.*)" page$/

您的代码看起来可能像 Java,因此可能类似于:

@When("^I can see the \"(.*)\" page$")

您可以在其中放置更复杂的正则表达式,但由于它是 Gherkin 步骤,因此您实际上并不需要它。看来目前您正在尝试获取非双引号的任何内容。您不需要这样做,因为正则表达式已经在寻找开盘价和收盘价。

请记住,您也可以完全删除引号:

Then I can see the /relative-url-path page

@When("^I can see the (.*) page$")

仅在您认为更易于阅读时才保留引号。有关正则表达式的更多信息

要仅匹配数字,您会这样做:

Then I can see the 123456

@Then("^I can see the (\d*)$")

我发现理查德·劳伦斯的 Cucumber Regex Cheatsheet 非常有帮助。您会在那里找到您需要的大部分模式。如果您需要更复杂的模式,您可以考虑是否最好在步骤定义代码中进行该评估。

关于java - 从 Gherkin 语句中转义数字或带引号的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10144860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com