Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Page#emulate_cpu_throttling #134

Merged
merged 2 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/api_coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
* createPDFStream => `#create_pdf_stream`
* deleteCookie => `#delete_cookie`
* emulate
* ~~emulateCPUThrottling~~
* emulateCPUThrottling => `#emulate_cpu_throttling`
* emulateIdleState => `#emulate_idle_state`
* emulateMediaFeatures => `#emulate_media_features`
* emulateMediaType => `#emulate_media_type`
Expand Down
9 changes: 9 additions & 0 deletions lib/puppeteer/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,15 @@ def emulate_media_type(media_type)
@client.send_message('Emulation.setEmulatedMedia', media: media_type_str)
end

# @param factor [Number|nil] Factor at which the CPU will be throttled (2x, 2.5x. 3x, ...). Passing `nil` disables cpu throttling.
def emulate_cpu_throttling(factor)
if factor.nil? || factor >= 1
@client.send_message('Emulation.setCPUThrottlingRate', rate: factor || 1)
else
raise ArgumentError.new('Throttling rate should be greater or equal to 1')
end
end

# @param features [Array]
def emulate_media_features(features)
if features.nil?
Expand Down
7 changes: 7 additions & 0 deletions spec/integration/emulation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,11 @@
expect { page.emulate_vision_deficiency('invalid') }.to raise_error(/Unsupported vision deficiency: invalid/)
end
end

describe 'Page.emulateCPUThrottling', skip: Puppeteer.env.firefox? do
it 'should change the CPU throttling rate successfully' do
page.emulate_cpu_throttling(100)
page.emulate_cpu_throttling(nil)
end
end
end