匹配前修改cucumber中的步骤文本

发布于 2024-10-14 15:41:07 字数 509 浏览 2 评论 0原文

我们有一些配置问题,希望通过在 Cucumber 中放入“特殊常量”来解决。例如,我们希望在步骤中使用文本 "__USER__" 的任何地方,都应将其替换为运行应用程序的当前用户(以便我们可以测试用户权限等内容)。

我试图采取的策略是做这样的事情:

BeforeStep do |step|
    domain = get_domain()
    username = get_username()
    step.text.gsub("__USER__", "#{domain}/#{username}")
end

但是,没有 BeforeStep。我尝试使用 Before do |scenario| ... end 但该场景没有任何我可以使用的东西。我们如何将代码中的 "__USER__" 实例替换为用户(以及将 "__CURRENT_DATE__" 实例替换为当前日期等)?

We have some configuration issues that we would like to solve by putting in "special constants" in Cucumber. For example, we would like anywhere that the text "__USER__" is used in a step, that should be replaced with the current user running the app (so that we can test things like user permissions).

The strategy I was trying to take toward this was to do something like this:

BeforeStep do |step|
    domain = get_domain()
    username = get_username()
    step.text.gsub("__USER__", "#{domain}/#{username}")
end

However, there is no BeforeStep. I tried to use Before do |scenario| ... end but the scenario didn't have anything I could use on it. How can we replace instances of "__USER__" in our code with the user (and instances of "__CURRENT_DATE__" with the current date, etc.)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

渔村楼浪 2024-10-21 15:41:07

我认为这是编写更多声明性步骤的情况,例如当用户登录时,而不是当我用“fred”填写“txt_user_name”时。在这种情况下,编写步骤定义会很容易:

When /^the user logs in$/ do
    domain = get_domain()
    username = get_username()
    fill_in "txt_user_name", :with => "#{domain}/#{username}"
end

您甚至可以引入步骤参数转换,将文本“用户”转换为您需要的用户名,因此您不必重复进行更改:

CAPTURE_USER = Transform /^(the user)$/ do |this_isnt_used|
    domain = get_domain()
    username = get_username()
    "#{domain}/#{username}"
end

When /^(#{CAPTURE_USER }) logs in$/ do |user_name|
    puts "Logging in as #{user_name}"
end

这将匹配步骤给定用户登录并传入正确的用户名作为参数。

另外,回顾你的问题,你可以使用转换来完成你想要做的事情,并让它替换 __USER__ 的实例,但我自己不会选择这个选项 - 感觉就像这会极大地影响场景的可读性。不过你的选择!

I think this is a case for writing more declarative steps such as When the user logs in as opposed to such things as When I fill in "txt_user_name" with "fred". It would be easy to write your step definition in that case:

When /^the user logs in$/ do
    domain = get_domain()
    username = get_username()
    fill_in "txt_user_name", :with => "#{domain}/#{username}"
end

You could even bring in a step argument transform to turn the text 'the user' into the username you need, so you don't have to repeatedly make the change:

CAPTURE_USER = Transform /^(the user)$/ do |this_isnt_used|
    domain = get_domain()
    username = get_username()
    "#{domain}/#{username}"
end

When /^(#{CAPTURE_USER }) logs in$/ do |user_name|
    puts "Logging in as #{user_name}"
end

This would match the step Given the user logs in and pass in the correct user name as a parameter.

Also, looking back at your question, you could use a transform to accomplish exactly what you were trying to do and have it replace instances of __USER__, but I wouldn't choose that option myself - it feels like it would impact the readability of the scenario too much. Your choice though!

雨后彩虹 2024-10-21 15:41:07

我通常使用 Cucumber 通过视图登录用户,然后从那里运行测试。

像这样的东西:

Given a user exists with email: "[email protected]", account_type: "customer" 
When I go to the homepage 
And I follow "Sign in" 
And I fill in "email" with "[email protected]" 
And I fill in "password" with "password" 
And I press "Sign in"

I usually use cucumber to log a user in through the view then run tests from there.

Something like:

Given a user exists with email: "[email protected]", account_type: "customer" 
When I go to the homepage 
And I follow "Sign in" 
And I fill in "email" with "[email protected]" 
And I fill in "password" with "password" 
And I press "Sign in"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文