Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/bidi/locate_nodes_test.js
2885 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 { Browser } = require('selenium-webdriver')
22
const { Pages, suite, ignore } = require('../../lib/test')
23
const BrowsingContext = require('selenium-webdriver/bidi/browsingContext')
24
const { Locator } = require('selenium-webdriver/bidi/browsingContext')
25
const { ScriptManager } = require('selenium-webdriver/index')
26
const { EvaluateResultType } = require('selenium-webdriver/bidi/evaluateResult')
27
const { LocalValue, ReferenceValue } = require('selenium-webdriver/bidi/protocolValue')
28
29
suite(
30
function (env) {
31
let driver
32
33
beforeEach(async function () {
34
driver = await env.builder().build()
35
})
36
37
afterEach(async function () {
38
await driver.quit()
39
})
40
41
describe('Locate Nodes', function () {
42
it('can locate nodes', async function () {
43
const id = await driver.getWindowHandle()
44
const browsingContext = await BrowsingContext(driver, {
45
browsingContextId: id,
46
})
47
48
await driver.get(Pages.xhtmlTestPage)
49
50
const element = await browsingContext.locateNodes(Locator.css('div'))
51
assert.strictEqual(element.length, 13)
52
})
53
54
it('can locate node', async function () {
55
const id = await driver.getWindowHandle()
56
const browsingContext = await BrowsingContext(driver, {
57
browsingContextId: id,
58
})
59
60
await driver.get(Pages.xhtmlTestPage)
61
62
const element = await browsingContext.locateNode(Locator.css('div'))
63
assert.strictEqual(element.type, 'node')
64
})
65
66
it('can locate node with css locator', async function () {
67
const id = await driver.getWindowHandle()
68
const browsingContext = await BrowsingContext(driver, {
69
browsingContextId: id,
70
})
71
72
await driver.get(Pages.xhtmlTestPage)
73
74
const elements = await browsingContext.locateNodes(Locator.css('div.extraDiv, div.content'), 1)
75
const element = elements[0]
76
assert.strictEqual(element.type, 'node')
77
assert.notEqual(element.value, undefined)
78
assert.strictEqual(element.value.localName, 'div')
79
assert.strictEqual(element.value.attributes.class, 'content')
80
assert.notEqual(element.sharedId, undefined)
81
})
82
83
it('can locate node with xpath locator', async function () {
84
const id = await driver.getWindowHandle()
85
const browsingContext = await BrowsingContext(driver, {
86
browsingContextId: id,
87
})
88
89
await driver.get(Pages.xhtmlTestPage)
90
const elements = await browsingContext.locateNodes(Locator.xpath('/html/body/div[2]'), 1)
91
92
const element = elements[0]
93
assert.strictEqual(element.type, 'node')
94
assert.notEqual(element.value, undefined)
95
assert.strictEqual(element.value.localName, 'div')
96
assert.strictEqual(element.value.attributes.class, 'content')
97
assert.notEqual(element.sharedId, undefined)
98
})
99
100
ignore(env.browsers(Browser.FIREFOX)).it('can locate node with inner test locator', async function () {
101
const id = await driver.getWindowHandle()
102
const browsingContext = await BrowsingContext(driver, {
103
browsingContextId: id,
104
})
105
106
await driver.get(Pages.xhtmlTestPage)
107
const elements = await browsingContext.locateNodes(Locator.innerText('Spaced out'), 1)
108
109
const element = elements[0]
110
assert.strictEqual(element.type, 'node')
111
assert.notEqual(element.value, undefined)
112
})
113
114
it('can locate node with max node count', async function () {
115
const id = await driver.getWindowHandle()
116
const browsingContext = await BrowsingContext(driver, {
117
browsingContextId: id,
118
})
119
120
await driver.get(Pages.xhtmlTestPage)
121
122
const elements = await browsingContext.locateNodes(Locator.css('div'), 4)
123
assert.strictEqual(elements.length, 4)
124
})
125
126
it('can locate node with given start nodes', async function () {
127
const id = await driver.getWindowHandle()
128
const browsingContext = await BrowsingContext(driver, {
129
browsingContextId: id,
130
})
131
132
await driver.get(Pages.formPage)
133
134
const script = await ScriptManager(id, driver)
135
136
const result = await script.evaluateFunctionInBrowsingContext(
137
id,
138
"document.querySelectorAll('form')",
139
false,
140
'root',
141
)
142
143
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
144
assert.notEqual(result.realmId, null)
145
assert.equal(result.result.type, 'nodelist')
146
147
const value = result.result.value
148
149
const startNodes = []
150
151
value.forEach((node) => {
152
startNodes.push(new ReferenceValue(node.handle, node.sharedId))
153
})
154
155
const elements = await browsingContext.locateNodes(Locator.css('input'), 50, 'none', undefined, startNodes)
156
157
assert.strictEqual(elements.length, 35)
158
})
159
160
it('can locate nodes in a given sandbox', async function () {
161
const sandbox = 'sandbox'
162
const id = await driver.getWindowHandle()
163
const browsingContext = await BrowsingContext(driver, {
164
browsingContextId: id,
165
})
166
167
await browsingContext.navigate(Pages.xhtmlTestPage, 'complete')
168
169
const elements = await browsingContext.locateNodes(Locator.css('div'), 1, sandbox)
170
171
assert.strictEqual(elements.length, 1)
172
173
const nodeId = elements[0].sharedId
174
175
const script = await ScriptManager(id, driver)
176
177
let argumentValues = []
178
let mapValue = { sharedId: LocalValue.createStringValue(nodeId) }
179
argumentValues.push(LocalValue.createMapValue(mapValue))
180
181
const response = await script.callFunctionInBrowsingContext(
182
id,
183
'function(){ return arguments[0]; }',
184
false,
185
argumentValues,
186
undefined,
187
undefined,
188
sandbox,
189
)
190
191
assert.equal(response.resultType, EvaluateResultType.SUCCESS)
192
assert.equal(response.result.type, 'map')
193
194
const value = response.result.value[0]
195
196
assert.strictEqual(value[1].type, 'string')
197
assert.strictEqual(value[1].value, nodeId)
198
})
199
200
it('can find element', async function () {
201
const id = await driver.getWindowHandle()
202
const browsingContext = await BrowsingContext(driver, {
203
browsingContextId: id,
204
})
205
206
await driver.get(Pages.xhtmlTestPage)
207
208
const element = await browsingContext.locateElement(Locator.css('p'))
209
const elementText = await element.getText()
210
assert.strictEqual(elementText, 'Open new window')
211
})
212
213
it('can find elements', async function () {
214
const id = await driver.getWindowHandle()
215
const browsingContext = await BrowsingContext(driver, {
216
browsingContextId: id,
217
})
218
219
await driver.get(Pages.xhtmlTestPage)
220
221
const elements = await browsingContext.locateElements(Locator.css('div'))
222
assert.strictEqual(elements.length, 13)
223
224
const elementText = await elements[0].getText()
225
assert.strictEqual(elementText.includes('Open new window'), true)
226
})
227
})
228
},
229
{ browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] },
230
)
231
232