为 selenium 远程设置 Rails 环境 url

set Rails environment url for selenium remote

我配置了 selenium 远程并且 运行。我可以使用 minitest 在水豚上使用 'visit root_path' 等测试方法从我的计算机访问我的开发服务器(使用 'rails s' 部署)'192.168.1.23:3000'。

但是,当我关闭我的开发服务器时,我无法在测试时访问我的测试服务器,我只是收到 chrome 找不到页面的错误

测试文件:

require "test_helper"
feature "dashboard" do
  scenario "test1", :js => true  do
  #Rails.application.routes.default_url_options[:host]= 'http://192.168.1.23:3000' 
  visit root_path
end 

注意:当 运行 访问 root_path

时激活第 4 行给我 nil

test_helper.rb

Capybara.configure do |config| 
  config.default_host = 'http://192.168.1.23:3000'
  config.app_host   = 'http://192.168.1.23:3000'
end

我也tried 测试环境,test.rb

Rails.application.default_url_options = {:host => "http://192.168.1.23:3000" }
Rails.application.routes.default_url_options = {:host => "http://192.168.1.23:3000" }

编辑: 我已将 rails 服务器配置为侦听外部地址:

boot.rb

#this lets listen to some interfaces,
#https://fullstacknotes.com/make-rails-4-2-listen-to-all-interface/
require 'rubygems'  
require 'rails/commands/server'

module Rails  
  class Server
    alias :default_options_bk :default_options
    def default_options
      default_options_bk.merge!(Host: '192.168.1.23')
    end
  end
end  

默认情况下,Capybara 在随机端口上运行应用程序,但您已将 app_host 设置为仅连接到端口 3000(这是开发服务器的默认端口)。而不是在 app_host set

中设置固定端口
Capybara.always_include_port = true
Capybara.app_host = 'http://192.168.1.23`

然后,如果由于您的 selenium 远程计算机和本地计算机之间的防火墙问题,随机端口分配无法正常工作,您可以设置

Capybara.server_port = <some fixed port number the test app will get run on>