diff --git a/CHANGELOG.md b/CHANGELOG.md index aaec170d..0dd75feb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ - `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 +- `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 @@ -48,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/docs/3-navigation.md b/docs/3-navigation.md index 1e1a62dd..d00ebfec 100644 --- a/docs/3-navigation.md +++ b/docs/3-navigation.md @@ -145,3 +145,41 @@ 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 +``` + +#### 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/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..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 @@ -21,6 +23,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 @@ -66,11 +70,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, @@ -93,18 +108,75 @@ 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 + # 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 +225,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 +329,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/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 e3a61362..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 @@ -11,6 +12,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 @@ -24,13 +27,17 @@ module Ferrum def page: () -> Page - def pages: () -> ::Array[Page] + def pages: () ?{ (Target) -> boolish } -> ::Array[Page] def workers: () -> ::Array[Worker] 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? + + def wait_for_new_page!: (?opener: Page?, ?timeout: (::Numeric | Deadline)) ?{ () -> void } -> Page def create_page: (**untyped options) -> Page @@ -55,5 +62,9 @@ module Ferrum def target?: (String target_id) -> bool def inspect: () -> ::String + + private + + def announce_page: (Target target) -> void end end 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 b55d9e1d..2612660d 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 @@ -147,40 +156,138 @@ end describe "#windows" do - it "waits for the window to load" do - browser.go_to + around do |example| + Ferrum::Utils::Deprecate.reset! + example.run + Ferrum::Utils::Deprecate.reset! + end - browser.execute <<-JS - window.open("/slow", "popup") - JS + 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"))) } - popup, = browser.windows(:last) - expect(popup.body).to include("slow page") - popup.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 - it "opens a second window under the same name" do + describe "#wait_for_new_page" do + it "returns the page opened with window.open" do browser.go_to - browser.execute <<-JS - window.open("/simple", "popup") - JS + popup = browser.wait_for_new_page { browser.execute(%(window.open("/simple"))) } - popup, = browser.windows(:last) expect(popup.body).to include("Test") - popup.close + expect(browser.pages).to include(popup) + ensure + popup&.close + end - sleep 0.5 # https://github.com/ChromeDevTools/devtools-protocol/issues/145 + it "returns the page opened by a link with a blank target" do + browser.go_to + browser.content = %(open) - browser.execute <<-JS - window.open("/simple", "popup") - JS + 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.windows(:last) + same = browser.wait_for_new_page { browser.execute(%(window.open("/simple", "popup"))) } + expect(same.body).to include("Test") - same.close + 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 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