Path: blob/trunk/rb/lib/selenium/webdriver/common/search_context.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 SearchContext22# @api private23FINDERS = {24class: 'class name',25class_name: 'class name',26css: 'css selector',27id: 'id',28link: 'link text',29link_text: 'link text',30name: 'name',31partial_link_text: 'partial link text',32relative: 'relative',33tag_name: 'tag name',34xpath: 'xpath'35}.freeze3637class << self38attr_accessor :extra_finders3940def finders41FINDERS.merge(extra_finders || {})42end43end4445#46# Find the first element matching the given arguments47#48# When using Element#find_element with :xpath, be aware that webdriver49# follows standard conventions: a search prefixed with "//" will search50# the entire document, not just the children of this current node. Use51# ".//" to limit your search to the children of the receiving Element.52#53# @overload find_element(how, what)54# @param [Symbol, String] how The method to find the element by55# @param [String] what The locator to use56# @overload find_element(opts)57# @param [Hash] opts Find options58# @option opts [Symbol] :how Key named after the method to find the element by, containing the locator59# @return [Element]60#61# @raise [Error::NoSuchElementError] if the element doesn't exist62#6364def find_element(*args)65how, what = extract_args(args)6667by = SearchContext.finders[how.to_sym]68raise ArgumentError, "cannot find element by #{how.inspect}" unless by6970bridge.find_element_by by, what, ref71end7273#74# Find all elements matching the given arguments75#76# @see SearchContext#find_element77#7879def find_elements(*args)80how, what = extract_args(args)8182by = SearchContext.finders[how.to_sym]83raise ArgumentError, "cannot find elements by #{how.inspect}" unless by8485bridge.find_elements_by by, what, ref86end8788private8990def extract_args(args)91case args.size92when 293args94when 195arg = args.first9697unless arg.respond_to?(:shift)98raise ArgumentError,99"expected #{arg.inspect}:#{arg.class} to respond to #shift"100end101102# this will be a single-entry hash, so use #shift over #first or #[]103arr = arg.dup.shift104raise ArgumentError, "expected #{arr.inspect} to have 2 elements" unless arr.size == 2105106arr107else108raise ArgumentError, "wrong number of arguments (#{args.size} for 2)"109end110end111end # SearchContext112end # WebDriver113end # Selenium114115116