Path: blob/trunk/rb/lib/selenium/webdriver/support/event_firing_bridge.rb
1865 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 Support22#23# @api private24#2526class EventFiringBridge27def initialize(delegate, listener)28@delegate = delegate2930@listener = if listener.respond_to? :call31BlockEventListener.new(listener)32else33listener34end35end3637def get(url)38dispatch(:navigate_to, url, driver) do39@delegate.get(url)40end41end4243def go_forward44dispatch(:navigate_forward, driver) do45@delegate.go_forward46end47end4849def go_back50dispatch(:navigate_back, driver) do51@delegate.go_back52end53end5455def click_element(ref)56dispatch(:click, create_element(ref), driver) do57@delegate.click_element(ref)58end59end6061def clear_element(ref)62dispatch(:change_value_of, create_element(ref), driver) do63@delegate.clear_element(ref)64end65end6667def send_keys_to_element(ref, keys)68dispatch(:change_value_of, create_element(ref), driver) do69@delegate.send_keys_to_element(ref, keys)70end71end7273def find_element_by(how, what, parent = nil)74e = dispatch(:find, how, what, driver) do75@delegate.find_element_by how, what, parent76end7778Element.new self, e.ref.last79end8081def find_elements_by(how, what, parent = nil)82es = dispatch(:find, how, what, driver) do83@delegate.find_elements_by(how, what, parent)84end8586es.map { |e| Element.new self, e.ref.last }87end8889def execute_script(script, *args)90dispatch(:execute_script, script, driver) do91@delegate.execute_script(script, *args)92end93end9495def quit96dispatch(:quit, driver) { @delegate.quit }97end9899def close100dispatch(:close, driver) { @delegate.close }101end102103private104105def create_element(ref)106# hmm. we're not passing self here to not fire events for potential calls made by the listener107Element.new @delegate, ref108end109110def driver111@driver ||= Driver.new(bridge: self)112end113114def dispatch(name, *)115@listener.__send__(:"before_#{name}", *)116returned = yield117@listener.__send__(:"after_#{name}", *)118119returned120end121122def method_missing(meth, ...) # rubocop:disable Style/MissingRespondToMissing123@delegate.__send__(meth, ...)124end125end # EventFiringBridge126end # Support127end # WebDriver128end # Selenium129130131