Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用capybara、phantomjs和rails在子域上测试javascript_Ruby On Rails_Capybara_Phantomjs_Subdomain - Fatal编程技术网

Ruby on rails 使用capybara、phantomjs和rails在子域上测试javascript

Ruby on rails 使用capybara、phantomjs和rails在子域上测试javascript,ruby-on-rails,capybara,phantomjs,subdomain,Ruby On Rails,Capybara,Phantomjs,Subdomain,解决方案 这起作用了。主要的本质是我必须设置Capybara.server\u端口和Capybara.app\u主机,并在登录表单中手动登录Capybara.app_host不能用动态子域设置,除非在变量中声明它。所有URL都必须硬编码 require 'spec_helper' feature 'customer' do let(:user) {FactoryGirl.create(:user)} let(:firm) {user.firm}

解决方案

这起作用了。主要的本质是我必须设置
Capybara.server\u端口
Capybara.app\u主机
,并在登录表单中手动登录
Capybara.app_host
不能用动态子域设置,除非在变量中声明它。所有URL都必须硬编码

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
原始问题 我正在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
水豚的poltergeist web驱动程序找不到url并返回空白页

Failure/Error: page.should have_content("Add new customer")
       expected there to be text "Add new customer" in ""
我在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
恶灵和幻影试图传递。我得到这个输出

{"name"=>"set_debug", "args"=>[true]}
{"response"=>true}
{"name"=>"visit", "args"=>["http://subdomain2.lvh.me:3003/statistics"]}
poltergeist [1362522132943] state default -> loading
{"response"=>{"status"=>"fail"}}
我是否需要在测试期间运行一台服务器才能正常工作

我尝试过selenium和capybara webkit,但phantomjs已经接近成功

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

欢迎提供有关设置的任何提示

更新

开始绝望了。我现在启动rails服务器

rails s -e test -p 3001
然后运行我的测试

现在我被重定向到登录页面。我在说明书里有这个

before(:each) do
  login_as(user, :scope => :user) 
end

我如何才能在rails测试服务器上登录测试用户,而不必为每个规范手动执行登录过程呢

一些Capybara驱动程序需要在实际的HTTP服务器上运行。水豚负责这一点,并在与测试相同的过程中为您启动一个线程,但在另一个线程上。Selenium是这些驱动因素之一,而RackTest不是

在测试中,您可以使用带有相对url的
visit
方法,例如:

visit("/statistics")
before(:each) do
  visit "/login"
  fill_in "Email", :with => "my@email.com"
  fill_in "Password", :with => "secret"
  click_button "Login"
end
Capybara将把这个请求定向到它刚刚启动的服务器上进行测试

当您希望在测试中使用绝对url时,可以,但也应该指定服务器运行的端口。此端口在测试期间随机选择。有些驱动程序有一种方法可用于检索端口号

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

Capybara.current_session.driver.server_port
访问
您可以使用的绝对url:

port_number = Capybara.current_session.driver.server_port
visit("http://127.0.0.1:#{port_number}/statistics")
在测试规范中,一种方法
login\u as
可能不起作用。您必须通过几个简单的步骤登录。例如:

visit("/statistics")
before(:each) do
  visit "/login"
  fill_in "Email", :with => "my@email.com"
  fill_in "Password", :with => "secret"
  click_button "Login"
end
要测试多个子域,可以设置
Capybara.app\u主机
。请看下面的详细说明

更新

Capybara 2包含一个名为
always\u include\u port
的端口,它将自动添加服务器正在运行的端口号

Capybara.always_include_port = true
所以不是

visit("http://127.0.0.1:#{port_number}/statistics")
您现在可以使用

visit("/statistics")
它将自动连接到
http://127.0.0.1:#{port_number}/statistics

如果要使用
Capybara.app\u host
测试多个子域,可以使用始终解析为127.0.0.1的域名,例如
lvh.me


例如,如果指定
Capybara.app\u host=”http://example.lvh.me“
它将使用
示例
子域运行测试。

您能清楚地解释一下您的问题吗?为什么所有URL都必须硬编码?这是否包括如果我们使用一个工厂生成一个具有子域的对象,然后使用before块来设置app_主机?谢谢你把我推到正确的方向。有了赏金,我想要一个详细的标准答案。我认为你应该在子域部分添加更详细的信息。其他一切都很好。