How to use mouse method of Selenium.WebDriver Package

Best Selenium code snippet using Selenium.WebDriver.mouse

action_builder.rb

Source:action_builder.rb Github

copy

Full Screen

...20 class ActionBuilder21 #22 # @api private23 #24 def initialize(mouse, keyboard)25 @devices = {26 :mouse => mouse,27 :keyboard => keyboard28 }29 @actions = []30 end31 #32 # Performs a modifier key press. Does not release33 # the modifier key - subsequent interactions may assume it's kept pressed.34 # Note that the modifier key is never released implicitly - either35 # #key_up(key) or #send_keys(:null) must be called to release the modifier.36 #37 # Equivalent to:38 # driver.action.click(element).send_keys(key)39 # # or40 # driver.action.click.send_keys(key)41 #42 # @example Press a key43 #44 # driver.action.key_down(:control).perform45 #46 # @example Press a key on an element47 #48 # el = driver.find_element(:id, "some_id")49 # driver.action.key_down(el, :shift).perform50 #51 # @param [:shift, :alt, :control, :command, :meta] The key to press.52 # @param [Selenium::WebDriver::Element] element An optional element53 # @raise [ArgumentError] if the given key is not a modifier54 # @return [ActionBuilder] A self reference.55 #56 def key_down(*args)57 if args.first.kind_of? Element58 @actions << [:mouse, :click, [args.shift]]59 end60 @actions << [:keyboard, :press, args]61 self62 end63 #64 # Performs a modifier key release.65 # Releasing a non-depressed modifier key will yield undefined behaviour.66 #67 # @example Release a key68 #69 # driver.action.key_up(:shift).perform70 #71 # @example Release a key from an element72 #73 # el = driver.find_element(:id, "some_id")74 # driver.action.key_up(el, :alt).perform75 #76 # @param [:shift, :alt, :control, :command, :meta] The modifier key to release.77 # @param [Selenium::WebDriver::Element] element An optional element78 # @raise [ArgumentError] if the given key is not a modifier key79 # @return [ActionBuilder] A self reference.80 #81 def key_up(*args)82 if args.first.kind_of? Element83 @actions << [:mouse, :click, [args.shift]]84 end85 @actions << [:keyboard, :release, args]86 self87 end88 #89 # Sends keys to the active element. This differs from calling90 # Element#send_keys(keys) on the active element in two ways:91 #92 # * The modifier keys included in this call are not released.93 # * There is no attempt to re-focus the element - so send_keys(:tab) for switching elements should work.94 #95 # @example Send the text "help" to an element96 #97 # el = driver.find_element(:id, "some_id")98 # driver.action.send_keys(el, "help").perform99 #100 # @example Send the text "help" to the currently focused element101 #102 # driver.action.send_keys("help").perform103 #104 # @param [Selenium::WebDriver::Element] element An optional element105 # @param [String] keys The keys to be sent.106 # @return [ActionBuilder] A self reference.107 #108 def send_keys(*args)109 if args.first.kind_of? Element110 @actions << [:mouse, :click, [args.shift]]111 end112 @actions << [:keyboard, :send_keys, args]113 self114 end115 #116 # Clicks (without releasing) in the middle of the given element. This is117 # equivalent to:118 #119 # driver.action.move_to(element).click_and_hold120 #121 # @example Clicking and holding on some element122 #123 # el = driver.find_element(:id, "some_id")124 # driver.action.click_and_hold(el).perform125 #126 # @param [Selenium::WebDriver::Element] element the element to move to and click.127 # @return [ActionBuilder] A self reference.128 #129 def click_and_hold(element = nil)130 @actions << [:mouse, :down, [element]]131 self132 end133 #134 # Releases the depressed left mouse button at the current mouse location.135 #136 # @example Releasing an element after clicking and holding it137 #138 # el = driver.find_element(:id, "some_id")139 # driver.action.click_and_hold(el).release.perform140 #141 # @return [ActionBuilder] A self reference.142 #143 def release(element = nil)144 @actions << [:mouse, :up, [element]]145 self146 end147 #148 # Clicks in the middle of the given element. Equivalent to:149 #150 # driver.action.move_to(element).click151 #152 # When no element is passed, the current mouse position will be clicked.153 #154 # @example Clicking on an element155 #156 # el = driver.find_element(:id, "some_id")157 # driver.action.click(el).perform158 #159 # @example Clicking at the current mouse position160 #161 # driver.action.click.perform162 #163 # @param [Selenium::WebDriver::Element] element An optional element to click.164 # @return [ActionBuilder] A self reference.165 #166 def click(element = nil)167 @actions << [:mouse, :click, [element]]168 self169 end170 #171 # Performs a double-click at middle of the given element. Equivalent to:172 #173 # driver.action.move_to(element).double_click174 #175 # @example Double click an element176 #177 # el = driver.find_element(:id, "some_id")178 # driver.action.double_click(el).perform179 #180 # @param [Selenium::WebDriver::Element] element An optional element to move to.181 # @return [ActionBuilder] A self reference.182 #183 def double_click(element = nil)184 @actions << [:mouse, :double_click, [element]]185 self186 end187 #188 # Moves the mouse to the middle of the given element. The element is scrolled into189 # view and its location is calculated using getBoundingClientRect. Then the190 # mouse is moved to optional offset coordinates from the element.191 #192 # Note that when using offsets, both coordinates need to be passed.193 #194 # @example Scroll element into view and move the mouse to it195 #196 # el = driver.find_element(:id, "some_id")197 # driver.action.move_to(el).perform198 #199 # @example200 #201 # el = driver.find_element(:id, "some_id")202 # driver.action.move_to(el, 100, 100).perform203 #204 # @param [Selenium::WebDriver::Element] element to move to.205 # @param [Integer] right_by Optional offset from the top-left corner. A negative value means206 # coordinates right from the element.207 # @param [Integer] down_by Optional offset from the top-left corner. A negative value means208 # coordinates above the element.209 # @return [ActionBuilder] A self reference.210 #211 def move_to(element, right_by = nil, down_by = nil)212 if right_by && down_by213 @actions << [:mouse, :move_to, [element, right_by, down_by]]214 else215 @actions << [:mouse, :move_to, [element]]216 end217 self218 end219 #220 # Moves the mouse from its current position (or 0,0) by the given offset.221 # If the coordinates provided are outside the viewport (the mouse will222 # end up outside the browser window) then the viewport is scrolled to223 # match.224 #225 # @example Move the mouse to a certain offset from its current position226 #227 # driver.action.move_by(100, 100).perform228 #229 # @param [Integer] right_by horizontal offset. A negative value means moving the230 # mouse left.231 # @param [Integer] down_by vertical offset. A negative value means moving the mouse232 # up.233 # @return [ActionBuilder] A self reference.234 # @raise [MoveTargetOutOfBoundsError] if the provided offset is outside235 # the document's boundaries.236 #237 def move_by(right_by, down_by)238 @actions << [:mouse, :move_by, [right_by, down_by]]239 self240 end241 #242 # Performs a context-click at middle of the given element. First performs243 # a move_to to the location of the element.244 #245 # @example Context-click at middle of given element246 #247 # el = driver.find_element(:id, "some_id")248 # driver.action.context_click(el).perform249 #250 # @param [Selenium::WebDriver::Element] element An element to context click.251 # @return [ActionBuilder] A self reference.252 #253 def context_click(element = nil)254 @actions << [:mouse, :context_click, [element]]255 self256 end257 #258 # A convenience method that performs click-and-hold at the location of the259 # source element, moves to the location of the target element, then260 # releases the mouse.261 #262 # @example Drag and drop one element onto another263 #264 # el1 = driver.find_element(:id, "some_id1")265 # el2 = driver.find_element(:id, "some_id2")266 # driver.action.drag_and_drop(el1, el2).perform267 #268 # @param [Selenium::WebDriver::Element] source element to emulate button down at.269 # @param [Selenium::WebDriver::Element] target element to move to and release the270 # mouse at.271 # @return [ActionBuilder] A self reference.272 #273 def drag_and_drop(source, target)274 click_and_hold source275 move_to target276 release target277 self278 end279 #280 # A convenience method that performs click-and-hold at the location of281 # the source element, moves by a given offset, then releases the mouse.282 #283 # @example Drag and drop an element by offset284 #285 # el = driver.find_element(:id, "some_id1")286 # driver.action.drag_and_drop_by(el, 100, 100).perform287 #288 # @param [Selenium::WebDriver::Element] source Element to emulate button down at.289 # @param [Integer] right_by horizontal move offset.290 # @param [Integer] down_by vertical move offset.291 # @param [Selenium::WebDriver::Element] target Element to move to and release the292 # mouse at.293 # @return [ActionBuilder] A self reference.294 #295 def drag_and_drop_by(source, right_by, down_by)296 click_and_hold source297 move_by right_by, down_by298 release299 self300 end301 #302 # Executes the actions added to the builder.303 #304 def perform305 @actions.each { |receiver, method, args|306 @devices.fetch(receiver).__send__(method, *args)...

