Skip to content

Commit

Permalink
Merge pull request #134 from YusukeIwaki/emulateCPUThrottling
Browse files Browse the repository at this point in the history
Implement Page#emulate_cpu_throttling
  • Loading branch information
Yusuke Iwaki committed Aug 16, 2021
2 parents 7a0a3d7 + baf5479 commit b39c2bb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
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

0 comments on commit b39c2bb

Please sign in to comment.