匹配前修改cucumber中的步骤文本
我们有一些配置问题,希望通过在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这是编写更多声明性步骤的情况,例如
当用户登录时
,而不是当我用“fred”填写“txt_user_name”时
。在这种情况下,编写步骤定义会很容易:您甚至可以引入步骤参数转换,将文本“用户”转换为您需要的用户名,因此您不必重复进行更改:
这将匹配步骤
给定用户登录
并传入正确的用户名作为参数。另外,回顾你的问题,你可以使用转换来完成你想要做的事情,并让它替换
__USER__
的实例,但我自己不会选择这个选项 - 感觉就像这会极大地影响场景的可读性。不过你的选择!I think this is a case for writing more declarative steps such as
When the user logs in
as opposed to such things asWhen I fill in "txt_user_name" with "fred"
. It would be easy to write your step definition in that case: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:
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!我通常使用 Cucumber 通过视图登录用户,然后从那里运行测试。
像这样的东西:
I usually use cucumber to log a user in through the view then run tests from there.
Something like: