Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/common/interactions/pointer_press.rb
1990 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
module Selenium
21
module WebDriver
22
module Interactions
23
#
24
# Actions related to clicking, tapping or pressing the pointer.
25
#
26
# @api private
27
#
28
29
class PointerPress < Interaction
30
include PointerEventProperties
31
32
BUTTONS = {left: 0,
33
touch: 0,
34
pen_contact: 0,
35
middle: 1,
36
right: 2,
37
pen_barrel: 2,
38
x1: 3,
39
back: 3,
40
x2: 4,
41
forward: 4}.freeze
42
DIRECTIONS = {down: :pointerDown, up: :pointerUp}.freeze
43
44
def initialize(source, direction, button, **opts)
45
super(source)
46
@direction = assert_direction(direction)
47
@button = assert_button(button)
48
@type = @direction
49
@opts = opts
50
end
51
52
def encode
53
process_opts.merge('type' => type.to_s, 'button' => @button)
54
end
55
56
private
57
58
def assert_source(source)
59
raise TypeError, "#{source.type} is not a valid input type" unless source.is_a? PointerInput
60
end
61
62
def assert_button(button)
63
case button
64
when Symbol
65
raise ArgumentError, "#{button} is not a valid button!" unless BUTTONS.key? button
66
67
BUTTONS[button]
68
when Integer
69
raise ArgumentError, 'Button number cannot be negative!' if button.negative?
70
71
button
72
else
73
raise TypeError, "button must be a positive integer or one of #{BUTTONS.keys}, not #{button.class}"
74
end
75
end
76
77
def assert_direction(direction)
78
raise ArgumentError, "#{direction.inspect} is not a valid button direction" unless DIRECTIONS.key? direction
79
80
DIRECTIONS[direction]
81
end
82
end # PointerPress
83
end # Interactions
84
end # WebDriver
85
end # Selenium
86
87