Path: blob/trunk/rb/spec/unit/selenium/webdriver/search_context_spec.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.1819require_relative 'spec_helper'2021module Selenium22module WebDriver23describe SearchContext do24let(:test_search_context) do25Class.new do26attr_reader :bridge, :ref2728include Selenium::WebDriver::SearchContext2930def initialize(bridge)31@bridge = bridge32end33end34end3536let(:element) { instance_double(Element) }37let(:bridge) { instance_double(Remote::Bridge).as_null_object }38let(:search_context) { test_search_context.new(bridge) }3940context 'when finding a single element' do41it 'accepts a hash' do42allow(bridge).to receive(:find_element_by).with('id', 'bar', nil).and_return(element)4344expect(search_context.find_element(id: 'bar')).to eq(element)45expect(bridge).to have_received(:find_element_by).with('id', 'bar', nil)46end4748it 'accepts two arguments' do49allow(bridge).to receive(:find_element_by).with('id', 'bar', nil).and_return(element)5051expect(search_context.find_element(:id, 'bar')).to eq(element)52expect(bridge).to have_received(:find_element_by).with('id', 'bar', nil)53end5455it "raises an error if given an invalid 'by'" do56expect {57search_context.find_element(foo: 'bar')58}.to raise_error(ArgumentError, 'cannot find element by :foo')59end6061it 'does not modify the hash given' do62selector = {id: 'foo'}6364search_context.find_element(selector)6566expect(selector).to eq(id: 'foo')67end68end6970context 'when finding multiple elements' do71it 'accepts a hash' do72allow(bridge).to receive(:find_elements_by).with('id', 'bar', nil).and_return([])7374expect(search_context.find_elements(id: 'bar')).to eq([])75expect(bridge).to have_received(:find_elements_by).with('id', 'bar', nil)76end7778it 'accepts two arguments' do79allow(bridge).to receive(:find_elements_by).with('id', 'bar', nil).and_return([])8081expect(search_context.find_elements(:id, 'bar')).to eq([])82expect(bridge).to have_received(:find_elements_by).with('id', 'bar', nil)83end8485it "raises an error if given an invalid 'by'" do86expect {87search_context.find_elements(foo: 'bar')88}.to raise_error(ArgumentError, 'cannot find elements by :foo')89end90end9192context 'when extra finders are registered' do93around do |example|94described_class.extra_finders = {accessibility_id: 'accessibility id'}95example.call96ensure97described_class.extra_finders = nil98end99100it 'finds element' do101allow(bridge).to receive(:find_element_by).with('accessibility id', 'foo', nil).and_return(element)102103expect(search_context.find_element(accessibility_id: 'foo')).to eq(element)104expect(bridge).to have_received(:find_element_by).with('accessibility id', 'foo', nil)105end106end107end108end # WebDriver109end # Selenium110111112