From 3dbc7ebd1d87bc4320080c7335ce225abd734661 Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Fri, 20 Nov 2026 16:31:50 +0300 Subject: [PATCH 1/3] feat(context): add wait_for_new_page wait_for_new_page runs a block and returns the page it opens, in a tab or a window, with opener: only the pages opened by that page. The context matches the opener id of each new page target against the waiting calls, so neither pages nor targets need to know each other. --- CHANGELOG.md | 3 ++ docs/3-navigation.md | 28 ++++++++++ lib/ferrum/browser.rb | 4 +- lib/ferrum/context.rb | 59 +++++++++++++++++++++ sig/ferrum/context.rbs | 10 ++++ spec/context_spec.rb | 116 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 218 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aaec170d..244d8a33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ - `context.target(id)` returns the context's target with that id - `timeout:` on `keyboard.type`, `node.type` and `mouse.move` bounds all the keys or steps together, the page `timeout` by default, raise it for a long text +- `context.wait_for_new_page { ... }`, and `browser.wait_for_new_page` for the default context, runs the block and + returns the page it opens, in a tab or a window, or `nil` if none opens in time. `wait_for_new_page!` raises + `Ferrum::TimeoutError` instead. Pass `opener:` to only get the pages opened by one page - `go_to(url, referrer:)` sets the referrer of that navigation only, the page's other requests don't carry it - `cookies.set` accepts `http_only:` and `same_site:` next to `httponly:` and `samesite:`, like the cookie's readers - `cookies[name, domain:, path:]` picks one of the cookies sharing a name diff --git a/docs/3-navigation.md b/docs/3-navigation.md index 1e1a62dd..ac610f86 100644 --- a/docs/3-navigation.md +++ b/docs/3-navigation.md @@ -145,3 +145,31 @@ Current window id ```ruby browser.window_id # => 1 ``` + +#### wait_for_new_page(\*\*options) { } : `Page` | `nil` + +Runs the block and returns the page it opens, in a tab or a window, e.g. through `window.open` or a link with +`target="_blank"`. Returns `nil` if no page opens in time. + +* options `Hash` + * :opener `Page` only return a page opened by this page, by default a page opened by any page of the context + * :timeout `Numeric` how long the block and the new page may take together, the browser's `timeout` by default + +```ruby +opened = browser.wait_for_new_page { browser.at_css("a[target=_blank]").click } +opened.url # => "https://example.com/" +opened.close + +page = browser.create_page +opened = browser.wait_for_new_page(opener: page) { page.at_css("a[target=_blank]").click } +``` + +#### wait_for_new_page!(\*\*options) { } : `Page` + +Same as `wait_for_new_page`, but raises `Ferrum::TimeoutError` if no page opens in time. + +The target of an opened page has the id of the page that opened it: + +```ruby +browser.targets[opened.target_id].opener_id == page.target_id # => true +``` diff --git a/lib/ferrum/browser.rb b/lib/ferrum/browser.rb index 6d62b018..872e10b7 100644 --- a/lib/ferrum/browser.rb +++ b/lib/ferrum/browser.rb @@ -27,8 +27,8 @@ class Browser extend Forwardable delegate %i[default_context] => :contexts - delegate %i[targets create_target page pages windows workers service_workers attach_target - cookies] => :default_context + delegate %i[targets create_target page pages windows wait_for_new_page wait_for_new_page! workers service_workers + attach_target cookies] => :default_context delegate %i[go_to goto go back forward refresh reload stop wait_for_reload wait_for_navigation wait_for_navigation! at_css at_xpath css xpath wait_for_selector current_url current_title url title diff --git a/lib/ferrum/context.rb b/lib/ferrum/context.rb index 902b78a8..6f72fcf9 100644 --- a/lib/ferrum/context.rb +++ b/lib/ferrum/context.rb @@ -21,6 +21,8 @@ def initialize(client, contexts, id, implicit: false) @client = client @contexts = contexts @targets = Concurrency::AwaitableMap.new + @page_waiters = [] + @page_waiters_mutex = Mutex.new end # Whether this is the browser's implicit context, the one it puts its @@ -105,6 +107,52 @@ def windows(pos = nil, size = 1) windows.map(&:page) end + # Runs the block and returns the page it opens, in a tab or a window, + # e.g. through `window.open` or a link with `target="_blank"`. + # + # @param [Page, nil] opener + # Only return a page opened by this page. By default a page opened by + # any page of the context is returned. + # + # @param [Numeric, Deadline] timeout + # How long the block and the new page may take together. Defaults to + # the browser's `timeout`. + # + # @yield The code that opens the page. + # + # @return [Page, nil] + # The first page opened since the call, `nil` if none opens in time. + # + # @example + # opened = browser.wait_for_new_page { browser.at_css("a[target=_blank]").click } + # opened.url # => "https://example.com/" + def wait_for_new_page(opener: nil, timeout: @client.timeout) + deadline = Deadline.wrap(timeout) + waiter = [opener&.target_id, Concurrency::Cell.new] + @page_waiters_mutex.synchronize { @page_waiters << waiter } + yield if block_given? + return unless waiter.last.wait(deadline) + + waiter.last.value.page + ensure + @page_waiters_mutex.synchronize { @page_waiters.delete(waiter) } + end + + # Same as {#wait_for_new_page}, but raises if no page opens in time. + # + # @param [Hash] options + # The options of {#wait_for_new_page}. + # + # @yield The code that opens the page. + # + # @return [Page] + # + # @raise [TimeoutError] + # If no page opens in time. + def wait_for_new_page!(**options, &) + wait_for_new_page(**options, &) || raise(TimeoutError) + end + # Creates a new target in this context and eagerly connects to it as a # {Page}, forwarding `options` to {Target#build_page}. # @@ -153,6 +201,8 @@ def add_target(params:, session_id: nil, attached: false) target.session_id = session_id if session_id && (attached || target.session_id.nil?) @default_target ||= target @targets[target.id] = target + announce_page(target) if target.equal?(new_target) + target end # Updates a known target's params, e.g. on `Target.targetInfoChanged`. @@ -255,5 +305,14 @@ def target?(target_id) def inspect %(#<#{self.class} @id=#{@id.inspect} @targets=#{@targets.inspect} @default_target=#{@default_target.inspect}>) end + + private + + def announce_page(target) + return unless target.page? && target.opener_id + + waiters = @page_waiters_mutex.synchronize { @page_waiters.select { |id, _| id.nil? || id == target.opener_id } } + waiters.each { |w| w.last.set(target) } + end end end diff --git a/sig/ferrum/context.rbs b/sig/ferrum/context.rbs index e3a61362..dbd62368 100644 --- a/sig/ferrum/context.rbs +++ b/sig/ferrum/context.rbs @@ -11,6 +11,8 @@ module Ferrum @client: Client @contexts: Contexts @targets: Concurrency::AwaitableMap[String, Target] + @page_waiters: ::Array[[String?, Concurrency::Cell]] + @page_waiters_mutex: ::Mutex @default_target: Target? @cookies: Cookies @@ -32,6 +34,10 @@ module Ferrum def windows: (?(:first | :last)?, ?::Integer) -> ::Array[Page] + def wait_for_new_page: (?opener: Page?, ?timeout: (::Numeric | Deadline)) ?{ () -> void } -> Page? + + def wait_for_new_page!: (?opener: Page?, ?timeout: (::Numeric | Deadline)) ?{ () -> void } -> Page + def create_page: (**untyped options) -> Page def create_target: () -> Target @@ -55,5 +61,9 @@ module Ferrum def target?: (String target_id) -> bool def inspect: () -> ::String + + private + + def announce_page: (Target target) -> void end end diff --git a/spec/context_spec.rb b/spec/context_spec.rb index b55d9e1d..93cdaf12 100644 --- a/spec/context_spec.rb +++ b/spec/context_spec.rb @@ -184,6 +184,122 @@ end end + describe "#wait_for_new_page" do + it "returns the page opened with window.open" do + browser.go_to + + popup = browser.wait_for_new_page { browser.execute(%(window.open("/simple"))) } + + expect(popup.body).to include("Test") + expect(browser.pages).to include(popup) + ensure + popup&.close + end + + it "returns the page opened by a link with a blank target" do + browser.go_to + browser.content = %(open) + + popup = browser.wait_for_new_page { browser.at_css("a").click } + + expect(popup.url).to eq(base_url("/simple")) + ensure + popup&.close + end + + it "waits for the page to load" do + browser.go_to + + popup = browser.wait_for_new_page { browser.execute(%(window.open("/slow", "popup"))) } + + expect(popup.body).to include("slow page") + ensure + popup&.close + end + + it "returns the page opened under the name of a closed one" do + browser.go_to + browser.wait_for_new_page { browser.execute(%(window.open("/simple", "popup"))) }.close + sleep 0.5 # https://github.com/ChromeDevTools/devtools-protocol/issues/145 + + same = browser.wait_for_new_page { browser.execute(%(window.open("/simple", "popup"))) } + + expect(same.body).to include("Test") + ensure + same&.close + end + + it "returns the page opened by the given opener" do + browser.go_to + other = browser.create_page + other.go_to("/") + + popup = browser.wait_for_new_page(opener: other) { other.execute(%(window.open("/simple"))) } + + expect(browser.targets[popup.target_id].opener_id).to eq(other.target_id) + ensure + popup&.close + other&.close + end + + it "leaves out the pages opened by other pages than the given opener" do + browser.go_to + other = browser.create_page + + popup = browser.wait_for_new_page(opener: other, timeout: 0.5) { browser.execute(%(window.open("/simple"))) } + + expect(popup).to be_nil + ensure + browser.targets.each_value { |t| t.page.close if t.opener_id } + other&.close + end + + it "leaves out the pages opened in other contexts" do + browser.go_to + context = browser.contexts.create + + popup = context.wait_for_new_page(timeout: 0.5) { browser.execute(%(window.open("/simple"))) } + + expect(popup).to be_nil + ensure + browser.targets.each_value { |t| t.page.close if t.opener_id } + context&.dispose + end + + it "returns nil when no page opens in time" do + expect(browser.wait_for_new_page(timeout: 0.1)).to be_nil + end + end + + describe "#wait_for_new_page!" do + it "returns the page opened with window.open" do + browser.go_to + + popup = browser.wait_for_new_page! { browser.execute(%(window.open("/simple"))) } + + expect(popup.body).to include("Test") + ensure + popup&.close + end + + it "returns the page opened by the given opener" do + browser.go_to + other = browser.create_page + other.go_to("/") + + popup = browser.wait_for_new_page!(opener: other) { other.execute(%(window.open("/simple"))) } + + expect(browser.targets[popup.target_id].opener_id).to eq(other.target_id) + ensure + popup&.close + other&.close + end + + it "raises when no page opens in time" do + expect { browser.wait_for_new_page!(timeout: 0.1) }.to raise_error(Ferrum::TimeoutError) + end + end + describe "#create_page" do it "creates a connected page in the context" do context = browser.contexts.create From 0393a44175d9d2d291fd1568c2b4b45030f9d1c1 Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Fri, 20 Nov 2026 19:37:24 +0300 Subject: [PATCH 2/3] feat(context): accept a block in pages to select targets --- CHANGELOG.md | 2 ++ docs/3-navigation.md | 10 ++++++++++ lib/ferrum/context.rb | 15 +++++++++++++-- sig/ferrum/context.rbs | 2 +- spec/context_spec.rb | 9 +++++++++ 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 244d8a33..208506ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ - `context.wait_for_new_page { ... }`, and `browser.wait_for_new_page` for the default context, runs the block and returns the page it opens, in a tab or a window, or `nil` if none opens in time. `wait_for_new_page!` raises `Ferrum::TimeoutError` instead. Pass `opener:` to only get the pages opened by one page +- `pages` of a context and of the browser takes a block that selects targets, e.g. `pages(&:opener_id)` for + the pages opened by another page - `go_to(url, referrer:)` sets the referrer of that navigation only, the page's other requests don't carry it - `cookies.set` accepts `http_only:` and `same_site:` next to `httponly:` and `samesite:`, like the cookie's readers - `cookies[name, domain:, path:]` picks one of the cookies sharing a name diff --git a/docs/3-navigation.md b/docs/3-navigation.md index ac610f86..d00ebfec 100644 --- a/docs/3-navigation.md +++ b/docs/3-navigation.md @@ -173,3 +173,13 @@ The target of an opened page has the id of the page that opened it: ```ruby browser.targets[opened.target_id].opener_id == page.target_id # => true ``` + +#### pages { |target| } : `Array` + +Returns the pages of the default context. With a block, only the pages of the targets it returns truthy for. + +```ruby +browser.pages # => [#, #] +browser.pages(&:opener_id) # the pages opened by another page +browser.pages { |t| t.opener_id == page.target_id } # the pages opened by `page` +``` diff --git a/lib/ferrum/context.rb b/lib/ferrum/context.rb index 6f72fcf9..572ea50e 100644 --- a/lib/ferrum/context.rb +++ b/lib/ferrum/context.rb @@ -68,11 +68,22 @@ def targets # All page targets in this context, connected to as {Page}s. # + # @yield [target] + # Only the pages of the targets the block returns truthy for are + # connected to and returned. + # + # @yieldparam [Target] target + # # @return [Array] # # @raise [TimeoutError] - def pages - @targets.values.select(&:page?).map(&:page) + # + # @example The pages opened by another page + # browser.pages(&:opener_id) + def pages(&) + targets = @targets.values.select(&:page?) + targets = targets.select(&) if block_given? + targets.map(&:page) end # Dedicated and shared workers spawned by any page in this context, diff --git a/sig/ferrum/context.rbs b/sig/ferrum/context.rbs index dbd62368..a3d4d9dd 100644 --- a/sig/ferrum/context.rbs +++ b/sig/ferrum/context.rbs @@ -26,7 +26,7 @@ module Ferrum def page: () -> Page - def pages: () -> ::Array[Page] + def pages: () ?{ (Target) -> boolish } -> ::Array[Page] def workers: () -> ::Array[Worker] diff --git a/spec/context_spec.rb b/spec/context_spec.rb index 93cdaf12..06f55247 100644 --- a/spec/context_spec.rb +++ b/spec/context_spec.rb @@ -65,6 +65,15 @@ ensure pages&.each(&:close) end + + it "returns only the pages of the targets the block selects" do + browser.go_to + popup = browser.wait_for_new_page! { browser.execute(%(window.open("/simple"))) } + + expect(browser.pages(&:opener_id)).to eq([popup]) + ensure + popup&.close + end end describe "#workers" do From a92bfc9a66afb0b90a76d03eb6cc9d498668462e Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Fri, 20 Nov 2026 16:09:36 +0300 Subject: [PATCH 3/3] ref(context): deprecate windows and target.window? windows only listed the pages another page opened and was used to pick the one just opened by its position. wait_for_new_page returns that page directly, so windows warns now and goes away in 0.22. Target#window? goes with it, it called a page with an opener a window, opener_id says the same. --- CHANGELOG.md | 3 +++ lib/ferrum/context.rb | 23 ++++++++++++++++++----- lib/ferrum/target.rb | 10 +++++++--- sig/ferrum/context.rbs | 3 ++- sig/ferrum/target.rbs | 3 ++- spec/browser_spec.rb | 31 ++++++++----------------------- spec/context_spec.rb | 42 ++++++++++++------------------------------ spec/dialog_spec.rb | 12 ++---------- spec/target_spec.rb | 15 +++++++++++---- 9 files changed, 65 insertions(+), 77 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 208506ba..0dd75feb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,9 @@ - `on` with a raw CDP event yields only the params, not the callback's index and the total number of callbacks ### Deprecated +- `target.window?`, will be removed in 0.22. Use `target.opener_id`, it is set for a target another one opened +- `windows` of a context and of the browser, will be removed in 0.22. Use `wait_for_new_page { ... }` for the page a click + opens, or `pages(&:opener_id)` for all of them - `cookies.all`, will be removed in 0.22. Keyed by name, it kept one of the cookies sharing a name, use `to_a`, or `map(&:name)` for the names - `current_url` and `current_title` on pages and frames, will be removed in 0.22. Use `url` and `title`, or diff --git a/lib/ferrum/context.rb b/lib/ferrum/context.rb index 572ea50e..0bb23202 100644 --- a/lib/ferrum/context.rb +++ b/lib/ferrum/context.rb @@ -12,6 +12,8 @@ module Ferrum # class Context POSITION = %i[first last].freeze + WINDOWS_DEPRECATED = "windows will be removed in Ferrum 0.22. Use wait_for_new_page { ... } for the page a click " \ + "opens, or pages(&:opener_id) for all of them." attr_reader :id @@ -106,14 +108,25 @@ def service_workers @targets.values.select(&:service_worker?) end - # When we call `page` method on target it triggers ruby to connect to given - # page by WebSocket, if there are many opened windows, but we need only one - # it makes more sense to get and connect to the needed one only which - # usually is the last one. + # The pages of this context opened by another page. + # + # @deprecated Will be removed in Ferrum 0.22. Use {#wait_for_new_page} for the page a click opens, or + # `pages(&:opener_id)` for all of them. + # + # @param [:first, :last, nil] pos + # + # @param [Integer] size + # + # @return [Array] + # + # @raise [ArgumentError] + # + # @raise [TimeoutError] def windows(pos = nil, size = 1) + Utils::Deprecate.warn(WINDOWS_DEPRECATED, user_code: true) raise ArgumentError if pos && !POSITION.include?(pos) - windows = @targets.values.select(&:window?) + windows = @targets.values.select(&:opener_id) windows = windows.send(pos, size) if pos windows.map(&:page) end diff --git a/lib/ferrum/target.rb b/lib/ferrum/target.rb index aa483e2f..f452d3d3 100644 --- a/lib/ferrum/target.rb +++ b/lib/ferrum/target.rb @@ -9,6 +9,8 @@ module Ferrum # class Target NEW_WINDOW_WAIT = ENV.fetch("FERRUM_NEW_WINDOW_WAIT", 0.3).to_f + WINDOW_DEPRECATED = "window? will be removed in Ferrum 0.22. Use opener_id, it is set for a target another one " \ + "opened." # You can create page yourself and assign it to target, used in cuprite # where we enhance page class and build page ourselves. @@ -159,11 +161,13 @@ def context_id @params["browserContextId"] unless @implicit end - # Whether this target is a window/tab, i.e. was opened via - # `window.open`/a link/etc. and thus has an {#opener_id}. + # Whether this target was opened by another one, i.e. has an {#opener_id}. + # + # @deprecated Will be removed in Ferrum 0.22. Use {#opener_id}. # # @return [Boolean] def window? + Utils::Deprecate.warn(WINDOW_DEPRECATED, user_code: true) !!opener_id end @@ -208,7 +212,7 @@ def service_worker? # @return [void] def maybe_sleep_if_new_window # Dirty hack because new window doesn't have events at all - sleep(NEW_WINDOW_WAIT) if window? + sleep(NEW_WINDOW_WAIT) if opener_id end # Sends a CDP command through {#client}. diff --git a/sig/ferrum/context.rbs b/sig/ferrum/context.rbs index a3d4d9dd..5006c5cb 100644 --- a/sig/ferrum/context.rbs +++ b/sig/ferrum/context.rbs @@ -1,6 +1,7 @@ module Ferrum class Context POSITION: ::Array[:first | :last] + WINDOWS_DEPRECATED: String attr_reader id: String @@ -32,7 +33,7 @@ module Ferrum def service_workers: () -> ::Array[Target] - def windows: (?(:first | :last)?, ?::Integer) -> ::Array[Page] + %a{deprecated} def windows: (?(:first | :last)?, ?::Integer) -> ::Array[Page] def wait_for_new_page: (?opener: Page?, ?timeout: (::Numeric | Deadline)) ?{ () -> void } -> Page? diff --git a/sig/ferrum/target.rbs b/sig/ferrum/target.rbs index e007a138..02638ad4 100644 --- a/sig/ferrum/target.rbs +++ b/sig/ferrum/target.rbs @@ -1,6 +1,7 @@ module Ferrum class Target NEW_WINDOW_WAIT: ::Float + WINDOW_DEPRECATED: String attr_writer page: Page? attr_reader options: Browser::Options @@ -47,7 +48,7 @@ module Ferrum def context_id: () -> String? - def window?: () -> bool + %a{deprecated} def window?: () -> bool def iframe?: () -> bool diff --git a/spec/browser_spec.rb b/spec/browser_spec.rb index 3fe9f074..5650adcd 100644 --- a/spec/browser_spec.rb +++ b/spec/browser_spec.rb @@ -333,8 +333,7 @@ def broken.puts(*) = raise(IOError, "closed stream") extensions: [File.expand_path("support/geolocation.js", __dir__)]) browser.go_to - browser.execute(%(window.open("/requiring_custom_extension", "popup"))) - popup, = browser.windows(:last) + popup = browser.wait_for_new_page { browser.execute(%(window.open("/requiring_custom_extension", "popup"))) } expect(popup.at_css("#location").text).to eq("1,-1") ensure @@ -897,22 +896,13 @@ def proxy_tunnel(socket, request, browser_url) expect(browser.targets.size).to eq(1) - browser.execute <<-JS - window.open("/simple", "popup1") - JS - - sleep 0.1 - - browser.execute <<-JS - window.open("/simple", "popup2") - JS + popup1 = browser.wait_for_new_page { browser.execute(%(window.open("/simple", "popup1"))) } + popup2 = browser.wait_for_new_page { browser.execute(%(window.open("/simple", "popup2"))) } + popup1.resize(width: 100, height: 200) + popup2.resize(width: 200, height: 100) - popup1, popup2 = browser.windows(:last, 2) - popup1&.resize(width: 100, height: 200) - popup2&.resize(width: 200, height: 100) - - expect(popup1&.viewport_size).to eq([100, 200]) - expect(popup2&.viewport_size).to eq([200, 100]) + expect(popup1.viewport_size).to eq([100, 200]) + expect(popup2.viewport_size).to eq([200, 100]) end it "resizes the viewport to the page size with fullscreen: true" do @@ -949,15 +939,10 @@ def proxy_tunnel(socket, request, browser_url) expect(browser.targets.size).to eq(2) - browser.execute <<~JS - window.open("/simple", "popup2") - JS - - sleep 0.1 + popup2 = browser.wait_for_new_page { browser.execute(%(window.open("/simple", "popup2"))) } expect(browser.targets.size).to eq(3) - popup2, = browser.windows(:last) expect(popup2.body).to include("Test") # Browser isn't dead, current page after executing JS closes connection # and we don't have a chance to push response to the Queue. Since the diff --git a/spec/context_spec.rb b/spec/context_spec.rb index 06f55247..2612660d 100644 --- a/spec/context_spec.rb +++ b/spec/context_spec.rb @@ -156,40 +156,22 @@ end describe "#windows" do - it "waits for the window to load" do - browser.go_to - - browser.execute <<-JS - window.open("/slow", "popup") - JS - - popup, = browser.windows(:last) - expect(popup.body).to include("slow page") - popup.close + around do |example| + Ferrum::Utils::Deprecate.reset! + example.run + Ferrum::Utils::Deprecate.reset! end - it "opens a second window under the same name" do + it "returns the pages opened by another page and warns that it is deprecated" do browser.go_to + popup = browser.wait_for_new_page { browser.execute(%(window.open("/simple", "popup"))) } - browser.execute <<-JS - window.open("/simple", "popup") - JS - - popup, = browser.windows(:last) - expect(popup.body).to include("Test") - popup.close - - sleep 0.5 # https://github.com/ChromeDevTools/devtools-protocol/issues/145 - - browser.execute <<-JS - window.open("/simple", "popup") - JS - - sleep 0.5 # https://github.com/ChromeDevTools/devtools-protocol/issues/145 - - same, = browser.windows(:last) - expect(same.body).to include("Test") - same.close + windows = nil + expect { windows = browser.windows } + .to output(/windows will be removed in Ferrum 0\.22.*called from #{__FILE__}/m).to_stderr + expect(windows).to eq([popup]) + ensure + popup&.close end end diff --git a/spec/dialog_spec.rb b/spec/dialog_spec.rb index b60c2b77..73d2dec7 100644 --- a/spec/dialog_spec.rb +++ b/spec/dialog_spec.rb @@ -50,11 +50,7 @@ def open_prompt(&block) it "works with second window" do browser.go_to - browser.execute <<-JS - window.open("/with_js", "popup") - JS - - popup, = browser.windows(:last) + popup = browser.wait_for_new_page { browser.execute(%(window.open("/with_js", "popup"))) } popup.on(:dialog, &:accept) popup.at_css("a#open-match").click @@ -80,11 +76,7 @@ def open_prompt(&block) it "works with second window" do browser.go_to - browser.execute <<-JS - window.open("/with_js", "popup") - JS - - popup, = browser.windows(:last) + popup = browser.wait_for_new_page { browser.execute(%(window.open("/with_js", "popup"))) } popup.on(:dialog, &:dismiss) popup.at_css("a#open-match").click diff --git a/spec/target_spec.rb b/spec/target_spec.rb index 2a153327..c6326860 100644 --- a/spec/target_spec.rb +++ b/spec/target_spec.rb @@ -106,12 +106,19 @@ def register_service_worker end describe "#window?" do - it "returns true for a popup" do - expect(open_popup).to be_window + around do |example| + Ferrum::Utils::Deprecate.reset! + example.run + Ferrum::Utils::Deprecate.reset! end - it "returns false for a page created directly" do - expect(target_of(page)).not_to be_window + it "returns true for a target another one opened and warns that it is deprecated" do + target = open_popup + + window = nil + expect { window = target.window? } + .to output(/window\? will be removed in Ferrum 0\.22.*called from #{__FILE__}/m).to_stderr + expect(window).to be(true) end end