Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/support/event_firing_bridge.rb
1865 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 Support
23
#
24
# @api private
25
#
26
27
class EventFiringBridge
28
def initialize(delegate, listener)
29
@delegate = delegate
30
31
@listener = if listener.respond_to? :call
32
BlockEventListener.new(listener)
33
else
34
listener
35
end
36
end
37
38
def get(url)
39
dispatch(:navigate_to, url, driver) do
40
@delegate.get(url)
41
end
42
end
43
44
def go_forward
45
dispatch(:navigate_forward, driver) do
46
@delegate.go_forward
47
end
48
end
49
50
def go_back
51
dispatch(:navigate_back, driver) do
52
@delegate.go_back
53
end
54
end
55
56
def click_element(ref)
57
dispatch(:click, create_element(ref), driver) do
58
@delegate.click_element(ref)
59
end
60
end
61
62
def clear_element(ref)
63
dispatch(:change_value_of, create_element(ref), driver) do
64
@delegate.clear_element(ref)
65
end
66
end
67
68
def send_keys_to_element(ref, keys)
69
dispatch(:change_value_of, create_element(ref), driver) do
70
@delegate.send_keys_to_element(ref, keys)
71
end
72
end
73
74
def find_element_by(how, what, parent = nil)
75
e = dispatch(:find, how, what, driver) do
76
@delegate.find_element_by how, what, parent
77
end
78
79
Element.new self, e.ref.last
80
end
81
82
def find_elements_by(how, what, parent = nil)
83
es = dispatch(:find, how, what, driver) do
84
@delegate.find_elements_by(how, what, parent)
85
end
86
87
es.map { |e| Element.new self, e.ref.last }
88
end
89
90
def execute_script(script, *args)
91
dispatch(:execute_script, script, driver) do
92
@delegate.execute_script(script, *args)
93
end
94
end
95
96
def quit
97
dispatch(:quit, driver) { @delegate.quit }
98
end
99
100
def close
101
dispatch(:close, driver) { @delegate.close }
102
end
103
104
private
105
106
def create_element(ref)
107
# hmm. we're not passing self here to not fire events for potential calls made by the listener
108
Element.new @delegate, ref
109
end
110
111
def driver
112
@driver ||= Driver.new(bridge: self)
113
end
114
115
def dispatch(name, *)
116
@listener.__send__(:"before_#{name}", *)
117
returned = yield
118
@listener.__send__(:"after_#{name}", *)
119
120
returned
121
end
122
123
def method_missing(meth, ...) # rubocop:disable Style/MissingRespondToMissing
124
@delegate.__send__(meth, ...)
125
end
126
end # EventFiringBridge
127
end # Support
128
end # WebDriver
129
end # Selenium
130
131