Selenium: Raise clear error when trying to get visible text on a shadow root

This commit is contained in:
Thomas Walpole 2022-10-17 17:33:24 -07:00
parent 957e8d9da9
commit 1899120224
9 changed files with 34 additions and 2 deletions

View File

@ -10,6 +10,8 @@ class Capybara::Selenium::Node < Capybara::Driver::Node
include Capybara::Selenium::Scroll
def visible_text
raise NotImplementedError, 'Getting visible text is not currently supported directly on shadow roots' if shadow_root?
native.text
end
@ -182,7 +184,7 @@ class Capybara::Selenium::Node < Capybara::Driver::Node
if native.respond_to? :tag_name
native.tag_name.downcase
else
native.is_a?(::Selenium::WebDriver::ShadowRoot) ? 'ShadowRoot' : 'Unknown'
shadow_root? ? 'ShadowRoot' : 'Unknown'
end
end
@ -533,6 +535,10 @@ private
id || type_or_id
end
def shadow_root?
defined?(::Selenium::WebDriver::ShadowRoot) && native.is_a?(::Selenium::WebDriver::ShadowRoot)
end
GET_XPATH_SCRIPT = <<~'JS'
(function(el, xml){
var xpath = '';

View File

@ -1220,13 +1220,25 @@ Capybara::SpecHelper.spec 'node' do
expect(descendant).to have_checked_field('shadow_checkbox')
end
it 'should produce error messages when failing', :focus_ do
it 'should produce error messages when failing' do
@session.visit('/with_shadow')
shadow_root = @session.find(:css, '#shadow_host').shadow_root
expect do
expect(shadow_root).to have_css('#shadow_content', text: 'Not in the document')
end.to raise_error(/tag="ShadowRoot"/)
end
it 'should get visible text' do
@session.visit('/with_shadow')
shadow_root = @session.find(:css, '#shadow_host').shadow_root
expect(shadow_root).to have_text('some text scroll.html')
end
it 'should get all text' do
@session.visit('/with_shadow')
shadow_root = @session.find(:css, '#shadow_host').shadow_root
expect(shadow_root).to have_text(:all, 'some text scroll.html')
end
end
describe '#reload', requires: [:js] do

View File

@ -85,6 +85,8 @@ Capybara::SpecHelper.run_specs TestSessions::Chrome, CHROME_DRIVER.to_s, capybar
skip 'Need to figure out testing of file downloading on windows platform' if Gem.win_platform?
when /Capybara::Session selenium_chrome Capybara::Window#maximize/
pending "Chrome headless doesn't support maximize" if ENV['HEADLESS']
when /Capybara::Session selenium_chrome node #shadow_root should get visible text/
pending "Selenium doesn't currently support getting visible text for shadow root elements"
end
end

View File

@ -62,6 +62,8 @@ Capybara::SpecHelper.run_specs TestSessions::Chrome, CHROME_REMOTE_DRIVER.to_s,
'Capybara::Session selenium_chrome_remote #attach_file with multipart form should fire change once for each set of files uploaded',
'Capybara::Session selenium_chrome_remote #attach_file with multipart form should fire change once when uploading multiple files from empty'
pending "Selenium with Remote Chrome doesn't support multiple file upload" unless selenium_gte?(3.14)
when /Capybara::Session selenium_chrome node #shadow_root should get visible text/
pending "Selenium doesn't currently support getting visible text for shadow root elements"
end
end

View File

@ -36,6 +36,8 @@ Capybara::SpecHelper.run_specs TestSessions::SeleniumEdge, 'selenium', capybara_
case example.metadata[:full_description]
when 'Capybara::Session selenium #attach_file with a block can upload by clicking the file input'
pending "EdgeChrome doesn't allow clicking on file inputs"
when /Capybara::Session selenium_chrome node #shadow_root should get visible text/
pending "Selenium doesn't currently support getting visible text for shadow root elements"
end
end

View File

@ -80,6 +80,8 @@ Capybara::SpecHelper.run_specs TestSessions::SeleniumFirefox, 'selenium', capyba
pending "Firefox doesn't yet have full W3C shadow root support"
when 'Capybara::Session selenium #fill_in should handle carriage returns with line feeds in a textarea correctly'
pending 'Not sure what firefox is doing here'
when /Capybara::Session selenium_chrome node #shadow_root should get visible text/
pending "Selenium doesn't currently support getting visible text for shadow root elements"
end
end

View File

@ -65,6 +65,8 @@ Capybara::SpecHelper.run_specs TestSessions::RemoteFirefox, FIREFOX_REMOTE_DRIVE
when /#accept_confirm should work with nested modals$/
# skip because this is timing based and hence flaky when set to pending
skip 'Broken in FF 63 - https://bugzilla.mozilla.org/show_bug.cgi?id=1487358' if firefox_gte?(63, @session)
when /Capybara::Session selenium_chrome node #shadow_root should get visible text/
pending "Selenium doesn't currently support getting visible text for shadow root elements"
end
end

View File

@ -108,6 +108,8 @@ Capybara::SpecHelper.run_specs TestSessions::SeleniumIE, 'selenium', capybara_sk
skip "IE doesn't support template elements"
when /Element#drop/
pending "IE doesn't support DataTransfer constructor"
when /Capybara::Session selenium_chrome node #shadow_root should get visible text/
pending "Selenium doesn't currently support getting visible text for shadow root elements"
end
end

View File

@ -88,6 +88,8 @@ Capybara::SpecHelper.run_specs TestSessions::Safari, SAFARI_DRIVER.to_s, capybar
'Capybara::Session selenium node #shadow_root should find elements inside the shadow dom using CSS',
'Capybara::Session selenium node #shadow_root should find nested shadow roots'
pending "Safari doesn't yet have W3C shadow root support"
when /Capybara::Session selenium_chrome node #shadow_root should get visible text/
pending "Selenium doesn't currently support getting visible text for shadow root elements"
end
end