Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/webComponent_test.js
2884 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
'use strict'
19
20
const assert = require('node:assert')
21
const { By, error } = require('selenium-webdriver')
22
const test = require('../lib/test')
23
const Pages = test.Pages
24
25
test.suite(
26
function (env) {
27
describe('ShadowRoot', function () {
28
let driver
29
30
before(async function () {
31
driver = await env.builder().build()
32
})
33
34
after(function () {
35
return driver.quit()
36
})
37
38
it('can get Shadow Root', async function () {
39
await driver.get(Pages.webComponents)
40
let element = await driver.findElement(By.css('custom-checkbox-element'))
41
await element.getShadowRoot()
42
// If an error is not thrown then test passes
43
})
44
45
it('Throws NoSuchShadowRoot when one is not attached', async function () {
46
await driver.get(Pages.simpleTestPage)
47
let element = await driver.findElement(By.css('input'))
48
49
try {
50
await element.getShadowRoot()
51
assert.fail('Error should have been thrown')
52
} catch (e) {
53
assert.ok(e instanceof error.NoSuchShadowRootError, `The error is ${typeof e}`)
54
}
55
})
56
57
it('can find element below a shadow root', async function () {
58
await driver.get(Pages.webComponents)
59
let element = await driver.findElement(By.css('custom-checkbox-element'))
60
let shadowRoot = await element.getShadowRoot()
61
await shadowRoot.findElement(By.css('input'))
62
// test passes if no error throw
63
})
64
65
it('can find elements below a shadow root', async function () {
66
await driver.get(Pages.webComponents)
67
let element = await driver.findElement(By.css('custom-checkbox-element'))
68
let shadowRoot = await element.getShadowRoot()
69
let actual = await shadowRoot.findElements(By.css('input'))
70
assert.strictEqual(actual.length, 1)
71
})
72
73
it('can return a shadowRoot from executeScript', async function () {
74
await driver.get(Pages.webComponents)
75
let element = await driver.findElement(By.css('custom-checkbox-element'))
76
let shadowRoot = await element.getShadowRoot()
77
let executeShadow = await driver.executeScript('return arguments[0].shadowRoot', element)
78
assert.strictEqual(executeShadow.getId(), shadowRoot.getId())
79
})
80
})
81
},
82
{ browsers: ['chrome'] },
83
)
84
85