"Fossies" - the Fresh Open Source Software Archive

Member "selenium-selenium-4.8.1/rb/lib/selenium/webdriver/common/search_context.rb" (17 Feb 2023, 3419 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.

    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 SearchContext
   23       # @api private
   24       FINDERS = {
   25         class: 'class name',
   26         class_name: 'class name',
   27         css: 'css selector',
   28         id: 'id',
   29         link: 'link text',
   30         link_text: 'link text',
   31         name: 'name',
   32         partial_link_text: 'partial link text',
   33         relative: 'relative',
   34         tag_name: 'tag name',
   35         xpath: 'xpath'
   36       }.freeze
   37 
   38       #
   39       # Find the first element matching the given arguments
   40       #
   41       # When using Element#find_element with :xpath, be aware that webdriver
   42       # follows standard conventions: a search prefixed with "//" will search
   43       # the entire document, not just the children of this current node. Use
   44       # ".//" to limit your search to the children of the receiving Element.
   45       #
   46       # @overload find_element(how, what)
   47       #   @param [Symbol, String] how The method to find the element by
   48       #   @param [String] what The locator to use
   49       # @overload find_element(opts)
   50       #   @param [Hash] opts Find options
   51       #   @option opts [Symbol] :how Key named after the method to find the element by, containing the locator
   52       # @return [Element]
   53       #
   54       # @raise [Error::NoSuchElementError] if the element doesn't exist
   55       #
   56 
   57       def find_element(*args)
   58         how, what = extract_args(args)
   59 
   60         by = FINDERS[how.to_sym]
   61         raise ArgumentError, "cannot find element by #{how.inspect}" unless by
   62 
   63         bridge.find_element_by by, what, ref
   64       end
   65 
   66       #
   67       # Find all elements matching the given arguments
   68       #
   69       # @see SearchContext#find_element
   70       #
   71 
   72       def find_elements(*args)
   73         how, what = extract_args(args)
   74 
   75         by = FINDERS[how.to_sym]
   76         raise ArgumentError, "cannot find elements by #{how.inspect}" unless by
   77 
   78         bridge.find_elements_by by, what, ref
   79       end
   80 
   81       private
   82 
   83       def extract_args(args)
   84         case args.size
   85         when 2
   86           args
   87         when 1
   88           arg = args.first
   89 
   90           unless arg.respond_to?(:shift)
   91             raise ArgumentError,
   92                   "expected #{arg.inspect}:#{arg.class} to respond to #shift"
   93           end
   94 
   95           # this will be a single-entry hash, so use #shift over #first or #[]
   96           arr = arg.dup.shift
   97           raise ArgumentError, "expected #{arr.inspect} to have 2 elements" unless arr.size == 2
   98 
   99           arr
  100         else
  101           raise ArgumentError, "wrong number of arguments (#{args.size} for 2)"
  102         end
  103       end
  104     end # SearchContext
  105   end # WebDriver
  106 end # Selenium