From 44cc85eaffa4b1c0bc7ad22c48012d2799552f2c Mon Sep 17 00:00:00 2001 From: Matthew McGarvey Date: Thu, 5 Nov 2020 17:05:33 -0600 Subject: [PATCH] Add helpful method for moving the cursor to an element this will allow users to easily trigger hover states --- spec/features/element_interactions_spec.cr | 19 ++++++++++++++++++ src/selenium/session.cr | 23 ++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/spec/features/element_interactions_spec.cr b/spec/features/element_interactions_spec.cr index b5b413c..c9791d9 100644 --- a/spec/features/element_interactions_spec.cr +++ b/spec/features/element_interactions_spec.cr @@ -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 + +

Hello, world!

+ 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 diff --git a/src/selenium/session.cr b/src/selenium/session.cr index 9221845..e067e3e 100644 --- a/src/selenium/session.cr +++ b/src/selenium/session.cr @@ -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