Full Screen

Full Screen

action_builder_spec.rb

Source:action_builder_spec.rb Github

copy

Full Screen

1require File.expand_path("../spec_helper", __FILE__)2describe Selenium::WebDriver::ActionBuilder do3 let(:bridge) { double("Bridge").as_null_object }4 let(:keyboard) { double(Selenium::WebDriver::Keyboard) }5 let(:mouse) { double(Selenium::WebDriver::Mouse) }6 let(:element) { Selenium::WebDriver::Element.new(bridge, 'element') }7 let(:builder) { Selenium::WebDriver::ActionBuilder.new(mouse, keyboard) }8 it "should create all keyboard actions" do9 keyboard.should_receive(:press).with(:shift)10 keyboard.should_receive(:send_keys).with("abc")11 keyboard.should_receive(:release).with(:control)12 builder.key_down(:shift).13 send_keys("abc").14 key_up(:control).perform15 end16 it "should pass an element to keyboard actions" do17 mouse.should_receive(:click).with(element)18 keyboard.should_receive(:press).with(:shift)19 builder.key_down(element, :shift).perform20 end21 it "should allow supplying individual elements to keyboard actions" do22 element2 = Selenium::WebDriver::Element.new(bridge, 'element2')23 element3 = Selenium::WebDriver::Element.new(bridge, 'element3')24 mouse.should_receive(:click).with(element)25 keyboard.should_receive(:press).with(:shift)26 mouse.should_receive(:click).with(element2)27 keyboard.should_receive(:send_keys).with("abc")28 mouse.should_receive(:click).with(element3)29 keyboard.should_receive(:release).with(:control)30 builder.key_down(element, :shift ).31 send_keys(element2, "abc").32 key_up(element3, :control).perform33 end34 it "should create all mouse actions" do35 mouse.should_receive(:down).with(element)36 mouse.should_receive(:up).with(element)37 mouse.should_receive(:click).with(element)38 mouse.should_receive(:double_click).with(element)39 mouse.should_receive(:move_to).with(element)40 mouse.should_receive(:context_click).with(element)41 builder.click_and_hold(element).42 release(element).43 click(element).44 double_click(element).45 move_to(element).46 context_click(element).perform47 end48 it "should drag and drop" do49 source = element50 target = Selenium::WebDriver::Element.new(bridge, 'element2')51 mouse.should_receive(:down).with(source)52 mouse.should_receive(:move_to).with(target)53 mouse.should_receive(:up)54 builder.drag_and_drop(source, target).perform55 end56 it "should drag and drop with offsets" do57 source = element58 mouse.should_receive(:down).with(source)59 mouse.should_receive(:move_by).with(-300, 400)60 mouse.should_receive(:up)61 builder.drag_and_drop_by(source, -300, 400).perform62 end63 it "can move the mouse by coordinates" do64 mouse.should_receive(:down).with(element)65 mouse.should_receive(:move_by).with(-300, 400)66 mouse.should_receive(:up)67 builder.click_and_hold(element).68 move_by(-300, 400).69 release.perform70 end71 it 'can click, hold and release at the current location' do72 mouse.should_receive(:down).with(nil)73 mouse.should_receive(:up)74 builder.click_and_hold.release.perform75 end76end...

