水豚:如何更改窗口大小?

时间:2018-10-12 14:50:10

标签: ruby selenium capybara

要实现一些简单操作(如更改运行Capybara + webdriver的浏览器窗口)似乎非常困难

我正在针对外部网站进行测试。 我想要的是最大化浏览器窗口。 我在网络上发现了许多提示,但似乎无济于事。要复制的最少代码是:

require 'capybara'
require 'selenium-webdriver'

session = Capybara::Session.new(:selenium)
window = Capybara.current_session.current_window
window.resize_to(1920,1080)
sleep(10)

结果是

Traceback (most recent call last):
        5: from C:/src/Ruby/Capybara/minimal.rb:5:in `<main>'
        4: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/capybara-3.9.0/lib/capybara/session.rb:426:in `current_window'
        3: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/capybara-3.9.0/lib/capybara/session.rb:102:in `driver'
        2: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/capybara-3.9.0/lib/capybara.rb:519:in `block in <top (required)>'
        1: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/capybara-3.9.0/lib/capybara.rb:519:in `new'
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/capybara-3.9.0/lib/capybara/rack_test/driver.rb:18:in `initialize': rack-test requires a rack application, but none was given (ArgumentError)

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您正在创建一个会话,但是随后在另一个会话的窗口上调用了resize。另一个会话是使用默认驱动程序(机架测试)自动生成的会话,该驱动程序根本不支持Windows。如果要通过手动创建的会话执行此操作,则只需

session = Capybara::Session.new(:selenium)
session.current_window.resize_to(1920,1080)