Question

I have a cucumber step like this (it is not working, but it is a concept):

And (/^I navigate to this url '(.*?)'$/) do |web|
   web = $ + 'web' + '_url'
   @browser.goto web
end

In a different file, paths.rb, I have this hardcoded URL:

$google_url = http://www.google.com

I want to be able to select that URL, by doing something like this:

And I navigate to this url 'google'

Right now, I have not find a way of selecting the 'real content' of the variable $google_url. Any ideas?

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top