使用capybara,phantomjs和rails在子域上测试javascript

And*_*tad 10 subdomain ruby-on-rails capybara phantomjs

这很有效.主要的本质是我必须在登录表单中设置Capybara.server_portCapybara.app_host手动登录.Capybara.app_host除非在变量中声明,否则无法使用动态子域进行设置.所有网址都必须硬编码.

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 => user.email
    fill_in "Password", :with => "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
Run Code Online (Sandbox Code Playgroud)

原始问题 我在rails中制作多租户应用程序.会有很多javascript.但是,我无法让测试工作.

当没有运行时,:js = true 每件事都有效.问题出现在像这样的规范中

 let(:customers)  {"http://#{firm.subdomain}.lvh.me:3003/customers"} 
 scenario "Statistics select", :js => true do 
   visit customers
   page.should have_content("Add new customer")
 end
Run Code Online (Sandbox Code Playgroud)

水豚的恶作剧网络驱动程序找不到网址并返回空白页面

Failure/Error: page.should have_content("Add new customer")
       expected there to be text "Add new customer" in ""
Run Code Online (Sandbox Code Playgroud)

我的spec_helper.rb中有这个

require 'capybara/rspec'
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new(app, :debug => true)
end
Run Code Online (Sandbox Code Playgroud)

恶作剧者和幻想者试图提供.我得到了这个输出

{"name"=>"set_debug", "args"=>[true]}
{"response"=>true}
{"name"=>"visit", "args"=>["http://subdomain2.lvh.me:3003/statistics"]}
poltergeist [1362522132943] state default -> loading
{"response"=>{"status"=>"fail"}}
Run Code Online (Sandbox Code Playgroud)

我是否需要在测试期间运行服务器才能使其工作?

我尝试过selenium和capybara-webkit,但是phantomjs最接近成功.

我也试过以不同的方式更改hosts文件(可能不正确)

欢迎任何有关安装的提示!

更新

开始变得绝望.我现在启动rails服务器

rails s -e test -p 3001
Run Code Online (Sandbox Code Playgroud)

然后运行我的测试.

现在我被重定向到登录页面.我在规格中有这个

before(:each) do
  login_as(user, :scope => :user) 
end
Run Code Online (Sandbox Code Playgroud)

如何在rails测试服务器上登录测试用户,而无需为每个规范手动通过登录过程

hjb*_*lok 14

Capybara已经为您启动服务器,引用文档:

一些Capybara驱动程序需要针对实际的HTTP服务器运行.Capybara负责处理这个并在与测试相同的过程中为您启动一个,但在另一个线程上.Selenium是其中一个驱动因素,而RackTest则不是.

在测试中,您可以将该visit方法与相对URL一起使用,例如:

visit("/statistics")
Run Code Online (Sandbox Code Playgroud)

Capybara会将此请求定向到刚开始进行此测试的服务器.

如果您想在测试中使用绝对URL,则可以,但您还应指定运行服务器的端口.在测试期间随机选择此端口.某些驱动程序有一个可用于检索端口号的方法.

例如,当您使用Capybara-Webkit驱动程序时:

Capybara.current_session.driver.server_port
Run Code Online (Sandbox Code Playgroud)

visit绝对网址,您可以使用:

port_number = Capybara.current_session.driver.server_port
visit("http://127.0.0.1:#{port_number}/statistics")
Run Code Online (Sandbox Code Playgroud)

在测试规范中,可能一种方法login_as不起作用.您只需几个简单的步骤即可登录.例如:

before(:each) do
  visit "/login"
  fill_in "Email", :with => "my@email.com"
  fill_in "Password", :with => "secret"
  click_button "Login"
end
Run Code Online (Sandbox Code Playgroud)

要测试多个子域,您可以设置Capybara.app_host.请查看此问题以获取详细说明.

UPDATE

水豚2包括一个很好的功能,always_include_port,它会自动添加的服务器上运行的端口号.

Capybara.always_include_port = true
Run Code Online (Sandbox Code Playgroud)

而不是

visit("http://127.0.0.1:#{port_number}/statistics")
Run Code Online (Sandbox Code Playgroud)

你现在可以使用了

visit("/statistics")
Run Code Online (Sandbox Code Playgroud)

它会自动连接到http://127.0.0.1:#{port_number}/statistics.

如果要使用多个子域进行测试Capybara.app_host,可以使用始终解析为127.0.0.1的域名lvh.me.

例如,如果您指定Capybara.app_host = "http://example.lvh.me"它将使用example子域运行测试.