"Fossies" - the Fresh Open Source Software Archive

Member "selenium-selenium-4.8.1/rb/lib/selenium/webdriver/common/driver_extensions/has_pinned_scripts.rb" (17 Feb 2023, 2556 Bytes) of package /linux/www/selenium-selenium-4.8.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "has_pinned_scripts.rb": 4.7.0_vs_4.8.0.

    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 DriverExtensions
   23       module HasPinnedScripts
   24         #
   25         # Returns the list of all pinned scripts.
   26         #
   27         # @return [Array<DevTools::PinnedScript>]
   28         #
   29 
   30         def pinned_scripts
   31           @pinned_scripts ||= []
   32         end
   33 
   34         #
   35         # Pins JavaScript snippet that is available during the whole
   36         # session on every page. This allows to store and call
   37         # scripts without sending them over the wire every time.
   38         #
   39         # @example
   40         #   script = driver.pin_script('return window.location.href')
   41         #   driver.execute_script(script)
   42         #   # navigate to a new page
   43         #   driver.execute_script(script)
   44         #
   45         # @param [String] script
   46         # @return [DevTools::PinnedScript]
   47         #
   48 
   49         def pin_script(script)
   50           script = DevTools::PinnedScript.new(script)
   51           pinned_scripts << script
   52 
   53           devtools.page.enable
   54           devtools.runtime.evaluate(expression: script.callable)
   55           response = devtools.page.add_script_to_evaluate_on_new_document(source: script.callable)
   56           script.devtools_identifier = response.dig('result', 'identifier')
   57 
   58           script
   59         end
   60 
   61         #
   62         # Unpins script making it undefined for the subsequent calls.
   63         #
   64         # @param [DevTools::PinnedScript] script
   65         #
   66 
   67         def unpin_script(script)
   68           devtools.runtime.evaluate(expression: script.remove)
   69           devtools.page.remove_script_to_evaluate_on_new_document(identifier: script.devtools_identifier)
   70           pinned_scripts.delete(script)
   71         end
   72       end # HasPinnedScripts
   73     end # DriverExtensions
   74   end # WebDriver
   75 end # Selenium