From 6b7e06595a153dbebaef18045a58c44bc61d0b84 Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Mon, 30 Nov 2026 00:57:23 +0300 Subject: [PATCH] feat!: return Download objects, window_bounds in its writer's shape, add Response cache predicates downloads.files and await_download return Ferrum::Download objects instead of merged CDP event hashes, with path (Chrome's filePath, the guid with allowAndName), state, completed?, canceled?, suggested_filename, byte counts and to_h for the raw params. window_bounds returns symbol keys with window_state:, the shape window_bounds= takes, instead of CDP's string keys and "windowState". The position RBS is fixed to the [left, top] it returns. Network::Response gets from_disk_cache? and from_service_worker?, so callers stop digging into params. --- CHANGELOG.md | 6 ++ docs/21-workers.md | 7 ++ docs/3-navigation.md | 4 +- docs/7-network.md | 5 +- docs/8-downloads.md | 28 +++++-- lib/ferrum/download.rb | 139 ++++++++++++++++++++++++++++++++ lib/ferrum/downloads.rb | 20 ++--- lib/ferrum/network/response.rb | 18 +++++ lib/ferrum/page.rb | 11 +-- sig/ferrum/download.rbs | 35 ++++++++ sig/ferrum/downloads.rbs | 8 +- sig/ferrum/network/response.rbs | 4 + sig/ferrum/page.rbs | 4 +- spec/browser_spec.rb | 2 +- spec/download_spec.rb | 37 +++++++++ spec/downloads_spec.rb | 16 ++-- spec/network/response_spec.rb | 38 +++++++++ spec/network_spec.rb | 12 +-- spec/page_spec.rb | 23 ++++-- spec/support/application.rb | 9 +++ 20 files changed, 373 insertions(+), 53 deletions(-) create mode 100644 lib/ferrum/download.rb create mode 100644 sig/ferrum/download.rbs create mode 100644 spec/download_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 6aa2f228..fe1a8d74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,12 +15,18 @@ element under it, unlike `scroll_by` [#744] - `RemoteObject#evaluate`, `#evaluate_handle` and `#execute` run JavaScript with `this` bound to the handle, as on `Node` [#746] +- `Network::Response#from_disk_cache?` and `#from_service_worker?` tell whether a response came from the disk cache or + a service worker [#751] - `browser.disconnect` closes the connection and leaves the browser running, so `close`, `disconnect` and `process.stop` make up a custom shutdown. `quit` still stops a browser disconnected this way [#750] - `browser.shutdown` asks the browser to exit, lets a launched one do so before killing it, and also shuts down a remote browser, unlike `quit`. `process.stop(grace:)` waits that long before signaling [#750] ### Changed +- `downloads.files` and `await_download` return `Ferrum::Download` objects instead of event hashes, with `path` + where Chrome saved the file, `state`, `completed?`, `suggested_filename` and `to_h` for the raw params [#751] +- `window_bounds` returns the shape `window_bounds=` takes, symbol keys and `window_state:`, instead of CDP's string + keys and `"windowState"` [#751] - A `wss://` connection to the browser verifies its certificate and host name and sends SNI, as the `https` `/json/version` request already did. A self-signed browser needs `:ssl_context`, see the option [#742] - `User-Agent` and `Accept-Language` set through `headers` are only sent with requests, they no longer change what diff --git a/docs/21-workers.md b/docs/21-workers.md index c9d9d388..78ef650c 100644 --- a/docs/21-workers.md +++ b/docs/21-workers.md @@ -44,6 +44,13 @@ target = browser.service_workers.first # => # true +``` + #### attach_target(target_id) : `Boolean` Manually attaches to a target discovered via `service_workers`. Once attached, `target.worker` returns a connected diff --git a/docs/3-navigation.md b/docs/3-navigation.md index 1c2c04eb..fa800a2f 100644 --- a/docs/3-navigation.md +++ b/docs/3-navigation.md @@ -150,12 +150,12 @@ Set window bounds browser.window_bounds = { left: 10, top: 20, width: 1024, height: 768, window_state: "normal" } ``` -#### window_bounds : `Hash` +#### window_bounds : `Hash` Get window bounds ```ruby -browser.window_bounds # => { "left": 0, "top": 1286, "width": 10, "height": 10, "windowState": "normal" } +browser.window_bounds # => { left: 0, top: 1286, width: 10, height: 10, window_state: "normal" } ``` #### window_id : `Integer` diff --git a/docs/7-network.md b/docs/7-network.md index 058a330b..ea987df3 100644 --- a/docs/7-network.md +++ b/docs/7-network.md @@ -247,8 +247,11 @@ Once every option is back to `nil` the override is removed and Chrome sends its #### cache=(enabled) -Turns the browser cache on or off for the page's requests. +Turns the browser cache on or off for the page's requests. `response.from_disk_cache?` tells whether a response came +from the cache: ```ruby page.network.cache = false +page.go_to("https://github.com/") +page.network.response.from_disk_cache? # => false ``` diff --git a/docs/8-downloads.md b/docs/8-downloads.md index 3bd08d0a..d020e24c 100644 --- a/docs/8-downloads.md +++ b/docs/8-downloads.md @@ -6,27 +6,39 @@ sidebar_position: 8 `page.downloads` -#### files `Array` +#### files `Array` -Returns all information about downloaded files as a `Hash`. +Every download the page started, finished or not. ```ruby page.go_to("http://localhost/attachment.pdf") -page.downloads.files # => [{"frameId"=>"E3316DF1B5383D38F8ADF7485005FDE3", "guid"=>"11a68745-98ac-4d54-9b57-9f9016c268b3", "url"=>"http://localhost/attachment.pdf", "suggestedFilename"=>"attachment.pdf", "totalBytes"=>4911, "receivedBytes"=>4911, "state"=>"completed"}] +page.downloads.files # => [#] ``` -#### await_download(timeout: 5) { } : `Hash` | `nil` +#### await_download(timeout: 5) { } : `Ferrum::Download` | `nil` -Runs the block and waits for the download it starts to finish. Returns the download, like an entry of `files`, its -`"state"` is `"completed"` or `"canceled"`. Returns `nil` if it doesn't finish within `timeout`, 5 seconds by -default. `await_download!` raises `Ferrum::TimeoutError` instead. Also on `page`. +Runs the block and waits for the download it starts to finish, `completed?` or `canceled?`. Returns `nil` if it +doesn't finish within `timeout`, 5 seconds by default. `await_download!` raises `Ferrum::TimeoutError` instead. Also on +`page`. ```ruby page.go_to("http://localhost/page") -page.await_download { page.at_css("#download").click } # => {"suggestedFilename"=>"attachment.pdf", "state"=>"completed", ...} +download = page.await_download { page.at_css("#download").click } +download.path # => "/tmp/attachment.pdf" page.await_download! { page.go_to("http://localhost/attachment.pdf") } ``` +#### Ferrum::Download + +* `guid` `String` the download's id +* `url` `String` where the file is downloaded from +* `suggested_filename` `String` the name the server or the link suggested +* `frame_id` `String` the frame that started the download +* `path` `String | nil` where Chrome saved the file once it's completed, named after `guid` with `behavior: :allowAndName` +* `state` `Symbol | nil` `:in_progress`, `:completed` or `:canceled`, with `completed?` and `canceled?` +* `received_bytes`, `total_bytes` `Integer | nil` +* `to_h` `Hash` the raw CDP event params + #### set_behavior(\*\*options) Sets behavior in case of file to be downloaded. diff --git a/lib/ferrum/download.rb b/lib/ferrum/download.rb new file mode 100644 index 00000000..d1c98d25 --- /dev/null +++ b/lib/ferrum/download.rb @@ -0,0 +1,139 @@ +# frozen_string_literal: true + +module Ferrum + # + # A file the page downloads, built from Chrome's `Browser.downloadWillBegin` + # event and updated by every `Browser.downloadProgress` after it. + # + class Download + STATES = { "inProgress" => :in_progress, "completed" => :completed, "canceled" => :canceled }.freeze + + # + # @param [Hash{String => Object}] params + # The `Browser.downloadWillBegin` event params. + # + # @api private + # + def initialize(params) + @params = params + end + + # + # The download's id, unique for the browser. + # + # @return [String] + # + def guid + @params["guid"] + end + + # + # The URL the file is downloaded from. + # + # @return [String] + # + def url + @params["url"] + end + + # + # The file name the server or the link suggested. + # + # @return [String] + # + def suggested_filename + @params["suggestedFilename"] + end + + # + # The id of the frame that started the download. + # + # @return [String] + # + def frame_id + @params["frameId"] + end + + # + # Where the file was saved, as Chrome reports it once the download is + # completed. With `behavior: :allowAndName` it's named after {#guid}. + # + # @return [String, nil] + # + # @example + # page.await_download { page.at_css("#download").click }.path # => "/tmp/downloads/file.pdf" + # + def path + @params["filePath"] + end + + # + # @return [:in_progress, :completed, :canceled, nil] + # `nil` until Chrome reports progress. + # + def state + STATES[@params["state"]] + end + + # + # @return [Boolean] + # + def completed? + state == :completed + end + + # + # @return [Boolean] + # + def canceled? + state == :canceled + end + + # + # Bytes received so far. + # + # @return [Integer, nil] + # + def received_bytes + @params["receivedBytes"] + end + + # + # The file size, `0` when the server doesn't send one. + # + # @return [Integer, nil] + # + def total_bytes + @params["totalBytes"] + end + + # + # The raw event params, merged from all events of this download. + # + # @return [Hash{String => Object}] + # + def to_h + @params.dup + end + + # + # Merges a `Browser.downloadProgress` event into the download. + # + # @param [Hash{String => Object}] params + # + # @return [void] + # + # @api private + # + def update(params) + @params = @params.merge(params) + end + + # + # @return [String] + # + def inspect + "#<#{self.class} @guid=#{guid.inspect} @suggested_filename=#{suggested_filename.inspect} @state=#{state.inspect}>" + end + end +end diff --git a/lib/ferrum/downloads.rb b/lib/ferrum/downloads.rb index 396705ee..86304489 100644 --- a/lib/ferrum/downloads.rb +++ b/lib/ferrum/downloads.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "ferrum/download" + module Ferrum # # Tracks files downloaded by a page. Configures the browser's download @@ -21,9 +23,9 @@ def initialize(page) end # - # Returns information about all downloaded files. + # Every download the page started, finished or not. # - # @return [Array] + # @return [Array] # def files @files.values @@ -37,15 +39,15 @@ def files # # @yield The code that starts the download, e.g. clicking a link. # - # @return [Hash, nil] - # The finished download, like an entry of {#files}, its `"state"` is - # `"completed"` or `"canceled"`. `nil` if it doesn't finish in time. + # @return [Download, nil] + # The finished download, {Download#completed?} or {Download#canceled?}. + # `nil` if it doesn't finish in time. # # @raise [ArgumentError] # If no block is given. # # @example - # page.await_download { page.at_css("#download").click } # => {"suggestedFilename" => "file.pdf", ...} + # page.await_download { page.at_css("#download").click }.path # => "/tmp/downloads/file.pdf" # def await_download(timeout: WAIT_TIMEOUT) raise ArgumentError, "await_download needs a block that starts the download" unless block_given? @@ -66,7 +68,7 @@ def await_download(timeout: WAIT_TIMEOUT) # # @yield The code that starts the download. # - # @return [Hash] + # @return [Download] # # @raise [TimeoutError] # If the download doesn't finish in time. @@ -128,7 +130,7 @@ def subscribe def subscribe_download_will_begin @page.on("Browser.downloadWillBegin") do |params| @idle.clear - @files[params["guid"]] = params + @files[params["guid"]] = Download.new(params) @waiters_mutex.synchronize { @waiters.find { |w| w[:guid].nil? }&.store(:guid, params["guid"]) } end end @@ -141,7 +143,7 @@ def subscribe_download_will_begin # @api private def subscribe_download_progress @page.on("Browser.downloadProgress") do |params| - @files[params["guid"]].merge!(params) + @files[params["guid"]].update(params) case params["state"] when "completed", "canceled" diff --git a/lib/ferrum/network/response.rb b/lib/ferrum/network/response.rb index 36717748..d15020e8 100644 --- a/lib/ferrum/network/response.rb +++ b/lib/ferrum/network/response.rb @@ -114,6 +114,24 @@ def content_type @content_type ||= headers.find { |k, _| k.downcase == "content-type" }&.last&.sub(/;.*\z/, "") end + # + # Whether the response was served from the browser's disk cache. + # + # @return [Boolean] + # + def from_disk_cache? + @response["fromDiskCache"] == true + end + + # + # Whether a service worker answered the request instead of the network. + # + # @return [Boolean] + # + def from_service_worker? + @response["fromServiceWorker"] == true + end + # See https://crbug.com/883475 # Sometimes we never get the Network.responseReceived event. # See https://crbug.com/764946 diff --git a/lib/ferrum/page.rb b/lib/ferrum/page.rb index 47519fdf..1ae021fa 100644 --- a/lib/ferrum/page.rb +++ b/lib/ferrum/page.rb @@ -190,7 +190,7 @@ def javascript? # page.position # => [10, 20] # def position - window_bounds.values_at("left", "top") + window_bounds.values_at(:left, :top) end # @@ -246,17 +246,18 @@ def window_bounds=(bounds) end # - # Current window bounds. + # Current window bounds, in the shape {#window_bounds=} takes. # - # @return [Hash{String => (Integer, String)}] + # @return [Hash{Symbol => (Integer, String)}] # # @raise [TimeoutError] # # @example - # page.window_bounds # => { "left": 0, "top": 1286, "width": 10, "height": 10, "windowState": "normal" } + # page.window_bounds # => { left: 0, top: 1286, width: 10, height: 10, window_state: "normal" } # def window_bounds - command("Browser.getWindowBounds", windowId: window_id).fetch("bounds") + bounds = command("Browser.getWindowBounds", windowId: window_id).fetch("bounds") + bounds.transform_keys { |k| k == "windowState" ? :window_state : k.to_sym } end # diff --git a/sig/ferrum/download.rbs b/sig/ferrum/download.rbs new file mode 100644 index 00000000..90400755 --- /dev/null +++ b/sig/ferrum/download.rbs @@ -0,0 +1,35 @@ +module Ferrum + class Download + STATES: ::Hash[String, :in_progress | :completed | :canceled] + + @params: ::Hash[String, untyped] + + def initialize: (::Hash[String, untyped] params) -> void + + def guid: () -> String + + def url: () -> String + + def suggested_filename: () -> String + + def frame_id: () -> String + + def path: () -> String? + + def state: () -> (:in_progress | :completed | :canceled)? + + def completed?: () -> bool + + def canceled?: () -> bool + + def received_bytes: () -> ::Integer? + + def total_bytes: () -> ::Integer? + + def to_h: () -> ::Hash[String, untyped] + + def update: (::Hash[String, untyped] params) -> void + + def inspect: () -> String + end +end diff --git a/sig/ferrum/downloads.rbs b/sig/ferrum/downloads.rbs index 32778c70..e3647a39 100644 --- a/sig/ferrum/downloads.rbs +++ b/sig/ferrum/downloads.rbs @@ -5,17 +5,17 @@ module Ferrum @page: Page @idle: Concurrency::Cell - @files: ::Hash[String, Hash[String, untyped]] + @files: ::Hash[String, Download] @waiters: ::Array[{ guid: String?, cell: Concurrency::Cell }] @waiters_mutex: ::Mutex def initialize: (Page) -> void - def files: () -> ::Array[Hash[String, untyped]] + def files: () -> ::Array[Download] - def await_download: (?timeout: (::Numeric | Deadline)) { () -> void } -> Hash[String, untyped]? + def await_download: (?timeout: (::Numeric | Deadline)) { () -> void } -> Download? - def await_download!: (?timeout: (::Numeric | Deadline)) { () -> void } -> Hash[String, untyped] + def await_download!: (?timeout: (::Numeric | Deadline)) { () -> void } -> Download def set_behavior: (save_path: String, ?behavior: :deny | :allow | :allowAndName | :default) -> nil diff --git a/sig/ferrum/network/response.rbs b/sig/ferrum/network/response.rbs index 98a347a4..2f074390 100644 --- a/sig/ferrum/network/response.rbs +++ b/sig/ferrum/network/response.rbs @@ -23,6 +23,10 @@ module Ferrum def content_type: () -> untyped + def from_disk_cache?: () -> bool + + def from_service_worker?: () -> bool + def body_size=: (untyped size) -> untyped def body: () -> untyped diff --git a/sig/ferrum/page.rbs b/sig/ferrum/page.rbs index e1ced22e..87bc8624 100644 --- a/sig/ferrum/page.rbs +++ b/sig/ferrum/page.rbs @@ -55,11 +55,11 @@ module Ferrum def activate: () -> nil - def position: () -> Hash[String, ::Integer] + def position: () -> [::Integer, ::Integer] def position=: (Hash[Symbol, ::Integer] bounds) -> Hash[String, untyped] - def window_bounds: () -> Hash[String, (::Integer | String)] + def window_bounds: () -> Hash[Symbol, (::Integer | String)] def window_bounds=: (Hash[Symbol, (::Integer | String)] bounds) -> Hash[String, untyped] diff --git a/spec/browser_spec.rb b/spec/browser_spec.rb index cb6b8f46..d2b4070b 100644 --- a/spec/browser_spec.rb +++ b/spec/browser_spec.rb @@ -1571,7 +1571,7 @@ def closed_by_peer?(socket) it "returns the window bounds of the page" do browser.window_bounds = { width: 800, height: 600 } - expect(browser.window_bounds).to include("width" => 800, "height" => 600) + expect(browser.window_bounds).to include(width: 800, height: 600) end end diff --git a/spec/download_spec.rb b/spec/download_spec.rb new file mode 100644 index 00000000..7aa6e5af --- /dev/null +++ b/spec/download_spec.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +describe Ferrum::Download do + let(:save_path) { "/tmp/ferrum-downloads" } + + after { FileUtils.rm_rf(save_path) } + + describe "#path" do + it "returns where the file was saved under its suggested name" do + download = download_with(:allow) + + expect(download.path).to eq("#{save_path}/attachment.pdf") + end + + it "returns the file named after the guid with allowAndName" do + download = download_with(:allowAndName) + + expect(download.path).to eq("#{save_path}/#{download.guid}") + expect(File.exist?(download.path)).to be(true) + end + + def download_with(behavior) + page.downloads.set_behavior(save_path: save_path, behavior: behavior) + page.await_download! { page.go_to("/attachment.pdf") } + end + end + + describe "#to_h" do + it "returns the params of every event of the download" do + page.downloads.set_behavior(save_path: save_path) + + download = page.await_download! { page.go_to("/attachment.pdf") } + + expect(download.to_h).to include("guid" => download.guid, "state" => "completed", "receivedBytes" => 4911) + end + end +end diff --git a/spec/downloads_spec.rb b/spec/downloads_spec.rb index ae09cbf1..4e0305f0 100644 --- a/spec/downloads_spec.rb +++ b/spec/downloads_spec.rb @@ -10,11 +10,9 @@ page.await_download { page.go_to("/#{filename}") } expect(page.downloads.files.size).to eq(1) - expect(page.downloads.files[0]).to include("frameId" => anything, - "guid" => anything, - "suggestedFilename" => "attachment.pdf", - "totalBytes" => 4911, - "url" => anything) + expect(page.downloads.files[0]).to have_attributes(suggested_filename: "attachment.pdf", + total_bytes: 4911, + url: base_url("/attachment.pdf")) ensure FileUtils.rm_rf(save_path) end @@ -26,8 +24,10 @@ download = page.await_download { page.go_to("/#{filename}") } - expect(download).to include("suggestedFilename" => filename, "state" => "completed") - expect(File.exist?("#{save_path}/#{filename}")).to be true + expect(download).to have_attributes(suggested_filename: filename, state: :completed) + expect(download).to be_completed + expect(download.path).to eq("#{save_path}/#{filename}") + expect(File.exist?(download.path)).to be true ensure FileUtils.rm_rf(save_path) end @@ -50,7 +50,7 @@ it "returns the download the block starts" do page.downloads.set_behavior(save_path: save_path) - expect(page.await_download! { page.go_to("/#{filename}") }).to include("state" => "completed") + expect(page.await_download! { page.go_to("/#{filename}") }).to be_completed ensure FileUtils.rm_rf(save_path) end diff --git a/spec/network/response_spec.rb b/spec/network/response_spec.rb index 80cc2eee..c455accd 100644 --- a/spec/network/response_spec.rb +++ b/spec/network/response_spec.rb @@ -110,6 +110,44 @@ end end + describe "#from_disk_cache?" do + it "returns true for a response served from the disk cache" do + page.go_to("/cacheable") + page.at_xpath("//a").click + + expect(last_exchange.response.from_disk_cache?).to be(true) + end + + it "returns false for a response loaded from the network" do + page.network.clear_cache + page.go_to("/cacheable") + + expect(last_exchange.response.from_disk_cache?).to be(false) + end + end + + describe "#from_service_worker?" do + it "returns true for a response a service worker answered" do + page.go_to + page.execute(<<~JS, timeout: 5) + await navigator.serviceWorker.register("/fetch_sw.js"); + await navigator.serviceWorker.ready; + while (!navigator.serviceWorker.controller) await new Promise((r) => setTimeout(r, 50)); + JS + + page.evaluate(%(await (await fetch("/from_service_worker")).text()), timeout: 5) + + expect(last_exchange.response.url).to eq(base_url("/from_service_worker")) + expect(last_exchange.response.from_service_worker?).to be(true) + end + + it "returns false for a response loaded from the network" do + page.go_to + + expect(last_exchange.response.from_service_worker?).to be(false) + end + end + describe "#body!" do it "gets response body" do page.go_to("/with_js") diff --git a/spec/network_spec.rb b/spec/network_spec.rb index 37a4bcfc..2cdb94a8 100644 --- a/spec/network_spec.rb +++ b/spec/network_spec.rb @@ -232,18 +232,18 @@ page.go_to("/cacheable") expect(traffic.length).to eq(1) expect(network.status).to eq(200) - expect(last_exchange.response.params.dig("response", "fromDiskCache")).to be_falsey + expect(last_exchange.response.from_disk_cache?).to be(false) page.at_xpath("//a").click expect(traffic.length).to eq(2) expect(network.status).to eq(200) - expect(last_exchange.response.params.dig("response", "fromDiskCache")).to be_truthy + expect(last_exchange.response.from_disk_cache?).to be(true) page.network.clear_cache page.at_xpath("//a").click expect(traffic.length).to eq(3) expect(network.status).to eq(200) - expect(last_exchange.response.params.dig("response", "fromDiskCache")).to be_falsey + expect(last_exchange.response.from_disk_cache?).to be(false) end end @@ -737,14 +737,14 @@ it "loads resources from the network when the cache is off" do network.cache = false - expect(cached_on_second_visit?).to be_falsey + expect(cached_on_second_visit?).to be(false) end it "loads resources from the cache when the cache is back on" do network.cache = false network.cache = true - expect(cached_on_second_visit?).to be_truthy + expect(cached_on_second_visit?).to be(true) end end @@ -752,6 +752,6 @@ def cached_on_second_visit? page.go_to("/cacheable") page.at_xpath("//a").click expect(traffic.length).to eq(2) - last_exchange.response.params.dig("response", "fromDiskCache") + last_exchange.response.from_disk_cache? end end diff --git a/spec/page_spec.rb b/spec/page_spec.rb index c3a662ba..8944ebae 100644 --- a/spec/page_spec.rb +++ b/spec/page_spec.rb @@ -71,7 +71,7 @@ it "resizes the window" do page.window_bounds = { width: 800, height: 600 } - expect(page.window_bounds).to include("width" => 800, "height" => 600) + expect(page.window_bounds).to include(width: 800, height: 600) end it "changes the window state" do @@ -79,20 +79,29 @@ page.window_bounds = { window_state: "fullscreen" } - expect(page.window_bounds["windowState"]).to eq("fullscreen") + expect(page.window_bounds[:window_state]).to eq("fullscreen") end end describe "#window_bounds" do it "returns the bounds and state of the window" do expect(page.window_bounds).to include( - "left" => an_instance_of(Integer), - "top" => an_instance_of(Integer), - "width" => an_instance_of(Integer), - "height" => an_instance_of(Integer), - "windowState" => "normal" + left: an_instance_of(Integer), + top: an_instance_of(Integer), + width: an_instance_of(Integer), + height: an_instance_of(Integer), + window_state: "normal" ) end + + it "returns bounds the writer takes back" do + page.window_bounds = { left: 30, top: 40, width: 800, height: 600 } + bounds = page.window_bounds + + page.window_bounds = bounds + + expect(page.window_bounds).to eq(bounds) + end end describe "#window_id" do diff --git a/spec/support/application.rb b/spec/support/application.rb index 19cd33a3..eaf07b3a 100644 --- a/spec/support/application.rb +++ b/spec/support/application.rb @@ -205,6 +205,15 @@ def authorized?(login, password) "self.addEventListener('activate', () => self.clients.claim());" end + get "/fetch_sw.js" do + content_type :js + "self.addEventListener('install', () => self.skipWaiting());" \ + "self.addEventListener('activate', (e) => e.waitUntil(self.clients.claim()));" \ + "self.addEventListener('fetch', (e) => { " \ + "if (new URL(e.request.url).pathname === '/from_service_worker') e.respondWith(new Response('from worker'));" \ + "});" + end + get "/:view" do |view| render_view view end