From 3c95a108bf7146e051da9d564f3f57cad42c2ec1 Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Sat, 6 Jan 2024 16:19:56 +0300 Subject: [PATCH] ref: Target builds client and stores as instance var --- lib/ferrum/target.rb | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/ferrum/target.rb b/lib/ferrum/target.rb index 099851c9..819a9b87 100644 --- a/lib/ferrum/target.rb +++ b/lib/ferrum/target.rb @@ -8,13 +8,14 @@ class Target # where we enhance page class and build page ourselves. attr_writer :page - attr_reader :session_id + attr_reader :session_id, :options - def initialize(client, session_id = nil, params = nil) + def initialize(browser_client, session_id = nil, params = nil) @page = nil - @client = client @session_id = session_id @params = params + @browser_client = browser_client + @options = browser_client.options end def update(params) @@ -29,9 +30,13 @@ def page @page ||= build_page end + def client + @client ||= build_client + end + def build_page(**options) maybe_sleep_if_new_window - Page.new(build_client, context_id: context_id, target_id: id, **options) + Page.new(client, context_id: context_id, target_id: id, **options) end def id @@ -70,11 +75,13 @@ def maybe_sleep_if_new_window private def build_client - options = @client.options - return @client.session(session_id) if options.flatten + return @browser_client.session(session_id) if options.flatten - ws_url = options.ws_url.merge(path: "/devtools/page/#{id}").to_s Client.new(ws_url, options) end + + def ws_url + options.ws_url.merge(path: "/devtools/page/#{id}").to_s + end end end