So I'm trying to add the CPUThrottlingRate
in my selenium
chromedriver
setup below.
require 'webdrivers/chromedriver'
module ChromeBrowser
class << self
def headless(throttling)
@browser ||= headless_chrome_browser(throttling || 'regular')
end
private
def headless_chrome_browser(throttling)
options = Selenium::WebDriver::Chrome::Options.new
# -> https://peter.sh/experiments/chromium-command-line-switches/#profiler-timing
options.add_argument '--ignore-certificate-errors'
# options.add_argument "--user-agent=#{random_user_agent}" # set Random User Agent
options.add_argument '--allow-insecure-localhost'
options.add_argument '--window-size=1400x1400'
options.add_argument '--disable-gpu'
options.add_argument '--disable-dev-shm-usage'
options.add_argument '--noerrdialogs' # Suppresses all error dialogs when present
options.add_argument '--profiler-timing=0' # whether chrome will contain timing information
options.add_argument '--disable-infobars' # prevent infobars from appearing
options.add_argument '--headless' if Rails.env.production? # headless on chrome
# options.add_argument '--blink-settings=imagesEnabled=false' # disable images
options.add_argument '--no-referrers' # don't send HTTP-Referer headers
options.add_argument '--disable-breakpad' # disables the crash reporting
options.add_argument '--disable-demo-model' # disables the chrome OS demo
options.add_argument '--disable-translate' # disable google translate
options.add_argument '--dns-prefetch-disable' # disable DNS prefetching
options.add_argument '--no-pings' # no hyperlink auditing pings
browser = Selenium::WebDriver.for :chrome, options: options
case throttling
when 'slow'
browser.network_conditions = {
offline: false,
latency: 50,
download_throughput: 51200,
upload_throughput: 51200
}
when 'regular'
browser.network_conditions = {
offline: false,
latency: 30,
download_throughput: 51200,
upload_throughput: 512000
}
when 'fast'
browser.network_conditions = {
offline: false,
latency: 20,
download_throughput: 1024000,
upload_throughput: 1024000
}
end
return browser
end
end
end
I'm looking for this solution here but in ruby.
## rate 1 is no throttle, 2 is 2x slower, etc.
driver.execute_cdp_cmd("Emulation.setCPUThrottlingRate", {'rate': 10})
developertools
is installedrunning on chromedriver v92
I'm trying to throttle both network speed and CPU to simulate ~kinda~ real-life requests.
Can't seem to figure it out, what am I missing? Useful links:
- https://www.selenium.dev/documentation/support_packages/chrome_devtools/
- CPU throttling in chrome via python selenium
- https://github.com/SeleniumHQ/selenium/wiki/Ruby-Bindings
I have tried:
...
browser = Selenium::WebDriver.for :chrome, options: options
devToolsSession = browser.devtools
devToolsSession.send_cmd('Emulation.setCPUThrottlingRate', {rate: 10})
Which produces
ArgumentError: wrong number of arguments (given 2, expected 1)
from /Users/minijohn/.asdf/installs/ruby/3.0.2/lib/ruby/gems/3.0.0/gems/selenium-webdriver-4.0.3/lib/selenium/webdriver/devtools.rb:55:in `send_cmd'
and with one param
Selenium::WebDriver::Error::WebDriverError: -32602: Invalid parameters: Failed to deserialize params.rate - BINDINGS: mandatory field missing at position 7
from /Users/minijohn/.asdf/installs/ruby/3.0.2/lib/ruby/gems/3.0.0/gems/selenium-webdriver-4.0.3/lib/selenium/webdriver/devtools.rb:69:in `send_cmd'
Also tried this
browser = Selenium::WebDriver.for :chrome, options: options
browser.send_cmd('Emulation.setCPUThrottlingRate', {rate: 10})
which produces
NoMethodError: undefined method `send_cmd' for #<Selenium::WebDriver::Chrome::Driver:0x2738339e1fe77884 browser=:chrome>
from (pry):14:in `headless_chrome_browser'
CodePudding user response:
The goal for Selenium is to define the common commands that the browser vendors will support in the WebDriver BiDi specification, and support them via a straightforward API, which will be accessible via Driver#devtools
method.
In the meantime, any Chrome DevTools command can be executed via Driver#execute_cdp
.
In this case it will look like:
SL-1495:code titusfortner$ irb
irb(main):001:0> require 'selenium-webdriver'
=> true
irb(main):002:0> driver = Selenium::WebDriver.for :chrome
=> #<Selenium::WebDriver::Chrome::Driver:0x..fc42365c4d30079b0 browser=:chrome>
irb(main):003:0> driver.execute_cdp('Emulation.setCPUThrottlingRate', rate: 10)
=> {}
irb(main):004:0>
The key here is that in Ruby we're using keywords for this instead of a Hash, and in Ruby 3, those are now handled differently.