如何以较低的速度运行 Selenium(通过 Capybara 使用)?

发布于 2024-10-14 20:28:34 字数 494 浏览 5 评论 0原文

默认情况下,Selenium 在我使用 Cucumber 定义的场景中尽可能快地运行。 我想将其设置为以较低的速度运行,以便我能够捕获该过程的视频。

我发现 Selenium::Client::Driver 的实例有一个 set_speed 方法。对应于 Java API

如何获取 Selenium::Client::Driver 类的实例?我最多可以到达 page.driver,但它会返回 Capybara::Driver::Selenium 的实例。

By default Selenium runs as fast as possible through the scenarios I defined using Cucumber.
I would like to set it to run at a lower speed, so I am able to capture a video of the process.

I figured out that an instance of Selenium::Client::Driver has a set_speed method. Which corresponds with the Java API.

How can I obtain an instance of the Selenium::Client::Driver class? I can get as far as page.driver, but that returns an instance of Capybara::Driver::Selenium.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

输什么也不输骨气 2024-10-21 20:28:34

感谢 http://groups.google.com/group/ruby-capybara/msg/6079b122979ffad2 寻求提示。

请注意,这使用了 Ruby 的睡眠,因此有点不精确 - 但应该可以为您完成这项工作。此外,所有事情都会调用执行,因此这就是亚秒级等待的原因。中间步骤 - 等待准备就绪、检查字段、聚焦、输入文本 - 每次暂停。

在 features/support 目录中创建一个“throttle.rb”(如果使用 Cucumber)并填充:

require 'selenium-webdriver'
module ::Selenium::WebDriver::Firefox
  class Bridge
    attr_accessor :speed

    def execute(*args)
      result = raw_execute(*args)['value']
      case speed
        when :slow
          sleep 0.3
        when :medium
          sleep 0.1
      end
      result
    end
  end
end

def set_speed(speed)
  begin
    page.driver.browser.send(:bridge).speed=speed
  rescue
  end
end

然后,在步骤定义中,调用:

set_speed(:slow)

或:

set_speed(:medium)

要重置,调用:

set_speed(:fast)

Thanks to http://groups.google.com/group/ruby-capybara/msg/6079b122979ffad2 for a hint.

Just a note that this uses Ruby's sleep, so it's somewhat imprecise - but should do the job for you. Also, execute is called for everything so that's why it's sub-second waiting. The intermediate steps - wait until ready, check field, focus, enter text - each pause.

Create a "throttle.rb" in your features/support directory (if using Cucumber) and fill it with:

require 'selenium-webdriver'
module ::Selenium::WebDriver::Firefox
  class Bridge
    attr_accessor :speed

    def execute(*args)
      result = raw_execute(*args)['value']
      case speed
        when :slow
          sleep 0.3
        when :medium
          sleep 0.1
      end
      result
    end
  end
end

def set_speed(speed)
  begin
    page.driver.browser.send(:bridge).speed=speed
  rescue
  end
end

Then, in a step definition, call:

set_speed(:slow)

or:

set_speed(:medium)

To reset, call:

set_speed(:fast)
楠木可依 2024-10-21 20:28:34

这将起作用,并且不那么脆弱(对于“less”的一些小值)

require 'selenium-webdriver'
module ::Selenium::WebDriver::Remote
  class Bridge
    alias_method :old_execute, :execute
    def execute(*args)
      sleep(0.1)
      old_execute(*args)
    end
  end
end

This will work, and is less brittle (for some small value of "less")

require 'selenium-webdriver'
module ::Selenium::WebDriver::Remote
  class Bridge
    alias_method :old_execute, :execute
    def execute(*args)
      sleep(0.1)
      old_execute(*args)
    end
  end
end
审判长 2024-10-21 20:28:34

作为更新,该类中的执行方法不再可用。它现在只在这里:

module ::Selenium::WebDriver::Remote

我需要限制 IE 中的一些测试,这有效。

As an update, the execute method in that class is no longer available. It is now here only:

module ::Selenium::WebDriver::Remote

I needed to throttle some tests in IE and this worked.

我的痛♀有谁懂 2024-10-21 20:28:34

此线程中提到的方法不再适用于 Selenium Webdriver v3。

相反,您需要向执行命令添加睡眠。

module Selenium::WebDriver::Remote
  class Bridge
    def execute(command, opts = {}, command_hash = nil)
      verb, path = commands(command) || raise(ArgumentError, "unknown command: #{command.inspect}")
      path = path.dup

      path[':session_id'] = session_id if path.include?(':session_id')

      begin
        opts.each { |key, value| path[key.inspect] = escaper.escape(value.to_s) }
      rescue IndexError
        raise ArgumentError, "#{opts.inspect} invalid for #{command.inspect}"
      end

      Selenium::WebDriver.logger.info("-> #{verb.to_s.upcase} #{path}")
      res = http.call(verb, path, command_hash)
      sleep(0.1) # <--- Add your sleep here.
      res
    end
  end
end

请注意,这是一种非常脆弱的减慢测试速度的方法,因为您正在对私有 API 进行猴子修补。

The methods mentioned in this thread no longer work with Selenium Webdriver v3.

You'll instead need to add a sleep to the execution command.

module Selenium::WebDriver::Remote
  class Bridge
    def execute(command, opts = {}, command_hash = nil)
      verb, path = commands(command) || raise(ArgumentError, "unknown command: #{command.inspect}")
      path = path.dup

      path[':session_id'] = session_id if path.include?(':session_id')

      begin
        opts.each { |key, value| path[key.inspect] = escaper.escape(value.to_s) }
      rescue IndexError
        raise ArgumentError, "#{opts.inspect} invalid for #{command.inspect}"
      end

      Selenium::WebDriver.logger.info("-> #{verb.to_s.upcase} #{path}")
      res = http.call(verb, path, command_hash)
      sleep(0.1) # <--- Add your sleep here.
      res
    end
  end
end

Note this is a very brittle way to slow down the tests since you're monkey patching a private API.

忘东忘西忘不掉你 2024-10-21 20:28:34

我想减慢水豚测试套件中的页面加载速度,看看是否可以触发一些间歇性失败的测试。我通过创建一个 nginx 反向代理容器并将其放置在我的测试容器和我用作无头浏览器的 phantomjs 容器之间来实现这一点。使用 limit_rate 指令限制速度。它最终没有帮助我实现我的目标,但它确实有效,并且可能对其他人来说是一个有用的策略!

I wanted to slow down the page load speeds in my Capybara test suite to see if I could trigger some intermittently failing tests. I achieved this by creating an nginx reverse proxy container and sitting it between my test container and the phantomjs container I was using as a headless browser. The speed was limited by using the limit_rate directive. It didn't help me to achieve my goal in the end, but it did work and it may be a useful strategy for others to use!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文