Skip to content

Commit

Permalink
Add helpful method for moving the cursor to an element
Browse files Browse the repository at this point in the history
this will allow users to easily trigger hover states
  • Loading branch information
matthewmcgarvey committed Nov 5, 2020
1 parent 956a73d commit 44cc85e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
19 changes: 19 additions & 0 deletions spec/features/element_interactions_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,24 @@ module Selenium::Command
element.displayed?.should be_false
end
end

it "can move the cursor to an element" do
TestServer.route "/home", <<-HTML
<style>
#text:hover {
color: rgba(83, 137, 4, 0.6);
}
</style>
<p id="text">Hello, world!</p>
HTML

with_session do |session|
session.navigate_to("http://localhost:3002/home")
element = session.find_element(:css, "#text")
element.css_value("color").should_not eq("rgba(83, 137, 4, 0.6)")
session.move_to(element)
session.find_element(:css, "#text").css_value("color").should eq("rgba(83, 137, 4, 0.6)")
end
end
end
end
23 changes: 23 additions & 0 deletions src/selenium/session.cr
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,29 @@ class Selenium::Session
command_handler.execute(:switch_to_parent_frame, path_variables)
end

def move_to(element)
rect = element.rect
rect_x = rect.try(&.x)
rect_width = rect.try(&.width)
rect_y = rect.try(&.y)
rect_height = rect.try(&.height)

return if rect_x.nil?
return if rect_width.nil?
return if rect_y.nil?
return if rect_height.nil?

move_to_x = (rect_x + (rect_width / 2)).to_i
move_to_y = (rect_y + (rect_height / 2)).to_i

sequence = Selenium::InputSourceActionSequence.new(
type: "pointer",
id: "action-sequence-key",
actions: [Selenium::Action.new("pointerMove", x: move_to_x, y: move_to_y)]
)
perform_actions([sequence])
end

private def path_variables
{":session_id" => id}
end
Expand Down

0 comments on commit 44cc85e

Please sign in to comment.