Path: blob/trunk/rb/lib/selenium/webdriver/common/interactions/wheel_actions.rb
1990 views
# frozen_string_literal: true12# 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.1819module Selenium20module WebDriver21module WheelActions22attr_writer :default_scroll_duration2324#25# By default this is set to 250ms in the ActionBuilder constructor26# It can be overridden with default_scroll_duration=27#2829def default_scroll_duration30@default_scroll_duration ||= @duration / 1000.0 # convert ms to seconds31end3233#34# If the element is outside the viewport, scrolls the bottom of the element to the bottom of the viewport.35#36# @example Scroll to element37# el = driver.find_element(id: "some_id")38# driver.action.scroll_to(element).perform39#40# @param [Object] element Which element to scroll into the viewport.41# @param [Object] device Which device to use to scroll42# @return [Selenium::WebDriver::WheelActions] A self reference.43def scroll_to(element, device: nil)44scroll(origin: element, device: device)45end4647#48# Scrolls by provided amounts with the origin in the top left corner of the viewport.49#50# @example Scroll viewport by a specified amount51# el = driver.find_element(id: "some_id")52# driver.action.scroll_by(100, 200).perform53#54# @param [Integer] delta_x Distance along X axis to scroll using the wheel. A negative value scrolls left.55# @param [Integer] delta_y Distance along Y axis to scroll using the wheel. A negative value scrolls up.56# @return [Selenium::WebDriver::WheelActions] A self reference.57def scroll_by(delta_x, delta_y, device: nil)58scroll(delta_x: delta_x, delta_y: delta_y, device: device)59end6061#62# Scrolls by provided amount based on a provided origin.63#64# The scroll origin is either the center of an element or the upper left of the viewport plus any offsets.65# If the origin is an element, and the element is not in the viewport, the bottom of the element will first66# be scrolled to the bottom of the viewport.67#68# @example Scroll from element by a specified amount69# el = driver.find_element(id: "some_id")70# origin = WheelActions::ScrollOrigin.element(el)71# driver.action.scroll_from(origin, 0, 200).perform72#73# @example Scroll from element by a specified amount with an offset74# el = driver.find_element(id: "some_id")75# origin = WheelActions::ScrollOrigin.element(el, 10, 10)76# driver.action.scroll_from(origin, 100, 200).perform77#78# @example Scroll viewport by a specified amount with an offset79# origin = WheelActions::ScrollOrigin.viewport(10, 10)80# driver.action.scroll_from(origin, 0, 200).perform81#82# @param [ScrollOrigin] scroll_origin Where scroll originates (viewport or element center) plus provided offsets.83# @param [Integer] delta_x Distance along X axis to scroll using the wheel. A negative value scrolls left.84# @param [Integer] delta_y Distance along Y axis to scroll using the wheel. A negative value scrolls up.85# @return [Selenium::WebDriver::WheelActions] A self reference.86# @raise [Error::MoveTargetOutOfBoundsError] If the origin with offset is outside the viewport.87def scroll_from(scroll_origin, delta_x, delta_y, device: nil)88raise TypeError, "#{scroll_origin.inspect} isn't a valid ScrollOrigin" unless scroll_origin.is_a?(ScrollOrigin)8990scroll(x: scroll_origin.x_offset,91y: scroll_origin.y_offset,92delta_x: delta_x,93delta_y: delta_y,94origin: scroll_origin.origin,95device: device)96end9798private99100def scroll(**opts)101opts[:duration] = default_scroll_duration102wheel = wheel_input(opts.delete(:device))103wheel.create_scroll(**opts)104tick(wheel)105self106end107108def wheel_input(name = nil)109device(name: name, type: Interactions::WHEEL) || add_wheel_input('wheel')110end111end # WheelActions112end # WebDriver113end # Selenium114115116