titusfortner / webdrivers

Keep your Selenium WebDrivers updated automatically
MIT License
592 stars 113 forks source link

Throttle CPU option #222

Closed Theminijohn closed 2 years ago

Theminijohn commented 2 years ago

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})

How would I do that?

Theminijohn commented 2 years ago

@titusfortner answered this one on SO.

I was close, the solution is:

driver.execute_cdp('Emulation.setCPUThrottlingRate', rate: 10)