Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/unit/selenium/webdriver/search_context_spec.rb
1865 views
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
require_relative 'spec_helper'
21
22
module Selenium
23
module WebDriver
24
describe SearchContext do
25
let(:test_search_context) do
26
Class.new do
27
attr_reader :bridge, :ref
28
29
include Selenium::WebDriver::SearchContext
30
31
def initialize(bridge)
32
@bridge = bridge
33
end
34
end
35
end
36
37
let(:element) { instance_double(Element) }
38
let(:bridge) { instance_double(Remote::Bridge).as_null_object }
39
let(:search_context) { test_search_context.new(bridge) }
40
41
context 'when finding a single element' do
42
it 'accepts a hash' do
43
allow(bridge).to receive(:find_element_by).with('id', 'bar', nil).and_return(element)
44
45
expect(search_context.find_element(id: 'bar')).to eq(element)
46
expect(bridge).to have_received(:find_element_by).with('id', 'bar', nil)
47
end
48
49
it 'accepts two arguments' do
50
allow(bridge).to receive(:find_element_by).with('id', 'bar', nil).and_return(element)
51
52
expect(search_context.find_element(:id, 'bar')).to eq(element)
53
expect(bridge).to have_received(:find_element_by).with('id', 'bar', nil)
54
end
55
56
it "raises an error if given an invalid 'by'" do
57
expect {
58
search_context.find_element(foo: 'bar')
59
}.to raise_error(ArgumentError, 'cannot find element by :foo')
60
end
61
62
it 'does not modify the hash given' do
63
selector = {id: 'foo'}
64
65
search_context.find_element(selector)
66
67
expect(selector).to eq(id: 'foo')
68
end
69
end
70
71
context 'when finding multiple elements' do
72
it 'accepts a hash' do
73
allow(bridge).to receive(:find_elements_by).with('id', 'bar', nil).and_return([])
74
75
expect(search_context.find_elements(id: 'bar')).to eq([])
76
expect(bridge).to have_received(:find_elements_by).with('id', 'bar', nil)
77
end
78
79
it 'accepts two arguments' do
80
allow(bridge).to receive(:find_elements_by).with('id', 'bar', nil).and_return([])
81
82
expect(search_context.find_elements(:id, 'bar')).to eq([])
83
expect(bridge).to have_received(:find_elements_by).with('id', 'bar', nil)
84
end
85
86
it "raises an error if given an invalid 'by'" do
87
expect {
88
search_context.find_elements(foo: 'bar')
89
}.to raise_error(ArgumentError, 'cannot find elements by :foo')
90
end
91
end
92
93
context 'when extra finders are registered' do
94
around do |example|
95
described_class.extra_finders = {accessibility_id: 'accessibility id'}
96
example.call
97
ensure
98
described_class.extra_finders = nil
99
end
100
101
it 'finds element' do
102
allow(bridge).to receive(:find_element_by).with('accessibility id', 'foo', nil).and_return(element)
103
104
expect(search_context.find_element(accessibility_id: 'foo')).to eq(element)
105
expect(bridge).to have_received(:find_element_by).with('accessibility id', 'foo', nil)
106
end
107
end
108
end
109
end # WebDriver
110
end # Selenium
111
112