Best Selenium code snippet using Selenium.WebDriver.WheelActions.scroll
action_builder_spec.rb
Source:action_builder_spec.rb
...206 window.pageYOffset&&t+o>window.pageXOffset207 IN_VIEWPORT208 driver.execute_script(in_viewport, element)209 end210 describe '#scroll_to', only: {browser: %i[chrome edge]} do211 it 'scrolls to element' do212 driver.navigate.to url_for('scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')213 iframe = driver.find_element(tag_name: 'iframe')214 expect(in_viewport?(iframe)).to eq false215 driver.action.scroll_to(iframe).perform216 expect(in_viewport?(iframe)).to eq true217 end218 it 'scrolls to provided offset from element' do219 driver.navigate.to url_for('scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')220 iframe = driver.find_element(tag_name: 'iframe')221 driver.action.scroll_to(iframe, 0, 200).perform222 driver.switch_to.frame(iframe)223 checkbox = driver.find_element(name: 'scroll_checkbox')224 expect(in_viewport?(checkbox)).to eq true225 end226 end227 describe '#scroll_by', only: {browser: %i[chrome edge]} do228 it 'scrolls by amount provided' do229 driver.navigate.to url_for('scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')230 footer = driver.find_element(tag_name: 'footer')231 y = footer.rect.y232 driver.action.scroll_by(0, y).perform233 expect(in_viewport?(footer)).to eq true234 end235 end236 describe '#scroll', only: {browser: %i[chrome edge]} do237 context 'when origin is offset from viewport' do238 it 'scrolls by amount provided' do239 driver.navigate.to url_for('scrolling_tests/frame_with_nested_scrolling_frame.html')240 iframe = driver.find_element(tag_name: 'iframe')241 origin = WheelActions::ScrollOrigin.viewport(10, 10)242 driver.action.scroll(0, 200, origin: origin).perform243 driver.switch_to.frame(iframe)244 checkbox = driver.find_element(name: 'scroll_checkbox')245 expect(in_viewport?(checkbox)).to eq true246 end247 it 'raises MoveTargetOutOfBoundsError when origin offset is out of viewport' do248 driver.navigate.to url_for('scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')249 origin = WheelActions::ScrollOrigin.viewport(-10, -10)250 expect {251 driver.action.scroll(0, 200, origin: origin).perform252 }.to raise_error(Error::MoveTargetOutOfBoundsError)253 end254 end255 context 'when origin is offset from center of element' do256 it 'scrolls by amount provided' do257 driver.navigate.to url_for('scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')258 footer = driver.find_element(tag_name: 'footer')259 origin = WheelActions::ScrollOrigin.element(footer, 10, -20)260 driver.action.scroll(0, 200, origin: origin).perform261 driver.switch_to.frame(driver.find_element(tag_name: 'iframe'))262 checkbox = driver.find_element(name: 'scroll_checkbox')263 expect(in_viewport?(checkbox)).to eq true264 end265 it 'raises MoveTargetOutOfBoundsError when origin offset is out of viewport' do266 driver.navigate.to url_for('scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')267 footer = driver.find_element(tag_name: 'footer')268 origin = WheelActions::ScrollOrigin.element(footer, 10, 20)269 expect {270 driver.action.scroll(0, 200, origin: origin).perform271 }.to raise_error(Error::MoveTargetOutOfBoundsError)272 end273 end274 end275 end # ActionBuilder276 end # WebDriver277end # Selenium...
wheel_actions.rb
Source:wheel_actions.rb
...17# under the License.18module Selenium19 module WebDriver20 module WheelActions21 def default_scroll_duration22 @default_scroll_duration ||= 0.25 # 250 milliseconds23 end24 #25 # Scrolls the viewport so that the provided element is at the bottom. Then the viewport26 # is further scrolled by the provided x and y offsets.27 #28 # @example Scroll to element29 #30 # el = driver.find_element(id: "some_id")31 # driver.action.scroll_to(el).perform32 #33 # @example Scroll to offset from element34 #35 # el = driver.find_element(id: "some_id")36 # driver.action.scroll_to(el, 0, 1000).perform37 #38 # @param [Selenium::WebDriver::Element] element to scroll to.39 # @param [Integer] x Optional horizontal offset to scroll from the center of the element.40 # A negative value means scrolling left.41 # @param [Integer] y Optional vertical offset to scroll from the center of the element.42 # A negative value means scrolling up.43 # @param [Symbol || String] device optional name of the WheelInput device to scroll with.44 # @return [ActionBuilder] A self reference.45 #46 def scroll_to(element, x = 0, y = 0, device: nil)47 scroll(x, y, origin: ScrollOrigin.element(element, 0, 0), device: device)48 end49 #50 # Scrolls the viewport from its current position by the provided offset.51 # The origin source is the upper left corner of the viewport52 #53 # @example Scroll by the provided amount54 #55 # driver.action.scroll_by(0, 1000).perform56 #57 # @param [Integer] x horizontal offset. A negative value means scrolling left.58 # @param [Integer] y vertical offset. A negative value means scrolling up.59 # @param [Symbol || String] device Optional name of the WheelInput device to scroll with60 # @return [ActionBuilder] A self reference.61 #62 def scroll_by(x = 0, y = 0, device: nil)63 scroll(x, y, origin: ScrollOrigin.viewport(0, 0), device: device)64 end65 #66 # Scrolls the viewport based on a ScrollOrigin.67 #68 # This method is needed instead of #scroll_to or #scroll_by69 # when what needs to be scrolled is only in a portion of the viewport.70 # The origin can be thought of as where on the screen you put the mouse when71 # executing a wheel scroll, or where you put your cursor when swiping a touch pad, etc.72 #73 # The offset for the origin is referenced to either the upper left of the viewport or the center of the element74 # The methods ScrollOrigin.viewport and ScrollOrigin.element are provided to ensure correct syntax75 #76 # @example Scroll by the provided amount originating from a source offset from upper left of the viewport77 #78 # el = driver.find_element(id: "some_id")79 # driver.action.scroll(0, 100, ScrollOrigin.viewport(400, 200)).perform80 #81 # @example Scroll by the provided amount originating from a source offset from the center of the provided element82 #83 # el = driver.find_element(id: "some_id")84 # driver.action.scroll(0, 100, ScrollOrigin.element(element, x: -400, 100)).perform85 #86 # @see ScrollOrigin87 #88 # @param [Integer] x horizontal offset. A negative value means scrolling left.89 # @param [Integer] y vertical offset. A negative value means scrolling up.90 # @param [Hash] origin The location the scroll originates from91 # @param [Symbol || String] device Optional name of the WheelInput device to scroll with92 # @return [ActionBuilder] A self reference.93 # @raise [MoveTargetOutOfBoundsError] if the origin value is outside the viewport.94 #95 def scroll(x, y, origin: ScrollOrigin.viewport(0, 0), device: nil)96 wheel = wheel_input(device)97 opts = {delta_x: Integer(x),98 delta_y: Integer(y),99 duration: default_scroll_duration}.merge!(origin)100 wheel.create_scroll(**opts)101 tick(wheel)102 self103 end104 private105 def wheel_input(name = nil)106 device(name: name, type: Interactions::WHEEL) || add_wheel_input('wheel')107 end108 end # WheelActions109 end # WebDriver110end # Selenium...
wheel_actions_spec.rb
Source:wheel_actions_spec.rb
...38 action_builder = ActionBuilder.new(bridge)39 expect(action_builder.send(:wheel_input)).to be_a(Interactions::WheelInput)40 end41 end42 describe '#scroll_to' do43 it 'calls scroll' do44 allow(builder).to receive(:scroll).and_call_original45 builder.scroll_to(element)46 expect(builder).to have_received(:scroll).with(0, 0, origin: {origin: element, x: 0, y: 0}, device: nil)47 end48 end49 describe '#scroll_by' do50 it 'calls scroll' do51 allow(builder).to receive(:scroll).and_call_original52 builder.scroll_by(20, 20)53 expect(builder).to have_received(:scroll).with(20, 20, origin: {origin: :viewport, x: 0, y: 0}, device: nil)54 end55 end56 describe '#scroll' do57 it 'gets wheel input' do58 allow(builder).to receive(:wheel_input).and_call_original59 builder.scroll 5, 5, device: wheel.name60 expect(builder).to have_received(:wheel_input).with(wheel.name)61 end62 it 'calls create_scroll with origin element offset' do63 allow(wheel).to receive(:create_scroll).and_call_original64 origin = WheelActions::ScrollOrigin.element(element, 10, 10)65 builder.scroll 5, 5, origin: origin, device: wheel.name66 expect(wheel).to have_received(:create_scroll).with(duration: duration,67 origin: element,68 x: 10,69 y: 10,70 delta_x: 5,71 delta_y: 5)72 end73 it 'calls create_scroll with origin viewport offset' do74 allow(wheel).to receive(:create_scroll).and_call_original75 origin = WheelActions::ScrollOrigin.viewport(-10, -10)76 builder.scroll 5, 5, origin: origin, device: wheel.name77 expect(wheel).to have_received(:create_scroll).with(duration: duration,78 origin: :viewport,79 x: -10,80 y: -10,81 delta_x: 5,82 delta_y: 5)83 end84 it 'passes the wheel to the #tick method' do85 allow(builder).to receive(:tick)86 builder.scroll 5, 587 expect(builder).to have_received(:tick).with(wheel)88 end89 it 'returns itself' do90 expect(builder.scroll(5, 5)).to eq(builder)91 end92 end93 end94 end # WebDriver95end # Selenium...
scroll_origin.rb
Source:scroll_origin.rb
1# frozen_string_literal: true2# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.18module Selenium19 module WebDriver20 module WheelActions21 module ScrollOrigin22 class << self23 def element(element, x = 0, y = 0)24 {origin: element, x: x, y: y}25 end26 def viewport(x, y)27 {origin: :viewport, x: x, y: y}28 end29 end30 end31 end # WheelActions32 end # WebDriver33end # Selenium...
scroll
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys "selenium"2driver.find_element(:name, 'btnG').click3driver.action.wheel(:down).perform4driver.action.wheel(:up).perform
scroll
Using AI Code Generation
1element = driver.find_element(:name, 'q')2driver.execute_script("window.scrollBy(0,1000)")3element = driver.find_element(:name, 'q')4mouse.scroll(0, 1000)5element = driver.find_element(:name, 'q')6touch.scroll(0, 1000)7element = driver.find_element(:name, 'q')8touch.scroll(0, 1000)9element = driver.find_element(:name, 'q')10touch.scroll(0, 1000)11element = driver.find_element(:name, 'q')12touch.scroll(
scroll
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys "selenium"2driver.find_element(:name, 'btnG').click3driver.action.wheel(:down).perform4driver.action.wheel(:up).perform
scroll
Using AI Code Generation
1element = driver.find_element(:name, 'q')2driver.execute_script("window.scrollBy(0,1000)")3element = driver.find_element(:name, 'q')4mouse.scroll(0, 1000)5element = driver.find_element(:name, 'q')6touch.scroll(0, 1000)7driver.action.scroll_by(0,100).perform
scroll
Using AI Code Generation
1element = driver.find_element(:name, 'q')2touch.scroll(0, 1000)3element = driver.find_element(:name, 'q')4touch.scroll(0, 1000)
scroll
Using AI Code Generation
1driver.action.scroll_by(0, 100).perform2scroll_by(x, y)3scroll_to(x, y)4scroll_to_element(element)5scroll_to_element_center(element)6scroll_to_element_bottom(element)7scroll_to_element_top(element)8scroll_to_element_left(element)9scroll_to_element_right(element)10scrollBy(x, y)11scrollTo(x, y)12scrollToElement(element)13scrollToElementCenter(element)14scrollToElementBottom(element)15scrollToElementTop(element)16scrollToElementLeft(element)17scrollToElementRight(element)18element = driver.find_element(:name, 'q')19touch.scroll(
scroll
Using AI Code Generation
1element = driver.find_element(:class, "gLFyf")2element = driver.find_element(:class, "FPdoLc")3driver.action.scroll_to(element).perform4element = driver.find_element(:class, "gLFyf")5driver.execute_script("arguments[0].scrollIntoView();", element)6element = driver.find_element(:class, "gLFyf")7driver.execute_script("arguments[0].scrollIntoView();", element)
scroll
Using AI Code Generation
1element = driver.find_element(:name, 'q')2driver.action.send_keys(element, :arrow_down).perform3driver.action.send_keys(element, :arrow_up).perform4driver.action.send_keys(element, :arrow_left).perform5driver.action.send_keys(element, :arrow_right).perform6driver.action.send_keys(element, :arrow_down * 100).perform7driver.action.send_keys(element, :arrow_up * 100).perform8driver.action.send_keys(element, :arrow_left * 100).perform9driver.action.send_keys(element, :arrow_right * 100).perform10driver.action.send_keys(:arrow_down * 100).perform11driver.action.send_keys(:arrow_up * 100).perform12driver.action.send_keys(:arrow_left * 100).perform13driver.action.send_keys(:arrow_right * 100).perform
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!