Class: Ferrum::Targets
- Inherits:
-
Object
- Object
- Ferrum::Targets
- Defined in:
- lib/ferrum/targets.rb
Constant Summary collapse
- TARGETS_RETRY_ATTEMPTS =
3
- TARGETS_RETRY_WAIT =
0.001
Instance Method Summary collapse
- #close_window(target_id) ⇒ Object
-
#initialize(browser) ⇒ Targets
constructor
A new instance of Targets.
- #open_new_window ⇒ Object
- #page ⇒ Object
- #push(target_id, page = nil) ⇒ Object
- #refresh ⇒ Object
- #reset ⇒ Object
- #switch_to_window(target_id) ⇒ Object
- #window_handle ⇒ Object
- #window_handles ⇒ Object
- #within_window(locator) ⇒ Object
Constructor Details
#initialize(browser) ⇒ Targets
Returns a new instance of Targets.
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/ferrum/targets.rb', line 8 def initialize(browser) @page = nil @mutex = Mutex.new @browser = browser @_default = targets.first["targetId"] @browser.on("Target.detachedFromTarget") do |params| page = remove_page(params["targetId"]) page&.close_connection end reset end |
Instance Method Details
#close_window(target_id) ⇒ Object
56 57 58 |
# File 'lib/ferrum/targets.rb', line 56 def close_window(target_id) remove_page(target_id)&.close end |
#open_new_window ⇒ Object
49 50 51 52 53 54 |
# File 'lib/ferrum/targets.rb', line 49 def open_new_window target_id = @browser.command("Target.createTarget", url: "about:blank", browserContextId: @_context_id)["targetId"] page = Page.new(target_id, @browser, true) push(target_id, page) target_id end |
#page ⇒ Object
32 33 34 35 |
# File 'lib/ferrum/targets.rb', line 32 def page raise NoSuchWindowError unless @page @page end |
#push(target_id, page = nil) ⇒ Object
22 23 24 |
# File 'lib/ferrum/targets.rb', line 22 def push(target_id, page = nil) @targets[target_id] = page end |
#refresh ⇒ Object
26 27 28 29 30 |
# File 'lib/ferrum/targets.rb', line 26 def refresh @mutex.synchronize do targets.each { |t| push(t["targetId"]) if !default?(t) && !has?(t) } end end |
#reset ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ferrum/targets.rb', line 73 def reset if @page @page.close @browser.command("Target.disposeBrowserContext", browserContextId: @_context_id) end @page = nil @targets = {} @_context_id = nil @_context_id = @browser.command("Target.createBrowserContext")["browserContextId"] target_id = @browser.command("Target.createTarget", url: "about:blank", browserContextId: @_context_id)["targetId"] @page = Page.new(target_id, @browser) push(target_id, @page) end |
#switch_to_window(target_id) ⇒ Object
45 46 47 |
# File 'lib/ferrum/targets.rb', line 45 def switch_to_window(target_id) @page = find_or_create_page(target_id) end |
#window_handle ⇒ Object
37 38 39 |
# File 'lib/ferrum/targets.rb', line 37 def window_handle page.target_id end |
#window_handles ⇒ Object
41 42 43 |
# File 'lib/ferrum/targets.rb', line 41 def window_handles @mutex.synchronize { @targets.keys } end |
#within_window(locator) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/ferrum/targets.rb', line 60 def within_window(locator) original = window_handle if window_handles.include?(locator) switch_to_window(locator) yield else raise NoSuchWindowError end ensure switch_to_window(original) end |