Full Screen

Full Screen

mouse

Using AI Code Generation

copy

Full Screen

1driver.find_element(:name, 'q').send_keys "Selenium WebDriver"2driver.find_element(:name, 'btnG').click3driver.find_element(:link_text, 'Selenium WebDriver').click4driver.find_element(:name, 'q').send_keys "Selenium WebDriver"5driver.find_element(:name, 'btnG').click6driver.find_element(:link_text, 'Selenium WebDriver').click7driver.action.send_keys(:arrow_left).perform8driver.action.send_keys(:arrow_right).perform9driver.action.send_keys(:arrow_up).perform10driver.action.send_keys(:arrow_down).perform11driver.action.send_keys(:enter).perform12driver.find_element(:name, 'q').send_keys "Selenium WebDriver"13driver.find_element(:name, 'btnG').click14driver.find_element(:link_text, 'Selenium WebDriver').click15driver.action.click_and_hold(driver.find_element(:link_text, 'Selenium WebDriver')).perform16driver.action.move_to(driver.find_element(:link_text, 'Selenium WebDriver')).perform17driver.action.release(driver.find_element(:link_text, 'Selenium WebDriver')).perform

Full Screen

Full Screen

mouse

Using AI Code Generation

copy

Full Screen

1element = driver.find_element(:name, 'q')2element = driver.find_element(:name, 'q')3element = driver.find_element(:name, 'q')4element = driver.find_element(:name, 'q')5element = driver.find_element(:name, 'q')6element = driver.find_element(:name, 'q')7element = driver.find_element(:name, 'q')

