selenium-webdriver 4.20.0 → 4.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGES.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 4.21.0
2
+
3
+ - Add CDP for Chrome 125 and remove 122
4
+ - Ensure 'selectVisibleByText' method is same as other languages (#13899)
5
+ - Ensure parity in the locators used by methods (#13902)
6
+
1
7
  ## 4.20.0
2
8
 
3
9
  - Add CDP for Chrome 124 and remove 121
package/README.md CHANGED
@@ -218,7 +218,7 @@ under the License.
218
218
  [LTS]: https://github.com/nodejs/LTS
219
219
  [PATH]: http://en.wikipedia.org/wiki/PATH_%28variable%29
220
220
  [api]: http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/
221
- [chrome]: http://chromedriver.storage.googleapis.com/index.html
221
+ [chrome]: https://googlechromelabs.github.io/chrome-for-testing/#stable
222
222
  [gh]: https://github.com/SeleniumHQ/selenium/
223
223
  [issues]: https://github.com/SeleniumHQ/selenium/issues
224
224
  [edge]: http://go.microsoft.com/fwlink/?LinkId=619687
@@ -500,21 +500,18 @@ class BrowsingContext {
500
500
  *
501
501
  * @param {Locator} locator - The locator object used to locate the nodes.
502
502
  * @param {number} [maxNodeCount] - The maximum number of nodes to locate (optional).
503
- * @param {string} [ownership] - The ownership type of the nodes (optional).
504
503
  * @param {string} [sandbox] - The sandbox name for locating nodes (optional).
505
504
  * @param {SerializationOptions} [serializationOptions] - The serialization options for locating nodes (optional).
506
505
  * @param {ReferenceValue[]} [startNodes] - The array of start nodes for locating nodes (optional).
507
506
  * @returns {Promise<RemoteValue[]>} - A promise that resolves to the arrays of located nodes.
508
507
  * @throws {Error} - If the locator is not an instance of Locator.
509
508
  * @throws {Error} - If the serializationOptions is provided but not an instance of SerializationOptions.
510
- * @throws {Error} - If the ownership is provided but not 'root' or 'none'.
511
509
  * @throws {Error} - If the startNodes is provided but not an array of ReferenceValue objects.
512
510
  * @throws {Error} - If any of the startNodes is not an instance of ReferenceValue.
513
511
  */
514
512
  async locateNodes(
515
513
  locator,
516
514
  maxNodeCount = undefined,
517
- ownership = undefined,
518
515
  sandbox = undefined,
519
516
  serializationOptions = undefined,
520
517
  startNodes = undefined,
@@ -527,10 +524,6 @@ class BrowsingContext {
527
524
  throw Error(`Pass in SerializationOptions object. Received: ${serializationOptions} `)
528
525
  }
529
526
 
530
- if (ownership !== undefined && !['root', 'none'].includes(ownership)) {
531
- throw Error(`Valid types are 'root' and 'none. Received: ${ownership}`)
532
- }
533
-
534
527
  if (startNodes !== undefined && !Array.isArray(startNodes)) {
535
528
  throw Error(`Pass in an array of ReferenceValue objects. Received: ${startNodes}`)
536
529
  }
@@ -549,7 +542,6 @@ class BrowsingContext {
549
542
  context: this._id,
550
543
  locator: Object.fromEntries(locator.toMap()),
551
544
  maxNodeCount: maxNodeCount,
552
- ownership: ownership,
553
545
  sandbox: sandbox,
554
546
  serializationOptions: serializationOptions,
555
547
  startNodes: startNodes,
@@ -574,20 +566,13 @@ class BrowsingContext {
574
566
  * Locates a single node in the browsing context.
575
567
  *
576
568
  * @param {Locator} locator - The locator used to find the node.
577
- * @param {string} [ownership] - The ownership of the node (optional).
578
569
  * @param {string} [sandbox] - The sandbox of the node (optional).
579
570
  * @param {SerializationOptions} [serializationOptions] - The serialization options for the node (optional).
580
571
  * @param {Array} [startNodes] - The starting nodes for the search (optional).
581
572
  * @returns {Promise<RemoteValue>} - A promise that resolves to the located node.
582
573
  */
583
- async locateNode(
584
- locator,
585
- ownership = undefined,
586
- sandbox = undefined,
587
- serializationOptions = undefined,
588
- startNodes = undefined,
589
- ) {
590
- const elements = await this.locateNodes(locator, 1, ownership, sandbox, serializationOptions, startNodes)
574
+ async locateNode(locator, sandbox = undefined, serializationOptions = undefined, startNodes = undefined) {
575
+ const elements = await this.locateNodes(locator, 1, sandbox, serializationOptions, startNodes)
591
576
  return elements[0]
592
577
  }
593
578
 
Binary file
Binary file
Binary file
package/lib/select.js CHANGED
@@ -36,7 +36,7 @@
36
36
 
37
37
  'use strict'
38
38
 
39
- const { By, escapeCss } = require('./by')
39
+ const { By } = require('./by')
40
40
  const error = require('./error')
41
41
 
42
42
  /**
@@ -146,6 +146,10 @@ class Select {
146
146
  * @param {WebElement} element Select WebElement.
147
147
  */
148
148
  constructor(element) {
149
+ if (element === null) {
150
+ throw new Error(`Element must not be null. Please provide a valid <select> element.`)
151
+ }
152
+
149
153
  this.element = element
150
154
 
151
155
  this.element.getAttribute('tagName').then(function (tagName) {
@@ -153,6 +157,10 @@ class Select {
153
157
  throw new Error(`Select only works on <select> elements`)
154
158
  }
155
159
  })
160
+
161
+ this.element.getAttribute('multiple').then((multiple) => {
162
+ this.multiple = multiple !== null && multiple !== 'false'
163
+ })
156
164
  }
157
165
 
158
166
  /**
@@ -216,9 +224,7 @@ class Select {
216
224
  let matched = false
217
225
  let isMulti = await this.isMultiple()
218
226
 
219
- let options = await this.element.findElements({
220
- css: 'option[value =' + escapeCss(value) + ']',
221
- })
227
+ let options = await this.element.findElements(By.xpath('.//option[@value = ' + escapeQuotes(value) + ']'))
222
228
 
223
229
  for (let option of options) {
224
230
  await this.setSelected(option)
@@ -254,30 +260,46 @@ class Select {
254
260
  async selectByVisibleText(text) {
255
261
  text = typeof text === 'number' ? text.toString() : text
256
262
 
257
- const normalized = text
258
- .trim() // strip leading and trailing white-space characters
259
- .replace(/\s+/, ' ') // replace sequences of whitespace characters by a single space
263
+ const xpath = './/option[normalize-space(.) = ' + escapeQuotes(text) + ']'
260
264
 
261
- /**
262
- * find option element using xpath
263
- */
264
- const formatted = /"/.test(normalized)
265
- ? 'concat("' + normalized.split('"').join('", \'"\', "') + '")'
266
- : `"${normalized}"`
267
- const dotFormat = `[. = ${formatted}]`
268
- const spaceFormat = `[normalize-space(text()) = ${formatted}]`
269
-
270
- const selections = [
271
- `./option${dotFormat}`,
272
- `./option${spaceFormat}`,
273
- `./optgroup/option${dotFormat}`,
274
- `./optgroup/option${spaceFormat}`,
275
- ]
276
-
277
- const optionElement = await this.element.findElement({
278
- xpath: selections.join('|'),
279
- })
280
- await this.setSelected(optionElement)
265
+ const options = await this.element.findElements(By.xpath(xpath))
266
+
267
+ for (let option of options) {
268
+ await this.setSelected(option)
269
+ if (!(await this.isMultiple())) {
270
+ return
271
+ }
272
+ }
273
+
274
+ let matched = Array.isArray(options) && options.length > 0
275
+
276
+ if (!matched && text.includes(' ')) {
277
+ const subStringWithoutSpace = getLongestSubstringWithoutSpace(text)
278
+ let candidates
279
+ if ('' === subStringWithoutSpace) {
280
+ candidates = await this.element.findElements(By.tagName('option'))
281
+ } else {
282
+ const xpath = './/option[contains(., ' + escapeQuotes(subStringWithoutSpace) + ')]'
283
+ candidates = await this.element.findElements(By.xpath(xpath))
284
+ }
285
+
286
+ const trimmed = text.trim()
287
+
288
+ for (let option of candidates) {
289
+ const optionText = await option.getText()
290
+ if (trimmed === optionText.trim()) {
291
+ await this.setSelected(option)
292
+ if (!(await this.isMultiple())) {
293
+ return
294
+ }
295
+ matched = true
296
+ }
297
+ }
298
+ }
299
+
300
+ if (!matched) {
301
+ throw new Error(`Cannot locate option with text: ${text}`)
302
+ }
281
303
  }
282
304
 
283
305
  /**
@@ -293,7 +315,7 @@ class Select {
293
315
  * @returns {Promise<boolean>}
294
316
  */
295
317
  async isMultiple() {
296
- return (await this.element.getAttribute('multiple')) !== null
318
+ return this.multiple
297
319
  }
298
320
 
299
321
  /**
@@ -353,29 +375,9 @@ class Select {
353
375
  */
354
376
  text = typeof text === 'number' ? text.toString() : text
355
377
 
356
- const normalized = text
357
- .trim() // strip leading and trailing white-space characters
358
- .replace(/\s+/, ' ') // replace sequences of whitespace characters by a single space
359
-
360
- /**
361
- * find option element using xpath
362
- */
363
- const formatted = /"/.test(normalized)
364
- ? 'concat("' + normalized.split('"').join('", \'"\', "') + '")'
365
- : `"${normalized}"`
366
- const dotFormat = `[. = ${formatted}]`
367
- const spaceFormat = `[normalize-space(text()) = ${formatted}]`
368
-
369
- const selections = [
370
- `./option${dotFormat}`,
371
- `./option${spaceFormat}`,
372
- `./optgroup/option${dotFormat}`,
373
- `./optgroup/option${spaceFormat}`,
374
- ]
375
-
376
- const optionElement = await this.element.findElement({
377
- xpath: selections.join('|'),
378
- })
378
+ const optionElement = await this.element.findElement(
379
+ By.xpath('.//option[normalize-space(.) = ' + escapeQuotes(text) + ']'),
380
+ )
379
381
  if (await optionElement.isSelected()) {
380
382
  await optionElement.click()
381
383
  }
@@ -431,9 +433,11 @@ class Select {
431
433
 
432
434
  let matched = false
433
435
 
434
- let options = await this.element.findElements({
435
- css: 'option[value =' + escapeCss(value) + ']',
436
- })
436
+ let options = await this.element.findElements(By.xpath('.//option[@value = ' + escapeQuotes(value) + ']'))
437
+
438
+ if (options.length === 0) {
439
+ throw new Error(`Cannot locate option with value: ${value}`)
440
+ }
437
441
 
438
442
  for (let option of options) {
439
443
  if (await option.isSelected()) {
@@ -457,4 +461,42 @@ class Select {
457
461
  }
458
462
  }
459
463
 
460
- module.exports = { Select }
464
+ function escapeQuotes(toEscape) {
465
+ if (toEscape.includes(`"`) && toEscape.includes(`'`)) {
466
+ const quoteIsLast = toEscape.lastIndexOf(`"`) === toEscape.length - 1
467
+ const substrings = toEscape.split(`"`)
468
+
469
+ // Remove the last element if it's an empty string
470
+ if (substrings[substrings.length - 1] === '') {
471
+ substrings.pop()
472
+ }
473
+
474
+ let result = 'concat('
475
+
476
+ for (let i = 0; i < substrings.length; i++) {
477
+ result += `"${substrings[i]}"`
478
+ result += i === substrings.length - 1 ? (quoteIsLast ? `, '"')` : `)`) : `, '"', `
479
+ }
480
+ return result
481
+ }
482
+
483
+ if (toEscape.includes('"')) {
484
+ return `'${toEscape}'`
485
+ }
486
+
487
+ // Otherwise return the quoted string
488
+ return `"${toEscape}"`
489
+ }
490
+
491
+ function getLongestSubstringWithoutSpace(text) {
492
+ let words = text.split(' ')
493
+ let longestString = ''
494
+ for (let word of words) {
495
+ if (word.length > longestString.length) {
496
+ longestString = word
497
+ }
498
+ }
499
+ return longestString
500
+ }
501
+
502
+ module.exports = { Select, escapeQuotes }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "selenium-webdriver",
3
- "version": "4.20.0",
3
+ "version": "4.21.0",
4
4
  "description": "The official WebDriver JavaScript bindings from the Selenium project",
5
5
  "license": "Apache-2.0",
6
6
  "keywords": [
@@ -20,7 +20,7 @@
20
20
  "url": "https://github.com/SeleniumHQ/selenium.git"
21
21
  },
22
22
  "engines": {
23
- "node": ">= 14.20.0"
23
+ "node": ">= 14.21.0"
24
24
  },
25
25
  "dependencies": {
26
26
  "jszip": "^3.10.1",