Skip to content

Commit f5f2887

Browse files
committed
[js] Removing circular dependency
Between webdriver.js and http.js
1 parent afe2884 commit f5f2887

3 files changed

Lines changed: 86 additions & 33 deletions

File tree

javascript/node/selenium-webdriver/lib/http.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const error = require('./error')
3131
const logging = require('./logging')
3232
const promise = require('./promise')
3333
const { Session } = require('./session')
34-
const { WebElement } = require('./webdriver')
34+
const webElement = require('./webelement')
3535

3636
const getAttribute = requireAtom(
3737
'get-attribute.js',
@@ -462,7 +462,7 @@ const CLIENTS =
462462
class Executor {
463463
/**
464464
* @param {!(Client|IThenable<!Client>)} client The client to use for sending
465-
* requests to the server, or a promise-like object that will resolve to
465+
* requests to the server, or a promise-like object that will resolve
466466
* to the client.
467467
*/
468468
constructor(client) {
@@ -625,10 +625,10 @@ function buildPath(path, parameters) {
625625
let key = pathParameters[i].substring(2) // Trim the /:
626626
if (key in parameters) {
627627
let value = parameters[key]
628-
if (WebElement.isId(value)) {
628+
if (webElement.isId(value)) {
629629
// When inserting a WebElement into the URL, only use its ID value,
630630
// not the full JSON.
631-
value = WebElement.extractId(value)
631+
value = webElement.extractId(value)
632632
}
633633
path = path.replace(pathParameters[i], '/' + value)
634634
delete parameters[key]

javascript/node/selenium-webdriver/lib/webdriver.js

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const path = require('path')
3838
const { NoSuchElementError } = require('./error')
3939
const cdpTargets = ['page', 'browser']
4040
const Credential = require('./virtual_authenticator').Credential
41+
const webElement = require('./webelement')
4142

4243
// Capability names that are defined in the W3C spec.
4344
const W3C_CAPABILITY_NAMES = new Set([
@@ -618,17 +619,17 @@ class IWebDriver {
618619
* Takes a PDF of the current page. The driver makes a best effort to
619620
* return a PDF based on the provided parameters.
620621
*
621-
* @param {{orientation: (string|undefined),
622-
* scale: (number|undefined),
623-
* background: (boolean|undefined)
624-
* width: (number|undefined)
625-
* height: (number|undefined)
626-
* top: (number|undefined)
627-
* bottom: (number|undefined)
628-
* left: (number|undefined)
629-
* right: (number|undefined)
630-
* shrinkToFit: (boolean|undefined)
631-
* pageRanges: (<Array>|undefined)}} options.
622+
* @param {{orientation:(string|undefined),
623+
* scale:(number|undefined),
624+
* background:(boolean|undefined),
625+
* width:(number|undefined),
626+
* height:(number|undefined),
627+
* top:(number|undefined),
628+
* bottom:(number|undefined),
629+
* left:(number|undefined),
630+
* right:(number|undefined),
631+
* shrinkToFit:(boolean|undefined),
632+
* pageRanges:(Array|undefined)}} options
632633
*/
633634
printPage(options) {} // eslint-disable-line
634635
}
@@ -2382,7 +2383,7 @@ class TargetLocator {
23822383

23832384
const LEGACY_ELEMENT_ID_KEY = 'ELEMENT'
23842385
const ELEMENT_ID_KEY = 'element-6066-11e4-a52e-4f735466cecf'
2385-
const SHADOWROOT_ID_KEY = 'shadow-6066-11e4-a52e-4f735466cecf'
2386+
const SHADOW_ROOT_ID_KEY = 'shadow-6066-11e4-a52e-4f735466cecf'
23862387

23872388
/**
23882389
* Represents a DOM element. WebElements can be found by searching from the
@@ -2427,27 +2428,15 @@ class WebElement {
24272428
* @throws {TypeError} if the object is not a valid encoded ID.
24282429
*/
24292430
static extractId(obj) {
2430-
if (obj && typeof obj === 'object') {
2431-
if (typeof obj[ELEMENT_ID_KEY] === 'string') {
2432-
return obj[ELEMENT_ID_KEY]
2433-
} else if (typeof obj[LEGACY_ELEMENT_ID_KEY] === 'string') {
2434-
return obj[LEGACY_ELEMENT_ID_KEY]
2435-
}
2436-
}
2437-
throw new TypeError('object is not a WebElement ID')
2431+
return webElement.extractId(obj)
24382432
}
24392433

24402434
/**
24412435
* @param {?} obj the object to test.
24422436
* @return {boolean} whether the object is a valid encoded WebElement ID.
24432437
*/
24442438
static isId(obj) {
2445-
return (
2446-
obj &&
2447-
typeof obj === 'object' &&
2448-
(typeof obj[ELEMENT_ID_KEY] === 'string' ||
2449-
typeof obj[LEGACY_ELEMENT_ID_KEY] === 'string')
2450-
)
2439+
return webElement.isId(obj)
24512440
}
24522441

24532442
/**
@@ -2992,8 +2981,8 @@ class ShadowRoot {
29922981
*/
29932982
static extractId(obj) {
29942983
if (obj && typeof obj === 'object') {
2995-
if (typeof obj[SHADOWROOT_ID_KEY] === 'string') {
2996-
return obj[SHADOWROOT_ID_KEY]
2984+
if (typeof obj[SHADOW_ROOT_ID_KEY] === 'string') {
2985+
return obj[SHADOW_ROOT_ID_KEY]
29972986
}
29982987
}
29992988
throw new TypeError('object is not a ShadowRoot ID')
@@ -3007,7 +2996,7 @@ class ShadowRoot {
30072996
return (
30082997
obj &&
30092998
typeof obj === 'object' &&
3010-
typeof obj[SHADOWROOT_ID_KEY] === 'string'
2999+
typeof obj[SHADOW_ROOT_ID_KEY] === 'string'
30113000
)
30123001
}
30133002

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
/**
21+
* @fileoverview Defines some common methods used for WebElements.
22+
*/
23+
24+
const LEGACY_ELEMENT_ID_KEY = 'ELEMENT'
25+
const ELEMENT_ID_KEY = 'element-6066-11e4-a52e-4f735466cecf'
26+
27+
/**
28+
* Contains logic about WebElements.
29+
*/
30+
class Element {
31+
/**
32+
* @param {?} obj the object to test.
33+
* @return {boolean} whether the object is a valid encoded WebElement ID.
34+
*/
35+
static isId (obj) {
36+
return (
37+
obj &&
38+
typeof obj === 'object' &&
39+
(typeof obj[ELEMENT_ID_KEY] === 'string' ||
40+
typeof obj[LEGACY_ELEMENT_ID_KEY] === 'string')
41+
)
42+
}
43+
44+
/**
45+
* Extracts the encoded WebElement ID from the object.
46+
*
47+
* @param {?} obj The object to extract the ID from.
48+
* @return {string} the extracted ID.
49+
* @throws {TypeError} if the object is not a valid encoded ID.
50+
*/
51+
static extractId (obj) {
52+
if (obj && typeof obj === 'object') {
53+
if (typeof obj[ELEMENT_ID_KEY] === 'string') {
54+
return obj[ELEMENT_ID_KEY]
55+
} else if (typeof obj[LEGACY_ELEMENT_ID_KEY] === 'string') {
56+
return obj[LEGACY_ELEMENT_ID_KEY]
57+
}
58+
}
59+
throw new TypeError('object is not a WebElement ID')
60+
}
61+
}
62+
63+
module.exports.isId = Element.isId
64+
module.exports.extractId = Element.extractId

0 commit comments

Comments
 (0)