Path: blob/trunk/rb/spec/integration/selenium/webdriver/shadow_root_spec.rb
1864 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 ShadowRoot, exclusive: [{bidi: false, reason: 'Not yet implemented with BiDi'},24{browser: %i[chrome firefox edge safari]}] do25before { driver.navigate.to url_for('webComponents.html') }2627let(:custom_element) { driver.find_element(css: 'custom-checkbox-element') }2829it 'gets shadow root from driver' do30shadow_root = custom_element.shadow_root31expect(shadow_root).to be_a described_class32end3334it 'raises error if no shadow root', exclude: {browser: :safari, reason: 'NoMethodError'} do35driver.navigate.to url_for('simpleTest.html')36div = driver.find_element(css: 'div')37expect { div.shadow_root }.to raise_error(Error::NoSuchShadowRootError)38end3940it 'raises error if the shadow root is detached', exclude: {browser: :safari, reason: 'NoMethodError'} do41driver.navigate.to url_for('simpleTest.html')42div = driver.find_element(css: 'div')43driver.execute_script('arguments[0].attachShadow({ mode: "open" });', div)44root = div.shadow_root45driver.execute_script('arguments[0].remove();', div)46expect { root.find_element(css: '#x') }.to raise_error(Error::DetachedShadowRootError)47end4849it 'gets shadow root from script',50exclude: {browser: :safari, reason: 'returns correct node, but references shadow root as a element'} do51shadow_root = custom_element.shadow_root52execute_shadow_root = driver.execute_script('return arguments[0].shadowRoot;', custom_element)53expect(execute_shadow_root).to eq shadow_root54end5556describe '#find_element' do57it 'by css' do58shadow_root = custom_element.shadow_root59element = shadow_root.find_element(css: 'input')6061expect(element).to be_a Element62end6364it 'by xpath', except: {browser: %i[chrome edge firefox safari],65reason: 'https://bugs.chromium.org/p/chromedriver/issues/detail?id=4097'} do66shadow_root = custom_element.shadow_root67element = shadow_root.find_element(xpath: "//input[type='checkbox']")6869expect(element).to be_a Element70end7172it 'by link text' do73shadow_root = custom_element.shadow_root74element = shadow_root.find_element(link_text: 'Example Link')7576expect(element).to be_a Element77end7879it 'by partial link text' do80shadow_root = custom_element.shadow_root81element = shadow_root.find_element(partial_link_text: 'Link')8283expect(element).to be_a Element84end8586it 'by tag name', except: {browser: %i[chrome edge firefox safari],87reason: 'https://bugs.chromium.org/p/chromedriver/issues/detail?id=4097'} do88shadow_root = custom_element.shadow_root89element = shadow_root.find_element(tag_name: 'input')9091expect(element).to be_a Element92end9394it 'raises error if not found' do95shadow_root = custom_element.shadow_root9697expect { shadow_root.find_element(css: 'no') }.to raise_error(Error::NoSuchElementError)98end99end100101describe '#find_elements' do102it 'by css' do103shadow_root = custom_element.shadow_root104elements = shadow_root.find_elements(css: 'input')105106expect(elements.size).to eq 1107expect(elements.first).to be_a Element108end109110it 'by xpath', except: {browser: %i[chrome edge firefox safari],111reason: 'https://bugs.chromium.org/p/chromedriver/issues/detail?id=4097'} do112shadow_root = custom_element.shadow_root113elements = shadow_root.find_elements(xpath: "//input[type='checkbox']")114115expect(elements.size).to eq 1116expect(elements.first).to be_a Element117end118119it 'by link text' do120shadow_root = custom_element.shadow_root121elements = shadow_root.find_elements(link_text: 'Example Link')122123expect(elements.size).to eq 1124expect(elements.first).to be_a Element125end126127it 'by partial link text' do128shadow_root = custom_element.shadow_root129elements = shadow_root.find_elements(partial_link_text: 'Link')130131expect(elements.size).to eq 1132expect(elements.first).to be_a Element133end134135it 'by tag name', except: {browser: %i[chrome edge firefox safari],136reason: 'https://bugs.chromium.org/p/chromedriver/issues/detail?id=4097'} do137shadow_root = custom_element.shadow_root138elements = shadow_root.find_elements(tag_name: 'input')139140expect(elements.size).to eq 1141expect(elements.first).to be_a Element142end143144it 'is empty when not found' do145shadow_root = custom_element.shadow_root146147elements = shadow_root.find_elements(css: 'no')148expect(elements.size).to eq 0149end150end151end152end # WebDriver153end # Selenium154155156