ruby 更改默认 Capybara 浏览器窗口大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18390071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:08:21  来源:igfitidea点击:

Change default Capybara browser window size

rubyseleniumrspeccapybara

提问by CDub

So, with respect to integration testing using Capybara and RSpec, I know I can do this:

因此,关于使用 Capybara 和 RSpec 的集成测试,我知道我可以这样做:

page.driver.browser.manage.window.resize_to(x,y)

per How to set Browser Window size in Rspec (Selenium)for specific RSpec tests, but is there a way to do this globally so that every test that is affected by media queries doesn't have to define this?

每个如何在Rspec (Selenium) 中为特定 RSpec 测试设置浏览器窗口大小,但是有没有办法全局执行此操作,以便每个受媒体查询影响的测试都不必定义它?

采纳答案by Amey

You could define that under before(:all)

你可以定义下 before(:all)

describe "Test" do
  before(:all) do
    ...
    ...
    page.driver.browser.manage.window.resize_to(x,y) #Mention it here
  end

  it "should find everything" do
    ...
  end

  after(:all) do
   ...
  end
end

回答by Mike Szyndel

A proper way to do it for all js tests is to add following inside spec_helper.rbRSpec.configureblock

对所有 js 测试执行此操作的正确方法是在spec_helper.rbRSpec.configure块内添加以下内容

config.before(:each, js: true) do
  Capybara.page.driver.browser.manage.window.maximize
end

to maximize the window. Change to resize_to(x,y)to set any window size.

最大化窗口。更改为resize_to(x,y)设置任何窗口大小。

EDIT: If you happen to be using Poltergeist the correct way to do it is

编辑:如果您碰巧正在使用 Poltergeist,那么正确的做法是

config.before(:each, js: true) do
  Capybara.page.driver.browser.resize(x,y)
end

回答by rattray

Perhaps due to a recent change in Capybara, what worked for me was:

也许是由于 Capybara 最近的变化,对我有用的是:

before do
  Capybara.page.current_window.resize_to(x, y)
end

回答by ilgam

For test runtime in Capybara 2.2.4version you can achieve this by doing

对于Capybara 2.2.4版本中的测试运行时,您可以通过执行此操作来实现

before do
  handle = Capybara.page.driver.current_window_handle
  Capybara.page.driver.resize_window_to(handle, height, width)
end

Or

或者

before do   
  Capybara.page.current_window.resize_to(height, width)
end

If you get Capybara::NotSupportedByDriverError: Capybara::Driver::Base#current_window_handle YOU MUST CHANGE YOUR DRIVER FOR EXAMPLE USE JAVASCRIPT DRIVER!

如果您收到 Capybara::NotSupportedByDriverError: Capybara::Driver::Base#current_window_handle 您必须更改您的驱动程序,例如使用JAVASCRIPT 驱动程序

before do   
  Capybara.page.current_window.resize_to(height, width)
end

scenario js: true do
  # your test here
end

回答by Nico Brenner

@tirdadc if you're using Poltergeist, you can add something like this to your rails_helper.rbfile:

@tirdadc 如果您使用的是 Poltergeist,则可以在rails_helper.rb文件中添加如下内容:

Capybara.register_driver :poltergeist do |app|
  options = {
    # js_errors: true,
    # cookies: true,
    window_size: [320, 568] # iphone 5
  }
  Capybara::Poltergeist::Driver.new(app, options)
end