Class: Capybara::Cuprite::Page
- Inherits:
-
Ferrum::Page
- Object
- Ferrum::Page
- Capybara::Cuprite::Page
- Extended by:
- Forwardable
- Defined in:
- lib/capybara/cuprite/page.rb
Constant Summary collapse
- MODAL_WAIT =
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
- #accept_confirm ⇒ Object
- #accept_prompt(modal_response) ⇒ Object
- #before_click(node, name, _keys = [], offset = {}) ⇒ Object
- #closed? ⇒ Boolean
- #dismiss_confirm ⇒ Object
- #dismiss_prompt ⇒ Object
- #find_modal(options) ⇒ Object
- #frame_name ⇒ Object
- #hover(node) ⇒ Object
-
#initialize ⇒ Page
constructor
A new instance of Page.
- #reset_modals ⇒ Object
- #select(node, value) ⇒ Object
- #send_keys(node, keys) ⇒ Object
- #set(node, value) ⇒ Object
- #switch_to_frame(handle) ⇒ Object
- #title ⇒ Object
- #trigger(node, event) ⇒ Object
Constructor Details
#initialize ⇒ Page
Returns a new instance of Page.
22 23 24 25 26 27 28 |
# File 'lib/capybara/cuprite/page.rb', line 22 def initialize(...) @frame_stack = [] @accept_modal = [] @modal_messages = [] @modal_response = nil super end |
Instance Method Details
#accept_confirm ⇒ Object
60 61 62 |
# File 'lib/capybara/cuprite/page.rb', line 60 def accept_confirm @accept_modal << true end |
#accept_prompt(modal_response) ⇒ Object
68 69 70 71 |
# File 'lib/capybara/cuprite/page.rb', line 68 def accept_prompt(modal_response) @accept_modal << true @modal_response = modal_response end |
#before_click(node, name, _keys = [], offset = {}) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/capybara/cuprite/page.rb', line 104 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. if e.class_name == "MouseEventFailed" end |
#closed? ⇒ Boolean
138 139 140 |
# File 'lib/capybara/cuprite/page.rb', line 138 def closed? false end |
#dismiss_confirm ⇒ Object
64 65 66 |
# File 'lib/capybara/cuprite/page.rb', line 64 def dismiss_confirm @accept_modal << false end |
#dismiss_prompt ⇒ Object
73 74 75 |
# File 'lib/capybara/cuprite/page.rb', line 73 def dismiss_prompt @accept_modal << false end |
#find_modal(options) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/capybara/cuprite/page.rb', line 77 def find_modal() start = Ferrum::Utils::ElapsedTime.monotonic_time expect_text = [: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 = .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_name ⇒ Object
130 131 132 |
# File 'lib/capybara/cuprite/page.rb', line 130 def frame_name evaluate("window.name") end |
#hover(node) ⇒ Object
45 46 47 48 49 |
# File 'lib/capybara/cuprite/page.rb', line 45 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_modals ⇒ Object
98 99 100 101 102 |
# File 'lib/capybara/cuprite/page.rb', line 98 def reset_modals @accept_modal = [] @modal_response = nil @modal_messages = [] end |
#select(node, value) ⇒ Object
35 36 37 |
# File 'lib/capybara/cuprite/page.rb', line 35 def select(node, value) evaluate_on(node: node, expression: "_cuprite.select(this, #{value})") end |
#send_keys(node, keys) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/capybara/cuprite/page.rb', line 51 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
30 31 32 33 |
# File 'lib/capybara/cuprite/page.rb', line 30 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
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/capybara/cuprite/page.rb', line 118 def switch_to_frame(handle) case handle when :parent @frame_stack.pop when :top @frame_stack = [] else @frame_stack << handle inject_extensions end end |
#title ⇒ Object
134 135 136 |
# File 'lib/capybara/cuprite/page.rb', line 134 def title active_frame.current_title end |
#trigger(node, event) ⇒ Object
39 40 41 42 43 |
# File 'lib/capybara/cuprite/page.rb', line 39 def trigger(node, event) = {} .merge!(wait: TRIGGER_CLICK_WAIT) if event.to_s == "click" && TRIGGER_CLICK_WAIT.positive? evaluate_on(node: node, expression: %(_cuprite.trigger(this, "#{event}")), **) end |