我正在尝试开始在我的应用程序上编写测试,但是我遇到js: true
的问题,这没有启动它自己的服务器。
环境金属
rails -v #Rails 5.2.0
ruby -v ruby 2.4.2p198
Gemfile:
gem 'capybara'
gem 'selenium-webdriver'
我遵循了本教程中的配置https://www.devmynd.com/blog/setting-up-rspec-and-capybara-in-rails-5-for-testing/
我知道它没有启动它的欠服务器,因为这是我得到的错误
Failures:
1) Login
Failure/Error: expect(page).to have_content("Sarah")
expected to find text "Sarah" in "This site can’t be reached\nlocalhost refused to connect.\nTry:\nChecking the connection\nChecking the proxy and the firewall\nERR_CONNECTION_REFUSED\nReload\nDETAILS"
# ./spec/helpers/login.rb:9:in `login_user'
这是`login_user'文件中的#./spec/helpers/login.rb:9:中的内容
require "rails_helper"
RSpec.feature "Financial Profile", :type => :feature do
scenario "just login", js: true do
login_user
end
def login_user
user = FactoryBot.create(:user, email: '[email protected]', name: 'Sarah Connor')
login_as(user, :scope => :user, :run_callbacks => false)
visit root_path
expect(page).to have_content("Sarah")
expect(page).to have_link("Logout")
end
end
现在,如果我做到了,这将非常完美
scenario "just login", js: false do
login_user
end
这是我如何通过WSL和Ubuntu在Rails 5.2.3上使它工作。我不必在Windows中安装任何东西,但确实在Linux分区中安装了chrome。
# Gemfile
group :test do
gem 'capybara', '~> 3.20', '>= 3.20.2'
gem 'webdrivers', '~> 4.1'
end
# spec/rails_helper.rb
Capybara.register_driver :headless_chrome do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
'goog:chromeOptions': { args: %w(no-sandbox headless disable-gpu window-size=1280,1024 disable-features=VizDisplayCompositor) } )
Capybara::Selenium::Driver.new(app, browser: :chrome, desired_capabilities: capabilities)
end
然后进行相关测试:
describe 'the test that needs javascript', js: true do
before(:all) do
# Selenium::WebDriver.logger.level = :debug
Capybara.current_driver = :headless_chrome
Capybara.javascript_driver = :headless_chrome
end
after(:all) do
Capybara.use_default_driver
end
it 'tests something' do
expect(1).to eq(1)
end
end