From 1aaa1c97bb8da99924ac047a03250de1812ba8fb Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Fri, 5 Nov 2021 21:20:07 +0300 Subject: [PATCH] Refactor code to fix rubocop warns --- .rubocop.yml | 3 ++ lib/ferrum/network.rb | 58 ++++++++++++++++++++++++--------------- lib/ferrum/page.rb | 2 ++ lib/ferrum/page/frames.rb | 35 +++++++++++++++++++++-- 4 files changed, 74 insertions(+), 24 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index f0a59924..328781ab 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -32,3 +32,6 @@ Style/MultilineBlockChain: Metrics/ModuleLength: Exclude: - spec/**/* + +Metrics/ParameterLists: + Max: 6 diff --git a/lib/ferrum/network.rb b/lib/ferrum/network.rb index 317da03b..5250ef43 100644 --- a/lib/ferrum/network.rb +++ b/lib/ferrum/network.rb @@ -125,6 +125,34 @@ def authorize(user:, password:, type: :server, &block) end def subscribe + subscribe_request_will_be_sent + subscribe_response_received + subscribe_loading_finished + subscribe_loading_failed + subscribe_log_entry_added + end + + def authorized_response(ids, request_id, username, password) + if ids.include?(request_id) + { response: "CancelAuth" } + elsif username && password + { response: "ProvideCredentials", + username: username, + password: password } + end + end + + def select(request_id) + @traffic.select { |e| e.id == request_id } + end + + def build_exchange(id) + Network::Exchange.new(@page, id).tap { |e| @traffic << e } + end + + private + + def subscribe_request_will_be_sent @page.on("Network.requestWillBeSent") do |params| request = Network::Request.new(params) @@ -151,7 +179,9 @@ def subscribe @exchange = exchange if exchange.navigation_request?(@page.main_frame.id) end + end + def subscribe_response_received @page.on("Network.responseReceived") do |params| exchange = select(params["requestId"]).last @@ -160,12 +190,16 @@ def subscribe exchange.response = response end end + end + def subscribe_loading_finished @page.on("Network.loadingFinished") do |params| exchange = select(params["requestId"]).last exchange.response.body_size = params["encodedDataLength"] if exchange&.response end + end + def subscribe_loading_failed @page.on("Network.loadingFailed") do |params| exchange = select(params["requestId"]).last exchange.error ||= Network::Error.new @@ -176,7 +210,9 @@ def subscribe exchange.error.monotonic_time = params["timestamp"] exchange.error.canceled = params["canceled"] end + end + def subscribe_log_entry_added @page.on("Log.entryAdded") do |params| entry = params["entry"] || {} if entry["source"] == "network" && entry["level"] == "error" @@ -191,28 +227,6 @@ def subscribe end end - def authorized_response(ids, request_id, username, password) - if ids.include?(request_id) - { response: "CancelAuth" } - elsif username && password - { response: "ProvideCredentials", - username: username, - password: password } - else - { response: "CancelAuth" } - end - end - - def select(request_id) - @traffic.select { |e| e.id == request_id } - end - - def build_exchange(id) - Network::Exchange.new(@page, id).tap { |e| @traffic << e } - end - - private - def blacklist_subscribe return unless blacklist? raise ArgumentError, "You can't use blacklist along with whitelist" if whitelist? diff --git a/lib/ferrum/page.rb b/lib/ferrum/page.rb index bbd26d11..5a5c25f7 100644 --- a/lib/ferrum/page.rb +++ b/lib/ferrum/page.rb @@ -162,9 +162,11 @@ def window_id @browser.command("Browser.getWindowForTarget", targetId: @target_id)["windowId"] end + # rubocop:disable Naming/AccessorMethodName def set_window_bounds(bounds = {}) @browser.command("Browser.setWindowBounds", windowId: window_id, bounds: bounds) end + # rubocop:enable Naming/AccessorMethodName def command(method, wait: 0, slowmoable: false, **params) iteration = @event.reset if wait.positive? diff --git a/lib/ferrum/page/frames.rb b/lib/ferrum/page/frames.rb index 904aab44..f07f8a87 100644 --- a/lib/ferrum/page/frames.rb +++ b/lib/ferrum/page/frames.rb @@ -24,24 +24,47 @@ def frame_by(id: nil, name: nil, execution_id: nil) end def frames_subscribe + subscribe_frame_attached + subscribe_frame_started_loading + subscribe_frame_navigated + subscribe_frame_stopped_loading + + subscribe_navigated_within_document + + subscribe_request_will_be_sent + + subscribe_execution_context_created + subscribe_execution_context_destroyed + subscribe_execution_contexts_cleared + end + + private + + def subscribe_frame_attached on("Page.frameAttached") do |params| parent_frame_id, frame_id = params.values_at("parentFrameId", "frameId") @frames[frame_id] = Frame.new(frame_id, self, parent_frame_id) end + end + def subscribe_frame_started_loading on("Page.frameStartedLoading") do |params| frame = @frames[params["frameId"]] frame.state = :started_loading @event.reset end + end + def subscribe_frame_navigated on("Page.frameNavigated") do |params| frame_id, name = params["frame"]&.values_at("id", "name") frame = @frames[frame_id] frame.state = :navigated frame.name = name unless name.to_s.empty? end + end + def subscribe_frame_stopped_loading on("Page.frameStoppedLoading") do |params| # `DOM.performSearch` doesn't work without getting #document node first. # It returns node with nodeId 1 and nodeType 9 from which descend the @@ -57,11 +80,15 @@ def frames_subscribe @event.set if idling? end + end + def subscribe_navigated_within_document on("Page.navigatedWithinDocument") do @event.set if idling? end + end + def subscribe_request_will_be_sent on("Network.requestWillBeSent") do |params| # Possible types: # Document, Stylesheet, Image, Media, Font, Script, TextTrack, XHR, @@ -69,7 +96,9 @@ def frames_subscribe # CSPViolationReport, Other @event.reset if params["frameId"] == @main_frame.id && params["type"] == "Document" end + end + def subscribe_execution_context_created on("Runtime.executionContextCreated") do |params| context_id = params.dig("context", "id") frame_id = params.dig("context", "auxData", "frameId") @@ -87,21 +116,23 @@ def frames_subscribe @frames[frame_id] ||= frame end + end + def subscribe_execution_context_destroyed on("Runtime.executionContextDestroyed") do |params| execution_id = params["executionContextId"] frame = frame_by(execution_id: execution_id) frame&.execution_id = nil end + end + def subscribe_execution_contexts_cleared on("Runtime.executionContextsCleared") do @frames.delete_if { |_, f| !f.main? } @main_frame.execution_id = nil end end - private - def idling? @frames.all? { |_, f| f.state == :stopped_loading } end