ruby-on-rails 如何设置默认浏览器大小与水豚+ Selenium + Firefox?

1wnzp6jl  于 2023-06-25  发布在  Ruby
关注(0)|答案(1)|浏览(156)

看看www.example.com和其他几个地方,似乎更改默认选项的方法是用您自己的代码覆盖驱动程序注册。https://gist.github.com/mars/6957187#gistcomment-3019727 and several other places, it appears that the way to change the default options is to overwrite the driver registration with your own code. Here's my driver registration:

RSpec.configure do |config|
  Capybara.register_driver :selenium_headless do |app|
    version = Capybara::Selenium::Driver.load_selenium
    options_key = Capybara::Selenium::Driver::CAPS_VERSION.satisfied_by?(version) ? :capabilities : :options
    browser_options = ::Selenium::WebDriver::Firefox::Options.new.tap do |opts|
      opts.add_argument '-headless'
      opts.add_argument '--window-size=1920,1080'
    end
    Capybara::Selenium::Driver.new(app, **{ :browser => :firefox, options_key => browser_options })
  end
end

我知道这是有效的,因为窗口大小显然被传递到Firefox:

root        1466    1449  0 00:47 pts/5    00:00:00 /root/.webdrivers/geckodriver --port=4444 --binary=/usr/bin/firefox
root        1469    1466  1 00:47 pts/5    00:00:04 firefox-esr --marionette -headless --window-size=1920,1080 -foreground -no-remote -profile /tmp/rust_mozprofileAPMJOb

但是,当我中断测试,然后使用save_and_open_screenshot时,浏览器肯定不是1920x1080:

General
Complete name                            : capybara-202106300047373170133175.png
Format                                   : PNG
Format/Info                              : Portable Network Graphic
File size                                : 45.1 KiB

Image
Format                                   : PNG
Format/Info                              : Portable Network Graphic
Format_Compression                       : Deflate
Width                                    : 1 366 pixels
Height                                   : 694 pixels
Color space                              : RGBA
Bit depth                                : 8 bits
Compression mode                         : Lossless
Stream size                              : 45.1 KiB (100%)

我不知道为什么浏览器的大小似乎是错误的。如果我手动调整页面大小,则保存的图像工作正常:

[9] pry(#<RSpec::ExampleGroups::TheUserLoginAndAvatarArea::ClickingTheDropdownForTheUserProfile>)> page.driver.browser.manage.window.resize_to(1920,1080)
=> {"x"=>0, "y"=>0, "width"=>1920, "height"=>1080}

但重点是不需要这么做。
我已经看到了几个在before中使用resize_to的例子,但这也不是最佳的。
有没有想过为什么这不像预期的那样工作?

    • 编辑:**它实际上与Chromium一起工作:
Capybara.register_driver :selenium_chrome_headless do |app|
    version = Capybara::Selenium::Driver.load_selenium
    options_key = Capybara::Selenium::Driver::CAPS_VERSION.satisfied_by?(version) ? :capabilities : :options
    browser_options = ::Selenium::WebDriver::Chrome::Options.new.tap do |opts|
      opts.add_argument('--no-sandbox')
      opts.add_argument('--window-size=1920,1080')
      opts.add_argument('--headless')
      opts.add_argument('--disable-gpu') if Gem.win_platform?
      # Workaround https://bugs.chromium.org/p/chromedriver/issues/detail?id=2650&q=load&sort=-id&colspec=ID%20Status%20Pri%20Owner%20Summary
      opts.add_argument('--disable-site-isolation-trials')
    end
  
    Capybara::Selenium::Driver.new(app, **{ :browser => :chrome, options_key => browser_options })
  end
ipakzgxi

ipakzgxi1#

我相信Firefox选项的参数名称略有不同,您可以通过以下方式实现更小的浏览器大小:

Capybara.register_driver :mobile_driver_without_ui do |app|
  browser_options =
    ::Selenium::WebDriver::Firefox::Options.new.tap do |opts|
      opts.add_argument('--width=335')
      opts.add_argument('--height=1080')
      opts.add_argument('--headless')
    end

  Capybara::Selenium::Driver.new(app, options: browser_options)
end

这帮助我解决了这个问题https://github.com/mozilla/geckodriver/issues/1354#issuecomment-413552319

相关问题