Skip to content

Commit 139cc06

Browse files
authored
Merge pull request #2406 from teamcapybara/shadow_dom_path
Detect shadow dom elements when calling `path`
2 parents 13bf109 + 2ec2a2f commit 139cc06

File tree

6 files changed

+23
-2
lines changed

6 files changed

+23
-2
lines changed

lib/capybara/selenium/node.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,9 @@ def attrs(*attr_names)
493493
var xpath = '';
494494
var pos, tempitem2;
495495
496+
if (el.getRootNode && el.getRootNode() instanceof ShadowRoot) {
497+
return ": Shadow DOM element - no XPath :"
498+
};
496499
while(el !== xml.documentElement) {
497500
pos = 0;
498501
tempitem2 = el;

lib/capybara/spec/public/test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,5 +271,10 @@ $(function() {
271271
});
272272
$('#multiple-file, #hidden_file').change(function(e){
273273
$('body').append($('<p class="file_change">File input changed</p>'));
274-
})
274+
});
275+
276+
var shadow = document.querySelector('#shadow').attachShadow({mode: 'open'});
277+
var span = document.createElement('span');
278+
span.textContent = 'The things we do in the shadows';
279+
shadow.appendChild(span);
275280
});

lib/capybara/spec/session/node_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,17 @@
409409
element = @session.find(:link, 'Second Link')
410410
expect(@session.find(:xpath, element.path)).to eq(element)
411411
end
412+
413+
it 'reports when element in shadow dom', requires: [:shadow_dom] do
414+
@session.visit('/with_js')
415+
shadow = @session.find(:css, '#shadow')
416+
element = @session.evaluate_script(<<~JS, shadow)
417+
(function(root){
418+
return root.shadowRoot.querySelector('span');
419+
})(arguments[0])
420+
JS
421+
expect(element.path).to eq ": Shadow DOM element - no XPath :"
422+
end
412423
end
413424

414425
describe '#trigger', requires: %i[js trigger] do

lib/capybara/spec/views/with_js.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
<p>This is an HTML5 draggable element.</p>
153153
</div>
154154

155+
<div id="shadow"></div>
155156
<script type="text/javascript">
156157
// a javascript comment
157158
var aVar = 123;

spec/dsl_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class TestClass
88
end
99

1010
Capybara::SpecHelper.run_specs TestClass.new, 'DSL', capybara_skip: %i[
11-
js modals screenshot frames windows send_keys server hover about_scheme psc download css driver scroll spatial html_validation
11+
js modals screenshot frames windows send_keys server hover about_scheme psc download css driver scroll spatial html_validation shadow_dom
1212
] do |example|
1313
case example.metadata[:full_description]
1414
when /has_css\? should support case insensitive :class and :id options/

spec/rack_test_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module TestSessions
2727
scroll
2828
spatial
2929
html_validation
30+
shadow_dom
3031
]
3132
Capybara::SpecHelper.run_specs TestSessions::RackTest, 'RackTest', capybara_skip: skipped_tests do |example|
3233
case example.metadata[:full_description]

0 commit comments

Comments
 (0)