0

I'm defining a CONSTANT in my env.rb file in cucumber as:

CONFIG = YAML::load_file("#{File.dirname(__FILE__)}/../../configs/config.yaml")

Then I want to access values from that 'config.yaml' file, like CONFIG['url'].

The problem I'm having is that I've got a page object siteprism class and I don't know how to access it?

class MyPage < SitePrism::Page
  set_url CONFIG['url']
...

it's failing with:

uninitialized constant MyPage::CONFIG (NameError)

I want to access the CONFIG constant that was defined in my env.rb file though, not a constant from MyPage. Do you guys know how I could achieve this?

mickael
  • 2,033
  • 7
  • 25
  • 38

2 Answers2

0

Use ::, like ::CONFIG['url'].

Notice that given that you have "YAML::load_file(" for CONFIG, the value of this expression would be nil.

sawa
  • 165,429
  • 45
  • 277
  • 381
  • sorry, the quotes were a typo. Edited. When I tried ::CONFIG['url'] inside the MyPage class I got: `uninitialized constant CONFIG Did you mean? RbConfig (NameError)` – mickael Dec 05 '16 at 12:46
  • Is the env.rb file somehow loaded? – sawa Dec 06 '16 at 04:40
  • I think the problem is something specific to SitePrism, although I'm not sure what. That gem has a method called `set_url`, so if I try to use CONFIG there it doesn't work and fails with the errors above. However, if I try to use CONFIG in that class but inside a new method that I create there it works fine. I tried to workaround the issue by using `set_url(new_method_to_retrieve_CONFIG)`, but that didn't work either. Not sure if there's any workaround. For now, I'm just duplicating the 'url' in my config file and in the `set_url "http://url"` command... – mickael Dec 06 '16 at 11:43
0

Ensure your load order is correct. Use toplevel namespace as advised. Better logic would be to delegate this to a Helper Module, include this helper module in your class. Then call the helper Module method

class MyKlass
  include MyHelper

  set_url helper_method[:key]
end

module MyHelper
  def helper_method
    {
      key: 'http://www.google.com/'
    }
  end
end

If anyone else believes that set_url isn't working. Feel free to raise an issue on the SitePrism repo: https://github.com/natritmeyer/site_prism/issues, or ask here if easier

Luke Hill
  • 460
  • 2
  • 10