File: event_firing_bridge.rb

package info (click to toggle)
ruby-selenium-webdriver 3.141.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,500 kB
  • sloc: ruby: 6,660; makefile: 3
file content (127 lines) | stat: -rw-r--r-- 3,569 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

module Selenium
  module WebDriver
    module Support
      #
      # @api private
      #

      class EventFiringBridge
        def initialize(delegate, listener)
          @delegate = delegate

          @listener = if listener.respond_to? :call
                        BlockEventListener.new(listener)
                      else
                        listener
                      end
        end

        def get(url)
          dispatch(:navigate_to, url, driver) do
            @delegate.get(url)
          end
        end

        def go_forward
          dispatch(:navigate_forward, driver) do
            @delegate.go_forward
          end
        end

        def go_back
          dispatch(:navigate_back, driver) do
            @delegate.go_back
          end
        end

        def click_element(ref)
          dispatch(:click, create_element(ref), driver) do
            @delegate.click_element(ref)
          end
        end

        def clear_element(ref)
          dispatch(:change_value_of, create_element(ref), driver) do
            @delegate.clear_element(ref)
          end
        end

        def send_keys_to_element(ref, keys)
          dispatch(:change_value_of, create_element(ref), driver) do
            @delegate.send_keys_to_element(ref, keys)
          end
        end

        def find_element_by(how, what, parent = nil)
          e = dispatch(:find, how, what, driver) do
            @delegate.find_element_by how, what, parent
          end

          Element.new self, e.ref
        end

        def find_elements_by(how, what, parent = nil)
          es = dispatch(:find, how, what, driver) do
            @delegate.find_elements_by(how, what, parent)
          end

          es.map { |e| Element.new self, e.ref }
        end

        def execute_script(script, *args)
          dispatch(:execute_script, script, driver) do
            @delegate.execute_script(script, *args)
          end
        end

        def quit
          dispatch(:quit, driver) { @delegate.quit }
        end

        def close
          dispatch(:close, driver) { @delegate.close }
        end

        private

        def create_element(ref)
          # hmm. we're not passing self here to not fire events for potential calls made by the listener
          Element.new @delegate, ref
        end

        def driver
          @driver ||= Driver.new(self)
        end

        def dispatch(name, *args)
          @listener.__send__("before_#{name}", *args)
          returned = yield
          @listener.__send__("after_#{name}", *args)

          returned
        end

        def method_missing(meth, *args, &blk)
          @delegate.__send__(meth, *args, &blk)
        end
      end # EventFiringBridge
    end # Support
  end # WebDriver
end # Selenium