Class: Capybara::Cuprite::Page

Inherits:
Ferrum::Page
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/capybara/cuprite/page.rb

Constant Summary collapse

ENV.fetch("CUPRITE_MODAL_WAIT", 0.05).to_f
TRIGGER_CLICK_WAIT =
ENV.fetch("CUPRITE_TRIGGER_CLICK_WAIT", 0.1).to_f

Instance Method Summary collapse

Constructor Details

#initializePage

Returns a new instance of Page.



23
24
25
26
27
28
29
30
# File 'lib/capybara/cuprite/page.rb', line 23

def initialize(...)
  @frame_stack = []
  @accept_modal = []
  @modal_messages = []
  @modal_response = nil
  @unhandled_modal_error = nil
  super
end

Instance Method Details

#accept_confirmObject



77
78
79
# File 'lib/capybara/cuprite/page.rb', line 77

def accept_confirm
  @accept_modal << true
end

#accept_prompt(modal_response) ⇒ Object



85
86
87
88
# File 'lib/capybara/cuprite/page.rb', line 85

def accept_prompt(modal_response)
  @accept_modal << true
  @modal_response = modal_response
end

#before_click(node, name, _keys = [], offset = {}) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/capybara/cuprite/page.rb', line 122

def before_click(node, name, _keys = [], offset = {})
  evaluate_on(node: node, expression: "_cuprite.scrollIntoViewport(this)")

  # If offset is given it may go outside of the element and likely error
  # will be raised that we detected another element at this position.
  return true if offset[:x] || offset[:y]

  x, y = find_position(node, **offset)
  evaluate_on(node: node, expression: "_cuprite.mouseEventTest(this, '#{name}', #{x}, #{y})")
  true
rescue Ferrum::JavaScriptError => e
  raise MouseEventFailed, e.message if e.class_name == "MouseEventFailed"
end

#closed?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/capybara/cuprite/page.rb', line 155

def closed?
  false
end

#commandObject

The Page.javascriptDialogOpening event is handled on Ferrum's background CDP dispatcher thread, so raising there wouldn't reach the caller and would permanently kill that thread instead. The dialog is always accepted immediately from that thread, and the error (if any) is stashed here to be raised from the main thread the next time it makes a command round trip.



43
44
45
# File 'lib/capybara/cuprite/page.rb', line 43

def command(...)
  raise_pending_unhandled_modal_error! { super }
end

#dismiss_confirmObject



81
82
83
# File 'lib/capybara/cuprite/page.rb', line 81

def dismiss_confirm
  @accept_modal << false
end

#dismiss_promptObject



90
91
92
# File 'lib/capybara/cuprite/page.rb', line 90

def dismiss_prompt
  @accept_modal << false
end

#ferrum_commandObject

Keep a handle to Ferrum's own implementation before overriding it below, so answering a dialog (see handle_javascript_dialog) can bypass our override.



35
# File 'lib/capybara/cuprite/page.rb', line 35

alias ferrum_command command

#find_modal(options) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/capybara/cuprite/page.rb', line 94

def find_modal(options)
  start = Ferrum::Utils::ElapsedTime.monotonic_time
  expect_text = options[:text]
  expect_regexp = expect_text.is_a?(Regexp) ? expect_text : Regexp.escape(expect_text.to_s)
  not_found_msg = "Unable to find modal dialog"
  not_found_msg += " with #{expect_text}" if expect_text
  wait = options.fetch(:wait, timeout)

  begin
    modal_text = @modal_messages.shift
    raise Capybara::ModalNotFound if modal_text.nil? || (expect_text && !modal_text.match(expect_regexp))
  rescue Capybara::ModalNotFound => e
    raise e, not_found_msg if Ferrum::Utils::ElapsedTime.timeout?(start, wait)

    sleep(MODAL_WAIT)
    retry
  end

  modal_text
end

#frame_nameObject



147
148
149
# File 'lib/capybara/cuprite/page.rb', line 147

def frame_name
  evaluate("window.name")
end

#hover(node) ⇒ Object



62
63
64
65
66
# File 'lib/capybara/cuprite/page.rb', line 62

def hover(node)
  evaluate_on(node: node, expression: "_cuprite.scrollIntoViewport(this)")
  x, y = find_position(node)
  command("Input.dispatchMouseEvent", type: "mouseMoved", x: x, y: y)
end

#reset_modalsObject



115
116
117
118
119
120
# File 'lib/capybara/cuprite/page.rb', line 115

def reset_modals
  @accept_modal = []
  @modal_response = nil
  @modal_messages = []
  @unhandled_modal_error = nil
end

#select(node, value) ⇒ Object



52
53
54
# File 'lib/capybara/cuprite/page.rb', line 52

def select(node, value)
  evaluate_on(node: node, expression: "_cuprite.select(this, #{value})")
end

#send_keys(node, keys) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/capybara/cuprite/page.rb', line 68

def send_keys(node, keys)
  unless evaluate_on(node: node, expression: %(_cuprite.containsSelection(this)))
    before_click(node, "click")
    node.click(mode: :left, keys: keys)
  end

  keyboard.type(keys)
end

#set(node, value) ⇒ Object



47
48
49
50
# File 'lib/capybara/cuprite/page.rb', line 47

def set(node, value)
  object_id = command("DOM.resolveNode", nodeId: node.node_id).dig("object", "objectId")
  evaluate("_cuprite.set(arguments[0], arguments[1])", { "objectId" => object_id }, value)
end

#switch_to_frame(handle) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/capybara/cuprite/page.rb', line 136

def switch_to_frame(handle)
  case handle
  when :parent
    @frame_stack.pop
  when :top
    @frame_stack = []
  else
    @frame_stack << handle
  end
end

#titleObject



151
152
153
# File 'lib/capybara/cuprite/page.rb', line 151

def title
  active_frame.current_title
end

#trigger(node, event) ⇒ Object



56
57
58
59
60
# File 'lib/capybara/cuprite/page.rb', line 56

def trigger(node, event)
  options = {}
  options.merge!(wait: TRIGGER_CLICK_WAIT) if event.to_s == "click" && TRIGGER_CLICK_WAIT.positive?
  evaluate_on(node: node, expression: %(_cuprite.trigger(this, "#{event}")), **options)
end