title: Testing javascript on subdomain with capybara, phantomjs and rails
body: <p><strong>Solution</strong></p>
  
  <p>This worked. The main essence is that I have to set the <code>Capybara.server_port</code> and <code>Capybara.app_host</code> and sign in manually in the sign in form. <code>Capybara.app_host</code> cannot be set with a dynamic subdomain unless its declared in a variable. All urls has to be hard coded. </p>
  
  <pre><code>require 'spec_helper'
  
  feature 'customer' do
  
      let(:user)        {FactoryGirl.create(:user)}
      let(:firm)        {user.firm} 
      let(:customers)   {"http://#{firm.subdomain}.lvh.me:31234/customers"} 
      let(:root_url)    {"http://#{firm.subdomain}.lvh.me:31234/"}
      before(:all) do 
        Capybara.server_port = 31234 
        sub = firm.subdomain
        Capybara.app_host = root_url 
      end
     def sign_in_on_js
      visit root_url
      fill_in "Email", :with =&gt; user.email
      fill_in "Password", :with =&gt; "password"
      click_button "Sign in"
      page.should have_content("Signed in successfully.") 
     end
  
    scenario "make new", js: true do
      sign_in_on_js
      visit customers
      page.should have_content("Add new customer")       
      find("#dialog_customer").click
      page.should have_content("Create new customer") 
    end 
  end
  </code></pre>
  
  <p><strong>Original question</strong>
  I am making a multitenant app in rails. There is going to be a lot of javascript. But, I cant get the testing to work.</p>
  
  <p>When not running <code>:js = true</code>  every thing works. The problem arises in specs like this one</p>
  
  <pre><code> let(:customers)  {"http://#{firm.subdomain}.lvh.me:3003/customers"} 
   scenario "Statistics select", :js =&gt; true do 
     visit customers
     page.should have_content("Add new customer")
   end
  </code></pre>
  
  <p>The poltergeist web driver for capybara cannot find the url and returns a blank page</p>
  
  <pre><code>Failure/Error: page.should have_content("Add new customer")
         expected there to be text "Add new customer" in ""
  </code></pre>
  
  <p>I have this in my spec_helper.rb</p>
  
  <pre><code>require 'capybara/rspec'
  require 'capybara/poltergeist'
  Capybara.javascript_driver = :poltergeist
  Capybara.register_driver :poltergeist do |app|
    Capybara::Poltergeist::Driver.new(app, :debug =&gt; true)
  end
  </code></pre>
  
  <p>Poltergeist and phantomjs try to deliver. I get this output</p>
  
  <pre><code>{"name"=&gt;"set_debug", "args"=&gt;[true]}
  {"response"=&gt;true}
  {"name"=&gt;"visit", "args"=&gt;["http://subdomain2.lvh.me:3003/statistics"]}
  poltergeist [1362522132943] state default -&gt; loading
  {"response"=&gt;{"status"=&gt;"fail"}}
  </code></pre>
  
  <p>Do I need to have a server running during testing to make this work?</p>
  
  <p>I've tried selenium and capybara-webkit, but phantomjs has gotten closest to success.</p>
  
  <p>I have also tried to change the hosts file in different ways( maybe not correct )  </p>
  
  <p>Any tips on setup are welcome!</p>
  
  <p><strong>Update</strong></p>
  
  <p>Starting to get desperate. I now start the rails server</p>
  
  <pre><code>rails s -e test -p 3001
  </code></pre>
  
  <p>and then run my tests.</p>
  
  <p>Now I get redirected to the sign in page. I have this in the specs </p>
  
  <pre><code>before(:each) do
    login_as(user, :scope =&gt; :user) 
  end
  </code></pre>
  
  <p>How can I sign in the test user on the rails test server without going trough the sign in process manually for every spec</p>