Full Screen

Full Screen

mouse

Using AI Code Generation

copy

Full Screen

1driver.find_element(:name, 'q').send_keys 'Selenium WebDriver'2driver.find_element(:name, 'btnG').click3driver.find_element(:link, 'Selenium WebDriver').send_keys :enter4driver.find_element(:link, 'Downloads').click5driver.find_element(:link, 'Ruby').click6driver.find_element(:name, 'q').send_keys 'Selenium WebDriver'7driver.find_element(:name, 'btnG').click8driver.find_element(:link, 'Selenium WebDriver').send_keys :enter9driver.find_element(:link, 'Downloads').click10driver.find_element(:link, 'Ruby').click11driver.find_element(:name, 'q').send_keys 'Selenium WebDriver'12driver.find_element(:name, 'btnG').click13driver.find_element(:link, 'Selenium WebDriver').send_keys :enter14driver.find_element(:link, 'Downloads').click15driver.find_element(:link, 'Ruby').click16driver.find_element(:name, 'q').send_keys 'Selenium WebDriver'17driver.find_element(:name, 'btnG').click18driver.find_element(:link, 'Selenium WebDriver').send_keys :enter19driver.find_element(:link, 'Downloads').click20driver.find_element(:

Full Screen

Full Screen

mouse

Using AI Code Generation

copy

Full Screen

1searchbox = driver.find_element(:name, 'q')2searchbox = driver.find_element(:name, 'q')3mouse.double_click(searchbox)4searchbox = driver.find_element(:name, 'q')

Full Screen

Full Screen

mouse

Using AI Code Generation

copy

Full Screen

1driver.mouse.move_to driver.find_element(:link_text, 'Gmail')2driver.mouse.click driver.find_element(:link_text, 'Gmail')3def click(element = nil)4def double_click(element = nil)5def context_click(element = nil)6def down(element = nil)7def up(element = nil)8def move_to(element = nil, x = 0, y = 0)9def drag_and_drop(element, right_by, down_by)10def drag_and_drop_by(element, right_by, down_by)11def click_and_hold(element = nil)12def release(element = nil)13def move_by(x, y)

Full Screen

Full Screen

mouse

Using AI Code Generation

copy

Full Screen

1search_box = driver.find_element(:name, 'q')2wait = Selenium::WebDriver::Wait.new(:timeout => 10)3wait.until { driver.title.downcase.start_with? "selenium webdriver" }4driver.find_element(:name, 'q').send_keys 'Selenium WebDriver'5driver.find_element(:name, 'btnG').click6driver.find_element(:link, 'Selenium WebDriver').send_keys :enter7driver.find_element(:link, 'Downloads').click8driver.find_element(:link, 'Ruby').click9driver.find_element(:name, 'q').send_keys 'Selenium WebDriver'10driver.find_element(:name, 'btnG').click11driver.find_element(:link, 'Selenium WebDriver').send_keys :enter12driver.find_element(:link, 'Downloads').click13driver.find_element(:

Full Screen

Full Screen

mouse

Using AI Code Generation

copy

Full Screen

1driver.mouse.move_to driver.find_element(:link_text, 'Gmail')2driver.mouse.click driver.find_element(:link_text, 'Gmail')3def click(element = nil)4def double_click(element = nil)5def context_click(element = nil)6def down(element = nil)7def up(element = nil)8def move_to(element = nil, x = 0, y = 0)9def drag_and_drop(element, right_by, down_by)10def drag_and_drop_by(element, right_by, down_by)11def click_and_hold(element = nil)12def release(element = nil)13def move_by(x, y)

Full Screen

Full Screen

mouse

Using AI Code Generation

copy

Full Screen

1search_box = driver.find_element(:name, 'q')2wait = Selenium::WebDriver::Wait.new(:timeout => 10)3wait.until { driver.title.downcase.start_with? "selenium webdriver" }

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful