Skip to content

Commit

Permalink
[js] Removing circular dependency
Browse files Browse the repository at this point in the history
Between webdriver.js and http.js
  • Loading branch information
diemol committed Aug 11, 2022
1 parent afe2884 commit f5f2887
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 33 deletions.
8 changes: 4 additions & 4 deletions javascript/node/selenium-webdriver/lib/http.js
Expand Up @@ -31,7 +31,7 @@ const error = require('./error')
const logging = require('./logging')
const promise = require('./promise')
const { Session } = require('./session')
const { WebElement } = require('./webdriver')
const webElement = require('./webelement')

const getAttribute = requireAtom(
'get-attribute.js',
Expand Down Expand Up @@ -462,7 +462,7 @@ const CLIENTS =
class Executor {
/**
* @param {!(Client|IThenable<!Client>)} client The client to use for sending
* requests to the server, or a promise-like object that will resolve to
* requests to the server, or a promise-like object that will resolve
* to the client.
*/
constructor(client) {
Expand Down Expand Up @@ -625,10 +625,10 @@ function buildPath(path, parameters) {
let key = pathParameters[i].substring(2) // Trim the /:
if (key in parameters) {
let value = parameters[key]
if (WebElement.isId(value)) {
if (webElement.isId(value)) {
// When inserting a WebElement into the URL, only use its ID value,
// not the full JSON.
value = WebElement.extractId(value)
value = webElement.extractId(value)
}
path = path.replace(pathParameters[i], '/' + value)
delete parameters[key]
Expand Down
47 changes: 18 additions & 29 deletions javascript/node/selenium-webdriver/lib/webdriver.js
Expand Up @@ -38,6 +38,7 @@ const path = require('path')
const { NoSuchElementError } = require('./error')
const cdpTargets = ['page', 'browser']
const Credential = require('./virtual_authenticator').Credential
const webElement = require('./webelement')

// Capability names that are defined in the W3C spec.
const W3C_CAPABILITY_NAMES = new Set([
Expand Down Expand Up @@ -618,17 +619,17 @@ class IWebDriver {
* Takes a PDF of the current page. The driver makes a best effort to
* return a PDF based on the provided parameters.
*
* @param {{orientation: (string|undefined),
* scale: (number|undefined),
* background: (boolean|undefined)
* width: (number|undefined)
* height: (number|undefined)
* top: (number|undefined)
* bottom: (number|undefined)
* left: (number|undefined)
* right: (number|undefined)
* shrinkToFit: (boolean|undefined)
* pageRanges: (<Array>|undefined)}} options.
* @param {{orientation:(string|undefined),
* scale:(number|undefined),
* background:(boolean|undefined),
* width:(number|undefined),
* height:(number|undefined),
* top:(number|undefined),
* bottom:(number|undefined),
* left:(number|undefined),
* right:(number|undefined),
* shrinkToFit:(boolean|undefined),
* pageRanges:(Array|undefined)}} options
*/
printPage(options) {} // eslint-disable-line
}
Expand Down Expand Up @@ -2382,7 +2383,7 @@ class TargetLocator {

const LEGACY_ELEMENT_ID_KEY = 'ELEMENT'
const ELEMENT_ID_KEY = 'element-6066-11e4-a52e-4f735466cecf'
const SHADOWROOT_ID_KEY = 'shadow-6066-11e4-a52e-4f735466cecf'
const SHADOW_ROOT_ID_KEY = 'shadow-6066-11e4-a52e-4f735466cecf'

/**
* Represents a DOM element. WebElements can be found by searching from the
Expand Down Expand Up @@ -2427,27 +2428,15 @@ class WebElement {
* @throws {TypeError} if the object is not a valid encoded ID.
*/
static extractId(obj) {
if (obj && typeof obj === 'object') {
if (typeof obj[ELEMENT_ID_KEY] === 'string') {
return obj[ELEMENT_ID_KEY]
} else if (typeof obj[LEGACY_ELEMENT_ID_KEY] === 'string') {
return obj[LEGACY_ELEMENT_ID_KEY]
}
}
throw new TypeError('object is not a WebElement ID')
return webElement.extractId(obj)
}

/**
* @param {?} obj the object to test.
* @return {boolean} whether the object is a valid encoded WebElement ID.
*/
static isId(obj) {
return (
obj &&
typeof obj === 'object' &&
(typeof obj[ELEMENT_ID_KEY] === 'string' ||
typeof obj[LEGACY_ELEMENT_ID_KEY] === 'string')
)
return webElement.isId(obj)
}

/**
Expand Down Expand Up @@ -2992,8 +2981,8 @@ class ShadowRoot {
*/
static extractId(obj) {
if (obj && typeof obj === 'object') {
if (typeof obj[SHADOWROOT_ID_KEY] === 'string') {
return obj[SHADOWROOT_ID_KEY]
if (typeof obj[SHADOW_ROOT_ID_KEY] === 'string') {
return obj[SHADOW_ROOT_ID_KEY]
}
}
throw new TypeError('object is not a ShadowRoot ID')
Expand All @@ -3007,7 +2996,7 @@ class ShadowRoot {
return (
obj &&
typeof obj === 'object' &&
typeof obj[SHADOWROOT_ID_KEY] === 'string'
typeof obj[SHADOW_ROOT_ID_KEY] === 'string'
)
}

Expand Down
64 changes: 64 additions & 0 deletions javascript/node/selenium-webdriver/lib/webelement.js
@@ -0,0 +1,64 @@
// 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'

/**
* @fileoverview Defines some common methods used for WebElements.
*/

const LEGACY_ELEMENT_ID_KEY = 'ELEMENT'
const ELEMENT_ID_KEY = 'element-6066-11e4-a52e-4f735466cecf'

/**
* Contains logic about WebElements.
*/
class Element {
/**
* @param {?} obj the object to test.
* @return {boolean} whether the object is a valid encoded WebElement ID.
*/
static isId (obj) {
return (
obj &&
typeof obj === 'object' &&
(typeof obj[ELEMENT_ID_KEY] === 'string' ||
typeof obj[LEGACY_ELEMENT_ID_KEY] === 'string')
)
}

/**
* Extracts the encoded WebElement ID from the object.
*
* @param {?} obj The object to extract the ID from.
* @return {string} the extracted ID.
* @throws {TypeError} if the object is not a valid encoded ID.
*/
static extractId (obj) {
if (obj && typeof obj === 'object') {
if (typeof obj[ELEMENT_ID_KEY] === 'string') {
return obj[ELEMENT_ID_KEY]
} else if (typeof obj[LEGACY_ELEMENT_ID_KEY] === 'string') {
return obj[LEGACY_ELEMENT_ID_KEY]
}
}
throw new TypeError('object is not a WebElement ID')
}
}

module.exports.isId = Element.isId
module.exports.extractId = Element.extractId

0 comments on commit f5f2887

Please sign in to comment.