2021-11-16 21:05:25 +00:00
|
|
|
// Licensed to the Software Freedom Conservancy (SFC) under one
|
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
|
// distributed with this work for additional information
|
|
|
|
|
// regarding copyright ownership. The SFC licenses this file
|
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
|
// "License"); you may not use this file except in compliance
|
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing,
|
|
|
|
|
// software distributed under the License is distributed on an
|
|
|
|
|
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
|
// KIND, either express or implied. See the License for the
|
|
|
|
|
// specific language governing permissions and limitations
|
|
|
|
|
// under the License.
|
|
|
|
|
|
|
|
|
|
'use strict'
|
|
|
|
|
|
2024-04-16 01:50:58 +05:30
|
|
|
const assert = require('node:assert')
|
2024-06-27 18:33:36 +01:00
|
|
|
const { By, error } = require('selenium-webdriver')
|
2021-11-16 21:05:25 +00:00
|
|
|
const test = require('../lib/test')
|
|
|
|
|
const Pages = test.Pages
|
|
|
|
|
|
2022-06-02 23:41:17 +05:30
|
|
|
test.suite(
|
|
|
|
|
function (env) {
|
2021-11-16 21:05:25 +00:00
|
|
|
describe('ShadowRoot', function () {
|
2022-06-02 23:41:17 +05:30
|
|
|
let driver
|
2021-11-16 21:05:25 +00:00
|
|
|
|
2022-06-02 23:41:17 +05:30
|
|
|
before(async function () {
|
|
|
|
|
driver = await env.builder().build()
|
|
|
|
|
})
|
2021-11-16 21:05:25 +00:00
|
|
|
|
2022-06-02 23:41:17 +05:30
|
|
|
after(function () {
|
|
|
|
|
return driver.quit()
|
|
|
|
|
})
|
2021-11-16 21:05:25 +00:00
|
|
|
|
2022-06-02 23:41:17 +05:30
|
|
|
it('can get Shadow Root', async function () {
|
|
|
|
|
await driver.get(Pages.webComponents)
|
2024-02-07 16:07:24 +00:00
|
|
|
let element = await driver.findElement(By.css('custom-checkbox-element'))
|
2022-06-02 23:41:17 +05:30
|
|
|
await element.getShadowRoot()
|
|
|
|
|
// If an error is not thrown then test passes
|
|
|
|
|
})
|
2021-11-16 21:05:25 +00:00
|
|
|
|
2022-06-02 23:41:17 +05:30
|
|
|
it('Throws NoSuchShadowRoot when one is not attached', async function () {
|
|
|
|
|
await driver.get(Pages.simpleTestPage)
|
|
|
|
|
let element = await driver.findElement(By.css('input'))
|
2021-11-16 21:05:25 +00:00
|
|
|
|
2022-06-02 23:41:17 +05:30
|
|
|
try {
|
|
|
|
|
await element.getShadowRoot()
|
|
|
|
|
assert.fail('Error should have been thrown')
|
|
|
|
|
} catch (e) {
|
2024-02-07 16:07:24 +00:00
|
|
|
assert.ok(e instanceof error.NoSuchShadowRootError, `The error is ${typeof e}`)
|
2022-06-02 23:41:17 +05:30
|
|
|
}
|
|
|
|
|
})
|
2021-11-16 21:05:25 +00:00
|
|
|
|
2022-06-02 23:41:17 +05:30
|
|
|
it('can find element below a shadow root', async function () {
|
|
|
|
|
await driver.get(Pages.webComponents)
|
2024-02-07 16:07:24 +00:00
|
|
|
let element = await driver.findElement(By.css('custom-checkbox-element'))
|
2022-06-02 23:41:17 +05:30
|
|
|
let shadowRoot = await element.getShadowRoot()
|
|
|
|
|
await shadowRoot.findElement(By.css('input'))
|
|
|
|
|
// test passes if no error throw
|
|
|
|
|
})
|
2021-11-16 21:05:25 +00:00
|
|
|
|
2022-06-02 23:41:17 +05:30
|
|
|
it('can find elements below a shadow root', async function () {
|
|
|
|
|
await driver.get(Pages.webComponents)
|
2024-02-07 16:07:24 +00:00
|
|
|
let element = await driver.findElement(By.css('custom-checkbox-element'))
|
2022-06-02 23:41:17 +05:30
|
|
|
let shadowRoot = await element.getShadowRoot()
|
|
|
|
|
let actual = await shadowRoot.findElements(By.css('input'))
|
|
|
|
|
assert.strictEqual(actual.length, 1)
|
|
|
|
|
})
|
2021-11-16 21:05:25 +00:00
|
|
|
|
2022-06-02 23:41:17 +05:30
|
|
|
it('can return a shadowRoot from executeScript', async function () {
|
|
|
|
|
await driver.get(Pages.webComponents)
|
2024-02-07 16:07:24 +00:00
|
|
|
let element = await driver.findElement(By.css('custom-checkbox-element'))
|
2022-06-02 23:41:17 +05:30
|
|
|
let shadowRoot = await element.getShadowRoot()
|
2024-02-07 16:07:24 +00:00
|
|
|
let executeShadow = await driver.executeScript('return arguments[0].shadowRoot', element)
|
2022-06-02 23:41:17 +05:30
|
|
|
assert.strictEqual(executeShadow.getId(), shadowRoot.getId())
|
|
|
|
|
})
|
2021-11-16 21:05:25 +00:00
|
|
|
})
|
2022-06-02 23:41:17 +05:30
|
|
|
},
|
2024-02-07 16:07:24 +00:00
|
|
|
{ browsers: ['chrome'] },
|
2021-11-16 21:05:25 +00:00
|
|
|
)
|