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.)?
I think this is a case for writing more declarative steps such as
When the user logs inas 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 inand 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!