Skip to content

Commit

Permalink
Merge pull request #135 from YusukeIwaki/emulateNetworkConditions
Browse files Browse the repository at this point in the history
add Page#emulateNetworkConditions
  • Loading branch information
Yusuke Iwaki committed Aug 16, 2021
2 parents b39c2bb + 4ae54f0 commit 6017dec
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 14 deletions.
4 changes: 2 additions & 2 deletions docs/api_coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* ~~errors~~
* executablePath => `#executable_path`
* launch
* ~~networkConditions~~
* networkConditions => `#network_conditions`
* product
* ~~registerCustomQueryHandler~~
* ~~unregisterCustomQueryHandler~~
Expand Down Expand Up @@ -85,7 +85,7 @@
* emulateIdleState => `#emulate_idle_state`
* emulateMediaFeatures => `#emulate_media_features`
* emulateMediaType => `#emulate_media_type`
* ~~emulateNetworkConditions~~
* emulateNetworkConditions => `#emulate_network_conditions`
* emulateTimezone => `#emulate_timezone`
* emulateVisionDeficiency => `#emulate_vision_deficiency`
* evaluate
Expand Down
2 changes: 1 addition & 1 deletion lib/puppeteer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module Puppeteer; end
require 'puppeteer/env'

# Custom data types.
require 'puppeteer/device'
require 'puppeteer/events'
require 'puppeteer/errors'
require 'puppeteer/geolocation'
Expand Down Expand Up @@ -44,6 +43,7 @@ module Puppeteer; end
require 'puppeteer/launcher'
require 'puppeteer/lifecycle_watcher'
require 'puppeteer/mouse'
require 'puppeteer/network_conditions'
require 'puppeteer/network_manager'
require 'puppeteer/page'
require 'puppeteer/protocol_stream_reader'
Expand Down
2 changes: 2 additions & 0 deletions lib/puppeteer/devices.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require_relative './device'

Puppeteer::DEVICES = Hash[
[
{
Expand Down
12 changes: 12 additions & 0 deletions lib/puppeteer/network_condition.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Puppeteer::NetworkCondition
# @param download [Number] Download speed (bytes/s)
# @param upload [Number] Upload speed (bytes/s)
# @param latency [Number] Latency (ms)
def initialize(download:, upload:, latency:)
@download = download
@upload = upload
@latency = latency
end

attr_reader :download, :upload, :latency
end
24 changes: 24 additions & 0 deletions lib/puppeteer/network_conditions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require_relative './network_condition'

Puppeteer::NETWORK_CONDITIONS = {
'Slow 3G' => Puppeteer::NetworkCondition.new(
download: ((500 * 1000) / 8) * 0.8,
upload: ((500 * 1000) / 8) * 0.8,
latency: 400 * 5,
),
'Fast 3G' => Puppeteer::NetworkCondition.new(
download: ((1.6 * 1000 * 1000) / 8) * 0.9,
upload: ((750 * 1000) / 8) * 0.9,
latency: 150 * 3.75,
),
}

module Puppeteer::NetworkConditions
module_function def slow_3g
Puppeteer::NETWORK_CONDITIONS['Slow 3G']
end

module_function def fast_3g
Puppeteer::NETWORK_CONDITIONS['Fast 3G']
end
end
58 changes: 47 additions & 11 deletions lib/puppeteer/network_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,46 @@ def initialize(username:, password:)
attr_reader :username, :password
end

class InternalNetworkCondition
attr_writer :offline, :upload, :download, :latency

def initialize(client)
@client = client
@offline = false
@upload = -1
@download = -1
@latency = 0
end

def offline_mode=(value)
return if @offline == value
@offline = value
update_network_conditions
end

def network_condition=(network_condition)
if network_condition
@upload = network_condition.upload
@download = network_condition.download
@latency = network_condition.latency
else
@upload = -1
@download = -1
@latency = 0
end
update_network_conditions
end

private def update_network_conditions
@client.send_message('Network.emulateNetworkConditions',
offline: @offline,
latency: @latency,
downloadThroughput: @download,
uploadThroughput: @upload,
)
end
end

# @param {!Puppeteer.CDPSession} client
# @param {boolean} ignoreHTTPSErrors
# @param {!Puppeteer.FrameManager} frameManager
Expand All @@ -29,13 +69,12 @@ def initialize(client, ignore_https_errors, frame_manager)

@extra_http_headers = {}

@offline = false

@attempted_authentications = Set.new
@user_request_interception_enabled = false
@protocol_request_interception_enabled = false
@user_cache_disabled = false
@request_id_to_interception_id = {}
@internal_network_condition = InternalNetworkCondition.new(@client)

@client.on_event('Fetch.requestPaused') do |event|
handle_request_paused(event)
Expand Down Expand Up @@ -94,15 +133,12 @@ def extra_http_headers

# @param value [TrueClass|FalseClass]
def offline_mode=(value)
return if @offline == value
@offline = value
@client.send_message('Network.emulateNetworkConditions',
offline: @offline,
# values of 0 remove any active throttling. crbug.com/456324#c9
latency: 0,
downloadThroughput: -1,
uploadThroughput: -1,
)
@internal_network_condition.offline_mode=(value)
end

# @param network_condition [Puppeteer::NetworkCondition|nil]
def emulate_network_conditions(network_condition)
@internal_network_condition.network_condition = network_condition
end

# @param user_agent [String]
Expand Down
5 changes: 5 additions & 0 deletions lib/puppeteer/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ def offline_mode=(enabled)
@frame_manager.network_manager.offline_mode = enabled
end

# @param network_condition [Puppeteer::NetworkCondition|nil]
def emulate_network_conditions(network_condition)
@frame_manager.network_manager.emulate_network_conditions(network_condition)
end

# @param {number} timeout
def default_navigation_timeout=(timeout)
@timeout_settings.default_navigation_timeout = timeout
Expand Down
5 changes: 5 additions & 0 deletions lib/puppeteer/puppeteer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ def devices
# # ???
# end

# @return [Puppeteer::NetworkConditions]
def network_conditions
Puppeteer::NetworkConditions
end

# @param args [Array<String>]
# @param user_data_dir [String]
# @param devtools [Boolean]
Expand Down
12 changes: 12 additions & 0 deletions spec/integration/emulation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,18 @@
end
end

describe 'Page#emulate_network_conditions', skip: Puppeteer.env.firefox? do
it 'should change navigator.connection.effectiveType' do
expect(page.evaluate('window.navigator.connection.effectiveType')).to eq('4g')
page.emulate_network_conditions(Puppeteer.network_conditions.fast_3g)
expect(page.evaluate('window.navigator.connection.effectiveType')).to eq('3g')
page.emulate_network_conditions(Puppeteer.network_conditions.slow_3g)
expect(page.evaluate('window.navigator.connection.effectiveType')).to eq('2g')
page.emulate_network_conditions(nil)
expect(page.evaluate('window.navigator.connection.effectiveType')).to eq('4g')
end
end

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

0 comments on commit 6017dec

Please sign in to comment.