title: How do I set the URL for a SitePrism::Page that might have a subdomain?
body: <p>The infrastructure I'm working with originally handled the URLs the way the <a href="http://rdoc.info/gems/site_prism/frames" rel="nofollow">Site Prism documentation</a> suggested: the domain was handled by setting <code>Capybara.app_host</code>, and the pages themselves called <code>set_url</code> with a relative path.</p>
  
  <p>It turns out that some pages on the production site have a subdomain, like <code>members.mycompany.com</code> rather than <code>www.mycompany.com</code>. The tests also need to be able to run on test environments with different domains entirely, and in the test environments there are no subdomains at all, even for those pages that do have the subdomain in the production site. So putting the full URL into each <code>SitePrism::Page</code> is not an option.</p>
  
  <p>I believed that the best option was to stop using <code>Capybara.app_host</code> entirely, and create a URL builder that would use the state of the environment (i.e. <code>:test</code> or <code>:production</code>), the relative path to the page, and a flag indicating whether or not the page was part of the production subdomain. Something like:</p>
  
  <pre><code>def get_url(relative_path, subdomain_flag=false)
     case Environment.get_environment
     when :test
        domain = "https://testmachine.localenvironment.com"
     when :production
        if subdomain_flag
           domain = "https://members.mycompany.com"
        else
           domain = "https://www.mycompany.com"
        end
     end
     return "#{domain}" + "#{relative_path}"
  end
  </code></pre>
  
  <p>I had hoped that I could use <code>get_url</code> in each of my <code>SitePrism::Page</code> objects to generate the full URL without the need for <code>Capybara.app_host</code>, like so:</p>
  
  <pre><code>class LoginPage &lt; SitePrism::Page
     set_url get_url("/login")
     ...
  end
  
  class MemberPage &lt; SitePrism::Page
     set_url get_url("/mypage", true)
     ...
  end
  </code></pre>
  
  <p>If the Environment were <code>:test</code>, I'd expect the <code>LoginPage</code> to load <code>https://testmachine.localenvironment.com/login</code> and <code>MemberPage</code> to load <code>https://testmachine.localenvironment.com/mypage</code>. If the Environment were <code>:production</code>, I'd expect <code>LoginPage</code> to load <code>https://www.mycompany.com/login</code> and <code>MemberPage</code> to load <code>https://members.mycompany.com/mypage</code>.</p>
  
  <p>I believe the code in my <code>get_url</code> method does what I think it should do, and I also believe that the Environment object I created is having its state set to the proper environment prior to the pages' <code>#load</code> method being called in the test. Sadly, when I try to load my <code>SitePrism::Page</code> objects, the environment value that gets picked up by <code>get_url</code> is always the default value from the Environment object (which is <code>:test</code>). As a result, it ends up loading the test environment URL even if I'm trying to execute against production.</p>
  
  <p>It feels like the <code>set_url</code> part of each <code>SitePrism::Page</code> object is evaluated before the <code>SitePrism::Page</code> objects are instantiated, and I can't seem to find a way to get my initialization of the Environment to happen prior to it. I'm new to Ruby so I'm hoping I'm just misunderstanding the order or hierarchy that these things are loading in, and that it's not difficult to correct. Can anyone identify what's wrong with my attempt, or, if there is a better way to handle this situation, point me to what that better way is?</p>