【问题标题】:Resize Chrome's window while using Capybara使用 Capybara 时调整 Chrome 的窗口大小
【发布时间】:2013-01-27 17:39:08
【问题描述】:

我使用 Capybara(含硒)、Chrome 和 RSpec。但我想在一些测试中改变浏览器的宽度。这种情况下的解决办法是什么?

spec/spec_helper.rb

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

# Using chrome as browser to test in capybara.
Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome)
end

# RSpec config.
RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller    
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = false

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # Clean test database.
  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end
end

【问题讨论】:

    标签: ruby-on-rails google-chrome selenium rspec capybara


    【解决方案1】:

    Selenium Webdriver 中有resize_to 函数可供您使用:

    page.driver.browser.manage.window.resize_to(1024, 768)
    

    Window 有一些其他可能有用的方法。

    【讨论】:

      【解决方案2】:

      我发现 Driver#browser 调用现已弃用(Capybara 2.5.0)。

      您可以使用Driver#resize_window_to 调用来调整窗口大小:

      page.driver.resize_window_to(page.driver.current_window_handle, 1_200, 800)

      您也可以使用Session#current_window,然后使用Window#resize_to

      page.current_window.resize_to(1_200, 800)

      查看文档了解更多信息:

      http://www.rubydoc.info/github/jnicklas/capybara/Capybara

      【讨论】:

        【解决方案3】:

        迟到的答案,但有人可能需要这个。您可以通过指定窗口大小来启动 chromedriver,以便在加载页面后无需调整大小:

        Capybara.register_driver :chrome_sizes do |app|
          profile = Selenium::WebDriver::Chrome::Profile.new 
        
          Capybara::Selenium::Driver.new(app,
            :browser => :chrome,
            :profile => profile,
            :args => ["--window-size=1240,1400"]
          )
        end
        

        【讨论】:

        【解决方案4】:

        试试这个:

        it "testname", :js => true do
          set_selenium_window_size(1280, 800) if Capybara.current_driver == :selenium 
          # Resize window. NB: cannot be moved to before :all block, because there Capybara.current_driver will be :webkit. It changes to :selenium inside :js => true blocks.
          # ... the rest of your test ...
        end
        
        def set_selenium_window_size(width, height)
          window = Capybara.current_session.driver.browser.manage.window
          window.resize_to(width, height)
        end
        

        感谢 Sidane 的要点https://gist.github.com/Sidane/2204218

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-15
          • 1970-01-01
          • 2019-05-15
          • 1970-01-01
          • 2019-11-26
          相关资源
          最近更新 更多