Skip to content

Commit

Permalink
Switch browser to offline mode
Browse files Browse the repository at this point in the history
  • Loading branch information
route committed May 8, 2022
1 parent 25712d1 commit 7f4c5bf
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ a block with this page, after which the page is closed.
- Windows support
- Show warning and accept dialog if no handler given
- `Ferrum::Cookies#set` ability to set cookie using `Ferrum::Cookies::Cookie` object
- `Ferrum::Network#emulate_network_conditions` activates emulation of network conditions
- `Ferrum::Network#offline_mode` puts browser into offline mode

### Changed

Expand Down
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -561,13 +561,41 @@ end
browser.network.authorize(user: "login", password: "pass", type: :proxy)

browser.go_to("https://google.com")

```

You used to call `authorize` method without block, but since it's implemented using request interception there could be
a collision with another part of your code that also uses request interception, so that authorize allows the request
while your code denies but it's too late. The block is mandatory now.

#### emulate_network_conditions(\*\*options)

Activates emulation of network conditions.

* options `Hash`
* :offline `Boolean` emulate internet disconnection, `false` by default
* :latency `Integer` minimum latency from request sent to response headers received (ms), `0` by
default
* :download_throughput `Integer` maximal aggregated download throughput (bytes/sec), `-1`
by default, disables download throttling
* :upload_throughput `Integer` maximal aggregated upload throughput (bytes/sec), `-1`
by default, disables download throttling
* :connection_type `String` connection type if known, one of: none, cellular2g, cellular3g, cellular4g,
bluetooth, ethernet, wifi, wimax, other. `nil` by default

```ruby
browser.network.emulate_network_conditions(connection_type: "cellular2g")
browser.go_to("https://github.com/")
```

#### offline_mode

Activates offline mode for a page.

```ruby
browser.network.offline_mode
browser.go_to("https://github.com/") # => Ferrum::StatusError (Request to https://github.com/ failed to reach server, check DNS and server status)
```


## Proxy

Expand Down
20 changes: 20 additions & 0 deletions lib/ferrum/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Network
AUTHORIZE_BLOCK_MISSING = "Block is missing, call `authorize(...) { |r| r.continue } "\
"or subscribe to `on(:request)` events before calling it"
AUTHORIZE_TYPE_WRONG = ":type should be in #{AUTHORIZE_TYPE}"
ALLOWED_CONNECTION_TYPE = %w[none cellular2g cellular3g cellular4g bluetooth ethernet wifi wimax other].freeze

attr_reader :traffic

Expand Down Expand Up @@ -150,6 +151,25 @@ def build_exchange(id)
Network::Exchange.new(@page, id).tap { |e| @traffic << e }
end

def emulate_network_conditions(offline: false, latency: 0, download_throughput: -1, upload_throughput: -1, connection_type: nil)
params = {
offline: offline, latency: latency,
downloadThroughput: download_throughput,
uploadThroughput: upload_throughput
}

if connection_type && ALLOWED_CONNECTION_TYPE.include?(connection_type)
params[:connectionType] = connection_type
end

@page.command("Network.emulateNetworkConditions", params)
true
end

def offline_mode
emulate_network_conditions(offline: true, latency: 0, download_throughput: 0, upload_throughput: 0)
end

private

def subscribe_request_will_be_sent
Expand Down
20 changes: 20 additions & 0 deletions spec/network_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -393,5 +393,25 @@ module Ferrum
expect(page.body).to include("Authorized POST request")
end
end

it "#emulate_network_conditions", skip: "doesn't work for now" do
page.network.emulate_network_conditions(latency: 500)

start = Utils::ElapsedTime.monotonic_time
page.go_to("/ferrum/with_js")

expect(Utils::ElapsedTime.elapsed_time(start)).to eq(2000)
end

it "#offline_mode" do
page.network.offline_mode

expect { page.go_to("/ferrum/with_js") }.to raise_error(
Ferrum::StatusError,
%r{Request to http://.*/ferrum/with_js failed to reach server, check DNS and server status}
)

expect(page.body).to eq("<html><head></head><body></body></html>")
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
base_url = Ferrum::Server.server.base_url
options = { base_url: base_url }
options.merge!(headless: false) if ENV["HEADLESS"] == "false"
options.merge!(slowmo: ENV["SLOWMO"].to_f) if ENV["SLOWMO"].to_f > 0

if ENV["CI"]
ferrum_logger = StringIO.new
Expand Down

0 comments on commit 7f4c5bf

Please sign in to comment.