在 Rails 应用程序中使用 Selenium WebDriver 和 Capybara 时出错

问题描述 投票:0回答:1

我正在使用 Rails 6.1 和 ruby 2.6.5,并且正在为 Active Admin 新页面创建测试,我发现测试这个新页面的最佳方法是使用 gems Capybara、Webdrivers 和 Selenium-Webdrivers。然而,我在使用这些宝石时遇到了麻烦。有人可以帮我解决这个问题,或者给我一些关于如何测试活动管理页面的想法吗?我已经进行了模型和控制器测试?以下是我的配置。

我的宝石文件

gem“网络驱动程序”

宝石“水豚”

gem 'selenium-webdriver'

我的rails_helper

require 'capybara/rspec'
require 'webdrivers'

Capybara.register_driver :selenium_chrome_headless do |app|
  options = ::Selenium::WebDriver::Options.new
  options.add_argument('--headless')
  options.add_argument('--disable-gpu')
  options.add_argument('--no-sandbox')
  options.add_argument('--disable-dev-shm-usage')
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end

Capybara.default_driver = :selenium_chrome_headless
RSpec.configure do |config|  
  config.include Devise::Test::IntegrationHelpers, type: :request

测试

require 'rails_helper'

RSpec.describe 'Admin TermsAndConditions Management', type: :feature do
  let!(:admin_user) { create(:admin_user) }
  let!(:terms_and_condition) { create(:terms_and_condition, description: 'Initial Terms and Conditions') }

  before do
    login_as(admin_user, scope: :admin_user)
  end

  describe 'Index page' do
    it 'displays the list of terms and conditions' do
      visit admin_terms_and_conditions_path
      expect(page).to have_content('Initial Terms and Conditions')
    end
  end

  describe 'Creating a new TermsAndCondition' do
    it 'creates a new terms and condition' do
      visit new_admin_terms_and_condition_path

      fill_in 'Description', with: 'New Terms and Conditions'
      click_button 'Create Terms and conditions'

      expect(page).to have_content('Terms and condition was successfully created.')
      expect(page).to have_content('New Terms and Conditions')
    end
  end

  describe 'Editing a TermsAndCondition' do
    it 'edits an existing terms and condition' do
      visit edit_admin_terms_and_condition_path(terms_and_condition)

      fill_in 'Description', with: 'Updated Terms and Conditions'
      click_button 'Update Terms and conditions'

      expect(page).to have_content('Terms and condition was successfully updated.')
      expect(page).to have_content('Updated Terms and Conditions')
    end
  end

  describe 'Deleting a TermsAndCondition' do
    it 'deletes an existing terms and condition' do
      visit admin_terms_and_conditions_path

      within("tr", text: terms_and_condition.description) do
        click_link 'Delete'
      end

      expect(page).to have_content('Terms and condition was successfully destroyed.')
      expect(page).not_to have_content(terms_and_condition.description)
    end
  end
end

错误:

1) Admin TermsAndConditions Management Index page displays the list of terms and conditions
     Failure/Error: options = ::Selenium::WebDriver::Options.new
     
     ArgumentError:
       wrong number of arguments (given 0, expected 1)
     # ./spec/rails_helper.rb:19:in `new'
     # ./spec/rails_helper.rb:19:in `block in <top (required)>'
     # ./spec/features/admin/terms_and_conditions_spec.rb:13:in `block (3 levels) in <top (required)>'

  2) Admin TermsAndConditions Management Creating a new TermsAndCondition creates a new terms and condition
     Failure/Error: options = ::Selenium::WebDriver::Options.new
     
     ArgumentError:
       wrong number of arguments (given 0, expected 1)
     # ./spec/rails_helper.rb:19:in `new'
     # ./spec/rails_helper.rb:19:in `block in <top (required)>'
     # ./spec/features/admin/terms_and_conditions_spec.rb:20:in `block (3 levels) in <top (required)>'

  3) Admin TermsAndConditions Management Editing a TermsAndCondition edits an existing terms and condition
     Failure/Error: options = ::Selenium::WebDriver::Options.new
     
     ArgumentError:
       wrong number of arguments (given 0, expected 1)
     # ./spec/rails_helper.rb:19:in `new'
     # ./spec/rails_helper.rb:19:in `block in <top (required)>'
     # ./spec/features/admin/terms_and_conditions_spec.rb:32:in `block (3 levels) in <top (required)>'

  4) Admin TermsAndConditions Management Deleting a TermsAndCondition deletes an existing terms and condition
     Failure/Error: options = ::Selenium::WebDriver::Options.new
     
     ArgumentError:
       wrong number of arguments (given 0, expected 1)
     # ./spec/rails_helper.rb:19:in `new'
     # ./spec/rails_helper.rb:19:in `block in <top (required)>'
     # ./spec/features/admin/terms_and_conditions_spec.rb:44:in `block (3 levels) in <top (required)>'
ruby-on-rails selenium-webdriver rspec webdriver capybara
1个回答
0
投票

我可能是错的,但我认为你的问题是你直接初始化超级类。 您应该初始化特定的浏览器选项:

    options = Selenium::WebDriver::Chrome::Options.new

这是我的 test_helper 的配置:

client = Selenium::WebDriver::Remote::Http::Default.new

Capybara.register_driver :chrome do |app|
  options = ::Selenium::WebDriver::Chrome::Options.new
  options.add_argument('--window-size=375,812') if ENV['MOBILE'] == 'true'
  Capybara::Selenium::Driver.new(app, browser: :chrome, http_client: client, options: options)
end
© www.soinside.com 2019 - 2024. All rights reserved.