Skip to content

Commit d13256a

Browse files
committed
Add support for click delay and offset position
1 parent 23f0fc4 commit d13256a

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.byebug_history
22
Gemfile.*
3+
.ruby-version

lib/capybara/cuprite/node.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,16 @@ def disabled?
144144
command(:disabled?)
145145
end
146146

147-
def click(keys = [], offset = {})
148-
prepare_and_click(:left, __method__, keys, offset)
147+
def click(keys = [], **options)
148+
prepare_and_click(:left, __method__, keys, options)
149149
end
150150

151-
def right_click(keys = [], offset = {})
152-
prepare_and_click(:right, __method__, keys, offset)
151+
def right_click(keys = [], **options)
152+
prepare_and_click(:right, __method__, keys, options)
153153
end
154154

155-
def double_click(keys = [], offset = {})
156-
prepare_and_click(:double, __method__, keys, offset)
155+
def double_click(keys = [], **options)
156+
prepare_and_click(:double, __method__, keys, options)
157157
end
158158

159159
def hover
@@ -225,9 +225,12 @@ def as_json(*)
225225

226226
private
227227

228-
def prepare_and_click(mode, name, keys, offset)
228+
def prepare_and_click(mode, name, keys, options)
229+
delay = options[:delay].to_i
230+
x, y = options.values_at(:x, :y)
231+
offset = { x: x, y: y, position: options[:offset] || :top }
229232
command(:before_click, name, keys, offset)
230-
node.click(mode: mode, keys: keys, offset: offset)
233+
node.click(mode: mode, keys: keys, offset: offset, delay: delay)
231234
end
232235

233236
def filter_text(text)

lib/capybara/cuprite/page.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,12 @@ def reset_modals
9494

9595
def before_click(node, name, keys = [], offset = {})
9696
evaluate_on(node: node, expression: "_cuprite.scrollIntoViewport(this)")
97-
x, y = find_position(node, offset[:x], offset[:y])
97+
98+
# If offset is given it may go outside of the element and likely error
99+
# will be raised that we detected another element at this position.
100+
return true if offset[:x] || offset[:y]
101+
102+
x, y = find_position(node, **offset)
98103
evaluate_on(node: node, expression: "_cuprite.mouseEventTest(this, '#{name}', #{x}, #{y})")
99104
true
100105
rescue Ferrum::JavaScriptError => e
@@ -174,8 +179,8 @@ def prepare_page
174179
end
175180
end
176181

177-
def find_position(node, *args)
178-
x, y = node.find_position(*args)
182+
def find_position(node, **options)
183+
x, y = node.find_position(**options)
179184
rescue Ferrum::BrowserError => e
180185
if e.message == "Could not compute content quads."
181186
raise MouseEventFailed.new("MouseEventFailed: click, none, 0, 0")

spec/spec_helper.rb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,10 @@ module TestSessions
6060
node Element#drop can drop multiple strings
6161
node #visible? details non-summary descendants should be non-visible
6262
node #visible? works when details is toggled open and closed
63-
#all with obscured filter should only find nodes on top in the viewport when fals
63+
#all with obscured filter should only find nodes on top in the viewport when false
6464
#all with obscured filter should not find nodes on top outside the viewport when false
6565
#all with obscured filter should find top nodes outside the viewport when true
6666
#all with obscured filter should only find non-top nodes when true
67-
#click offset when w3c_click_offset is false should offset outside the element
68-
#click offset when w3c_click_offset is true should offset from center of element
69-
#click offset when w3c_click_offset is true should offset outside from center of element
70-
#double_click offset when w3c_click_offset is false should offset outside the element
71-
#double_click offset when w3c_click_offset is true should offset from center of element
72-
#double_click offset when w3c_click_offset is true should offset outside from center of element
73-
#right_click offset when w3c_click_offset is false should offset outside the element
74-
#right_click offset when w3c_click_offset is true should offset from center of element
75-
#right_click offset when w3c_click_offset is true should offset outside from center of element
7667
#fill_in should fill in a color field
7768
#has_field with valid should be false if field is invalid
7869
#find with spatial filters should find an element above another element

0 commit comments

Comments
 (0)