Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/integration/selenium/webdriver/shadow_root_spec.rb
1864 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 ShadowRoot, exclusive: [{bidi: false, reason: 'Not yet implemented with BiDi'},
25
{browser: %i[chrome firefox edge safari]}] do
26
before { driver.navigate.to url_for('webComponents.html') }
27
28
let(:custom_element) { driver.find_element(css: 'custom-checkbox-element') }
29
30
it 'gets shadow root from driver' do
31
shadow_root = custom_element.shadow_root
32
expect(shadow_root).to be_a described_class
33
end
34
35
it 'raises error if no shadow root', exclude: {browser: :safari, reason: 'NoMethodError'} do
36
driver.navigate.to url_for('simpleTest.html')
37
div = driver.find_element(css: 'div')
38
expect { div.shadow_root }.to raise_error(Error::NoSuchShadowRootError)
39
end
40
41
it 'raises error if the shadow root is detached', exclude: {browser: :safari, reason: 'NoMethodError'} do
42
driver.navigate.to url_for('simpleTest.html')
43
div = driver.find_element(css: 'div')
44
driver.execute_script('arguments[0].attachShadow({ mode: "open" });', div)
45
root = div.shadow_root
46
driver.execute_script('arguments[0].remove();', div)
47
expect { root.find_element(css: '#x') }.to raise_error(Error::DetachedShadowRootError)
48
end
49
50
it 'gets shadow root from script',
51
exclude: {browser: :safari, reason: 'returns correct node, but references shadow root as a element'} do
52
shadow_root = custom_element.shadow_root
53
execute_shadow_root = driver.execute_script('return arguments[0].shadowRoot;', custom_element)
54
expect(execute_shadow_root).to eq shadow_root
55
end
56
57
describe '#find_element' do
58
it 'by css' do
59
shadow_root = custom_element.shadow_root
60
element = shadow_root.find_element(css: 'input')
61
62
expect(element).to be_a Element
63
end
64
65
it 'by xpath', except: {browser: %i[chrome edge firefox safari],
66
reason: 'https://bugs.chromium.org/p/chromedriver/issues/detail?id=4097'} do
67
shadow_root = custom_element.shadow_root
68
element = shadow_root.find_element(xpath: "//input[type='checkbox']")
69
70
expect(element).to be_a Element
71
end
72
73
it 'by link text' do
74
shadow_root = custom_element.shadow_root
75
element = shadow_root.find_element(link_text: 'Example Link')
76
77
expect(element).to be_a Element
78
end
79
80
it 'by partial link text' do
81
shadow_root = custom_element.shadow_root
82
element = shadow_root.find_element(partial_link_text: 'Link')
83
84
expect(element).to be_a Element
85
end
86
87
it 'by tag name', except: {browser: %i[chrome edge firefox safari],
88
reason: 'https://bugs.chromium.org/p/chromedriver/issues/detail?id=4097'} do
89
shadow_root = custom_element.shadow_root
90
element = shadow_root.find_element(tag_name: 'input')
91
92
expect(element).to be_a Element
93
end
94
95
it 'raises error if not found' do
96
shadow_root = custom_element.shadow_root
97
98
expect { shadow_root.find_element(css: 'no') }.to raise_error(Error::NoSuchElementError)
99
end
100
end
101
102
describe '#find_elements' do
103
it 'by css' do
104
shadow_root = custom_element.shadow_root
105
elements = shadow_root.find_elements(css: 'input')
106
107
expect(elements.size).to eq 1
108
expect(elements.first).to be_a Element
109
end
110
111
it 'by xpath', except: {browser: %i[chrome edge firefox safari],
112
reason: 'https://bugs.chromium.org/p/chromedriver/issues/detail?id=4097'} do
113
shadow_root = custom_element.shadow_root
114
elements = shadow_root.find_elements(xpath: "//input[type='checkbox']")
115
116
expect(elements.size).to eq 1
117
expect(elements.first).to be_a Element
118
end
119
120
it 'by link text' do
121
shadow_root = custom_element.shadow_root
122
elements = shadow_root.find_elements(link_text: 'Example Link')
123
124
expect(elements.size).to eq 1
125
expect(elements.first).to be_a Element
126
end
127
128
it 'by partial link text' do
129
shadow_root = custom_element.shadow_root
130
elements = shadow_root.find_elements(partial_link_text: 'Link')
131
132
expect(elements.size).to eq 1
133
expect(elements.first).to be_a Element
134
end
135
136
it 'by tag name', except: {browser: %i[chrome edge firefox safari],
137
reason: 'https://bugs.chromium.org/p/chromedriver/issues/detail?id=4097'} do
138
shadow_root = custom_element.shadow_root
139
elements = shadow_root.find_elements(tag_name: 'input')
140
141
expect(elements.size).to eq 1
142
expect(elements.first).to be_a Element
143
end
144
145
it 'is empty when not found' do
146
shadow_root = custom_element.shadow_root
147
148
elements = shadow_root.find_elements(css: 'no')
149
expect(elements.size).to eq 0
150
end
151
end
152
end
153
end # WebDriver
154
end # Selenium
155
156