Solution - Using eval (bad idea)
The quickest and most direct solution is to use eval
to execute the string to get the variable. However, it is often suggested that eval
is a bad idea.
And (/^I navigate to this url '(.*?)'$/) do |web|
url = eval('$' + web + '_url')
@browser.goto url
end
Solution - Create a module and use send
I think a better idea would be to create a module that includes all your path constants. This would be better because (1) you prevent polluting the space with many global variables and (2) using send
is safer than eval
.
In your paths.rb, you could replace your global variables with a module that can return the different urls:
module Paths
class << self
def google_url()
'http://www.google.com'
end
end
end
You would then use send
in your steps to translate the string to a method:
And (/^I navigate to this url '(.*?)'$/) do |web|
url = Paths.send(web + '_url')
@browser.goto url
end