如何在 Ruby 中将 DevTools Network 与 Selenium 4 结合使用

问题描述 投票:0回答:2

我正在将 Selenium 4 与 Ruby 结合使用,并尝试捕获网络活动,但我不知道如何使用这些库,也没有找到与 Ruby 相关的任何内容。

所以我所做的是:

  • 我在 Gemfile 中添加了 DevTool gem:
    gem 'selenium-devtools', '~> 0.91.1'
  • 我正在尝试这个代码:
devtools = browser.devtools
network = Selenium::DevTools::V91::Network.new(devtools)
network.enable

但似乎不起作用。

调试时,我看到一些奇怪的东西:在创建 DevTools 对象时,

socket
方法似乎出现异常:

不幸的是,我不明白为什么会发生这种情况,也没有找到任何相关信息。我试图强行使用

.new
方法,但它在某个地方失败了,但它不会带我去任何地方。

您是否偶然发现过类似的东西,或者有人对如何在 Ruby 中正确使用 DevTools 有一些建议吗?

ruby selenium google-chrome-devtools selenium4
2个回答
0
投票

我还没有使用过 Selenium 4 DevTools,但是还有另一种方法可以合并它。

要访问 DevTools,您需要首先在 Ruby 中的 YML 文件中提及它们。像这样的东西

chrome:
  switches:
    - auto-open-devtools-for-tabs
  prefs:
    download:
        default_directory: downloads/
    devtools:
        preferences:
            panel-selectedTab: '"network"'

同样,如果您想打开控制台选项卡,您可以从网络更改为控制台选项卡。

如果你想记录内容,你可以在 Ruby 中尝试这个特殊的 gem,它有助于在 Ruby 中屏幕记录测试用例Screenrecord


0
投票

似乎没有关于使用开发工具的 ruby 文档,但这对我有用:

# Install the selenium-webdriver and selenium-devtools gems

require "selenium-webdriver"

driver = Selenium::WebDriver.for :chrome
devtools = driver.devtools

network = devtools.send(:network)
network.enable

network.on(:request_will_be_sent) do |params|
  # Code to inspect requests that will be sent
end

network.on(:response_received) do |params|
  # Code to inspect responses
end

更多信息

您可以使用这些符号来实例化多个 devtool 对象,而不是实例化网络对象,具体取决于您的 chrome devtools 协议 (CDP) 版本:https://www.selenium.dev/selenium/docs/api /rb/Selenium/DevTools/V121.html

console = devtools.send(:console)
console.enable

console.on(:message_added) do |params|
  # Code to inspect console messages
end
animations = devtools.send(:animation)
animations.enable

animations.on(:animation_started) do |params|
  # Code to run when animations start
end

...等等

当我在本地检查代码时,我发现的关于可以注册的特定事件类型的唯一文档是在实际的 selenium-devtools gem 中 - 我找不到任何 rubydocs 也没有在 github 上找到源代码(如果你发现了,请评论链接)

© www.soinside.com 2019 - 2024. All rights reserved.