Skip to content

Commit 51095a4

Browse files
authored
chore/code style changes, add util (#10975)
1 parent 878feb7 commit 51095a4

9 files changed

Lines changed: 78 additions & 42 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@
4141
* {name: string}|
4242
* {partialLinkText: string}|
4343
* {tagName: string}|
44-
* {xpath: string})}
44+
* {xpath: string})} ByHash
4545
*/
46-
var ByHash // eslint-disable-line
4746

4847
/**
4948
* Error thrown if an invalid character is encountered while escaping a CSS

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class Executor {
204204
// PUBLIC API
205205

206206
module.exports = {
207-
Command: Command,
208-
Name: Name,
209-
Executor: Executor,
207+
Command,
208+
Name,
209+
Executor,
210210
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
'use strict'
1919

20+
const { isObject } = require('./util')
21+
2022
/**
2123
* The base WebDriver error type. This error type is only used directly when a
2224
* more appropriate category is not defined for the offending error.
@@ -505,7 +507,7 @@ function encodeError(err) {
505507
* @see https://w3c.github.io/webdriver/webdriver-spec.html#protocol
506508
*/
507509
function isErrorResponse(data) {
508-
return data && typeof data === 'object' && typeof data.error === 'string'
510+
return isObject(data) && typeof data.error === 'string'
509511
}
510512

511513
/**
@@ -540,15 +542,13 @@ function throwDecodedError(data) {
540542
function checkLegacyResponse(responseObj) {
541543
// Handle the legacy Selenium error response format.
542544
if (
543-
responseObj &&
544-
typeof responseObj === 'object' &&
545-
typeof responseObj['status'] === 'number' &&
546-
responseObj['status'] !== 0
545+
isObject(responseObj) &&
546+
typeof responseObj.status === 'number' &&
547+
responseObj.status !== 0
547548
) {
548-
let status = responseObj['status']
549-
let ctor = LEGACY_ERROR_CODE_TO_TYPE.get(status) || WebDriverError
549+
const { status, value } = responseObj
550550

551-
let value = responseObj['value']
551+
let ctor = LEGACY_ERROR_CODE_TO_TYPE.get(status) || WebDriverError
552552

553553
if (!value || typeof value !== 'object') {
554554
throw new ctor(value + '')

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const logging = require('./logging')
3232
const promise = require('./promise')
3333
const { Session } = require('./session')
3434
const webElement = require('./webelement')
35+
const { isObject } = require('./util')
3536

3637
const getAttribute = requireAtom(
3738
'get-attribute.js',
@@ -572,10 +573,7 @@ function parseHttpResponse(command, httpResponse) {
572573

573574
if (parsed && typeof parsed === 'object') {
574575
let value = parsed.value
575-
let isW3C =
576-
value !== null &&
577-
typeof value === 'object' &&
578-
typeof parsed.status === 'undefined'
576+
let isW3C = isObject(value) && typeof parsed.status === 'undefined'
579577

580578
if (!isW3C) {
581579
error.checkLegacyResponse(parsed)
@@ -645,10 +643,10 @@ function buildPath(path, parameters) {
645643
// PUBLIC API
646644

647645
module.exports = {
648-
Executor: Executor,
649-
Client: Client,
650-
Request: Request,
651-
Response: Response,
646+
Executor,
647+
Client,
648+
Request,
649+
Response,
652650
// Exported for testing.
653-
buildPath: buildPath,
651+
buildPath,
654652
}

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,7 @@
2222

2323
'use strict'
2424

25-
/**
26-
* Determines whether a {@code value} should be treated as a promise.
27-
* Any object whose "then" property is a function will be considered a promise.
28-
*
29-
* @param {?} value The value to test.
30-
* @return {boolean} Whether the value is a promise.
31-
*/
32-
function isPromise(value) {
33-
return Object.prototype.toString.call(value) === '[object Promise]'
34-
}
25+
const { isObject, isPromise } = require('./util')
3526

3627
/**
3728
* Creates a promise that will be resolved at a set time in the future.
@@ -218,7 +209,7 @@ async function fullyResolved(value) {
218209
return fullyResolveKeys(/** @type {!Array} */ (value))
219210
}
220211

221-
if (value && typeof value === 'object') {
212+
if (isObject(value)) {
222213
return fullyResolveKeys(/** @type {!Object} */ (value))
223214
}
224215

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
* Determines whether a {@code value} should be treated as an object.
22+
* @param {?} value The value to test.
23+
* @returns {boolean} Whether the value is an object.
24+
*/
25+
function isObject(value) {
26+
return Object.prototype.toString.call(value) === '[object Object]'
27+
}
28+
29+
/**
30+
* Determines whether a {@code value} should be treated as a promise.
31+
* Any object whose "then" property is a function will be considered a promise.
32+
*
33+
* @param {?} value The value to test.
34+
* @return {boolean} Whether the value is a promise.
35+
*/
36+
function isPromise(value) {
37+
return Object.prototype.toString.call(value) === '[object Promise]'
38+
}
39+
40+
module.exports = {
41+
isObject,
42+
isPromise,
43+
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,11 @@ class Credential {
234234
}
235235
}
236236

237-
module.exports.Credential = Credential
238-
module.exports.VirtualAuthenticatorOptions = VirtualAuthenticatorOptions
239-
module.exports.Transport = Transport
240-
module.exports.Protocol = Protocol
237+
// PUBLIC API
238+
239+
module.exports = {
240+
Credential,
241+
VirtualAuthenticatorOptions,
242+
Transport,
243+
Protocol,
244+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ const { Capabilities } = require('./capabilities')
3737
const path = require('path')
3838
const { NoSuchElementError } = require('./error')
3939
const cdpTargets = ['page', 'browser']
40-
const Credential = require('./virtual_authenticator').Credential
40+
const { Credential } = require('./virtual_authenticator')
4141
const webElement = require('./webelement')
42+
const { isObject } = require('./util')
4243

4344
// Capability names that are defined in the W3C spec.
4445
const W3C_CAPABILITY_NAMES = new Set([
@@ -214,7 +215,7 @@ function fromWireValue(driver, value) {
214215
} else if (ShadowRoot.isId(value)) {
215216
let id = ShadowRoot.extractId(value)
216217
value = new ShadowRoot(driver, id)
217-
} else if (value && typeof value === 'object') {
218+
} else if (isObject(value)) {
218219
let result = {}
219220
for (let key in value) {
220221
if (Object.prototype.hasOwnProperty.call(value, key)) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717

1818
'use strict'
19+
const { isObject } = require('./util')
1920

2021
/**
2122
* @fileoverview Defines some common methods used for WebElements.
@@ -33,8 +34,7 @@ const ELEMENT_ID_KEY = 'element-6066-11e4-a52e-4f735466cecf'
3334
*/
3435
function isId(obj) {
3536
return (
36-
obj &&
37-
typeof obj === 'object' &&
37+
isObject(obj) &&
3838
(typeof obj[ELEMENT_ID_KEY] === 'string' ||
3939
typeof obj[LEGACY_ELEMENT_ID_KEY] === 'string')
4040
)
@@ -48,7 +48,7 @@ function isId(obj) {
4848
* @throws {TypeError} if the object is not a valid encoded ID.
4949
*/
5050
function extractId(obj) {
51-
if (obj && typeof obj === 'object') {
51+
if (isObject(obj)) {
5252
if (typeof obj[ELEMENT_ID_KEY] === 'string') {
5353
return obj[ELEMENT_ID_KEY]
5454
} else if (typeof obj[LEGACY_ELEMENT_ID_KEY] === 'string') {

0 commit comments

Comments
 (0)