ruby-on-rails - 在使用Capybara时调整Chrome窗口的大小

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

我使用Capybara(含 Selenium ),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

最佳答案

您可以使用Selenium Webdriver中的resize_to函数:

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

Window还有一些其他有用的方法。

关于ruby-on-rails - 在使用Capybara时调整Chrome窗口的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14550137/

相关文章:

javascript - 响应 header 中的 Set-Cookie 未保存到浏览器 cookie 存储中

angular - 如何不忘记再次打开 Protractor 到 Angular 同步?

selenium - Xpath选择器差异: pros and cons

ruby-on-rails - 如何在 ruby​​ on rails 中显示父表列名?

ruby-on-rails - 使用前端服务器和不同机器上的音频文件在 LAN 上运行 Ruby on rails 应用程序的流式音频?

ruby-on-rails - rails 4 : use of form_for , 没有路线,POST

jquery - 确定 JQuery 效果是否仍在执行

ruby-on-rails - stub AWS S3 对象请求

css - 如何使顶部/底部边框出现在左/右边框的顶部跨浏览器 css

google-chrome - 如何将 webpack sourcemap 制作成原始文件