Skip to content

Commit 84dd610

Browse files
[nodejs] code cleanup first iteration (#9158)
* code cleanup first iteration * [JS] Specifying encoding to read data in string Signed-off-by: Sri Harsha <sri_harsha509@hotmail.com> Co-authored-by: Sri Harsha <sri_harsha509@hotmail.com>
1 parent 777cf25 commit 84dd610

39 files changed

+334
-276
lines changed

javascript/node/selenium-webdriver/io/exec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ class Command {
121121
* @return {!Command} The launched command.
122122
*/
123123
module.exports = function exec(command, opt_options) {
124-
var options = opt_options || {}
124+
const options = opt_options || {}
125125

126-
var proc = childProcess.spawn(command, options.args || [], {
126+
let proc = childProcess.spawn(command, options.args || [], {
127127
env: options.env || process.env,
128128
stdio: options.stdio || 'ignore',
129129
})
@@ -133,7 +133,7 @@ module.exports = function exec(command, opt_options) {
133133
proc.unref()
134134
process.once('exit', onProcessExit)
135135

136-
let result = new Promise((resolve) => {
136+
const result = new Promise((resolve) => {
137137
proc.once('exit', (code, signal) => {
138138
proc = null
139139
process.removeListener('exit', onProcessExit)

javascript/node/selenium-webdriver/io/index.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,12 @@ exports.tmpDir = function () {
204204
*/
205205
exports.tmpFile = function (opt_options) {
206206
return checkedCall((callback) => {
207-
// |tmp.file| checks arguments length to detect options rather than doing a
208-
// truthy check, so we must only pass options if there are some to pass.
209-
if (opt_options) {
210-
tmp.file(opt_options, callback)
211-
} else {
212-
tmp.file(callback)
213-
}
207+
/** check fixed in v > 0.2.1 if
208+
* (typeof options === 'function') {
209+
* return [{}, options];
210+
* }
211+
*/
212+
tmp.file(opt_options, callback)
214213
})
215214
}
216215

@@ -224,7 +223,7 @@ exports.tmpFile = function (opt_options) {
224223
* not be found.
225224
*/
226225
exports.findInPath = function (file, opt_checkCwd) {
227-
let dirs = []
226+
const dirs = []
228227
if (opt_checkCwd) {
229228
dirs.push(process.cwd())
230229
}
@@ -329,7 +328,7 @@ exports.mkdirp = function mkdirp(dir) {
329328
* will be relative to `rootPath`.
330329
*/
331330
exports.walkDir = function (rootPath) {
332-
let seen = []
331+
const seen = []
333332
return (function walk(dir) {
334333
return checkedCall((callback) => fs.readdir(dir, callback)).then((files) =>
335334
Promise.all(

javascript/node/selenium-webdriver/io/zip.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ function load(path) {
180180
*/
181181
function unzip(src, dst) {
182182
return load(src).then((zip) => {
183-
let promisedDirs = new Map()
184-
let promises = []
183+
const promisedDirs = new Map()
184+
const promises = []
185185

186186
zip.z_.forEach((relPath, file) => {
187187
let p
@@ -218,7 +218,6 @@ function unzip(src, dst) {
218218
}
219219

220220
// PUBLIC API
221-
222-
exports.Zip = Zip
223-
exports.load = load
224-
exports.unzip = unzip
221+
module.exports.Zip = Zip
222+
module.exports.load = load
223+
module.exports.unzip = unzip

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ class LegacyActionSequence {
342342
* @private
343343
*/
344344
scheduleKeyboardAction_(description, keys) {
345-
let cmd = new command.Command(
345+
const cmd = new command.Command(
346346
command.Name.LEGACY_ACTION_SEND_KEYS
347347
).setParameter('value', keys)
348348
this.schedule_(description, cmd)

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,7 @@
3030
* @return {boolean} Whether the value is a promise.
3131
*/
3232
function isPromise(value) {
33-
try {
34-
// Use array notation so the Closure compiler does not obfuscate away our
35-
// contract.
36-
return (
37-
value &&
38-
(typeof value === 'object' || typeof value === 'function') &&
39-
typeof value['then'] === 'function'
40-
)
41-
} catch (ex) {
42-
return false
43-
}
33+
return Object.prototype.toString.call(value) === '[object Promise]'
4434
}
4535

4636
/**
@@ -50,9 +40,7 @@ function isPromise(value) {
5040
* @return {!Promise<void>} The promise.
5141
*/
5242
function delayed(ms) {
53-
return new Promise((resolve) => {
54-
setTimeout(resolve, ms)
55-
})
43+
return new Promise((resolve) => setTimeout(resolve, ms))
5644
}
5745

5846
/**
@@ -199,8 +187,8 @@ async function filter(array, fn, self = undefined) {
199187

200188
for (let i = 0; i < n; i++) {
201189
if (i in arr) {
202-
let value = arr[i]
203-
let include = await fn.call(self, value, i, arr)
190+
const value = arr[i]
191+
const include = await fn.call(self, value, i, arr)
204192
if (include) {
205193
values[valuesLength++] = value
206194
}

javascript/node/selenium-webdriver/remote/index.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,16 @@ class DriverService {
210210
}
211211

212212
return resolveCommandLineFlags(this.args_).then((args) => {
213-
var command = exec(self.executable_, {
213+
const command = exec(self.executable_, {
214214
args: args,
215215
env: self.env_,
216216
stdio: self.stdio_,
217217
})
218218

219219
resolveCommand(command)
220220

221-
var earlyTermination = command.result().then(function (result) {
222-
var error =
221+
const earlyTermination = command.result().then(function (result) {
222+
const error =
223223
result.code == null
224224
? Error('Server was killed with ' + result.signal)
225225
: Error('Server terminated early with status ' + result.code)
@@ -229,7 +229,7 @@ class DriverService {
229229
throw error
230230
})
231231

232-
var hostname = self.hostname_
232+
let hostname = self.hostname_
233233
if (!hostname) {
234234
hostname =
235235
(!self.loopbackOnly_ && net.getAddress()) ||
@@ -338,9 +338,8 @@ DriverService.Builder = class {
338338
* @this {THIS}
339339
* @template THIS
340340
*/
341-
addArguments(var_args) { // eslint-disable-line
342-
let args = Array.prototype.slice.call(arguments, 0)
343-
this.options_.args = this.options_.args.concat(args)
341+
addArguments(...arguments_) {
342+
this.options_.args = this.options_.args.concat(arguments_)
344343
return this
345344
}
346345

javascript/node/selenium-webdriver/test/actions_test.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,31 @@ test.suite(function (env) {
5454
await driver.get(fileServer.whereIs('/data/actions/click.html'))
5555

5656
let box = await driver.findElement(By.id('box'))
57-
assert.equal(await box.getAttribute('class'), '')
57+
assert.strictEqual(await box.getAttribute('class'), '')
5858

5959
await driver.actions().click(box).perform()
60-
assert.equal(await box.getAttribute('class'), 'green')
60+
assert.strictEqual(await box.getAttribute('class'), 'green')
6161
})
6262

6363
it('click(element) clicks in center of element', async function () {
6464
await driver.get(fileServer.whereIs('/data/actions/record_click.html'))
6565

6666
const div = await driver.findElement(By.css('div'))
6767
const rect = await div.getRect()
68-
assert.deepEqual(rect, { width: 500, height: 500, x: 0, y: 0 })
68+
assert.deepStrictEqual(rect, { width: 500, height: 500, x: 0, y: 0 })
6969

7070
await driver.actions().click(div).perform()
7171

7272
const clicks = await driver.executeScript('return clicks')
73-
assert.deepEqual(clicks, [[250, 250]])
73+
assert.deepStrictEqual(clicks, [[250, 250]])
7474
})
7575

7676
it('can move relative to element center', async function () {
7777
await driver.get(fileServer.whereIs('/data/actions/record_click.html'))
7878

7979
const div = await driver.findElement(By.css('div'))
8080
const rect = await div.getRect()
81-
assert.deepEqual(rect, { width: 500, height: 500, x: 0, y: 0 })
81+
assert.deepStrictEqual(rect, { width: 500, height: 500, x: 0, y: 0 })
8282

8383
await driver
8484
.actions()
@@ -87,7 +87,7 @@ test.suite(function (env) {
8787
.perform()
8888

8989
const clicks = await driver.executeScript('return clicks')
90-
assert.deepEqual(clicks, [[260, 260]])
90+
assert.deepStrictEqual(clicks, [[260, 260]])
9191
})
9292

9393
test
@@ -96,10 +96,10 @@ test.suite(function (env) {
9696
await driver.get(fileServer.whereIs('/data/actions/click.html'))
9797

9898
let box = await driver.findElement(By.id('box'))
99-
assert.equal(await box.getAttribute('class'), '')
99+
assert.strictEqual(await box.getAttribute('class'), '')
100100

101101
await driver.actions().doubleClick(box).perform()
102-
assert.equal(await box.getAttribute('class'), 'blue')
102+
assert.strictEqual(await box.getAttribute('class'), 'blue')
103103
})
104104

105105
// For some reason for Chrome 75 we need to wrap this test in an extra
@@ -112,27 +112,27 @@ test.suite(function (env) {
112112
await driver.get(fileServer.whereIs('/data/actions/drag.html'))
113113

114114
let slide = await driver.findElement(By.id('slide'))
115-
assert.equal(await slide.getCssValue('left'), '0px')
116-
assert.equal(await slide.getCssValue('top'), '0px')
115+
assert.strictEqual(await slide.getCssValue('left'), '0px')
116+
assert.strictEqual(await slide.getCssValue('top'), '0px')
117117

118118
let br = await driver.findElement(By.id('BR'))
119119
await driver.actions().dragAndDrop(slide, br).perform()
120-
assert.equal(await slide.getCssValue('left'), '206px')
121-
assert.equal(await slide.getCssValue('top'), '206px')
120+
assert.strictEqual(await slide.getCssValue('left'), '206px')
121+
assert.strictEqual(await slide.getCssValue('top'), '206px')
122122

123123
let tr = await driver.findElement(By.id('TR'))
124124
await driver.actions().dragAndDrop(slide, tr).perform()
125-
assert.equal(await slide.getCssValue('left'), '206px')
126-
assert.equal(await slide.getCssValue('top'), '1px')
125+
assert.strictEqual(await slide.getCssValue('left'), '206px')
126+
assert.strictEqual(await slide.getCssValue('top'), '1px')
127127
})
128128
})
129129

130130
it('move()', async function () {
131131
await driver.get(fileServer.whereIs('/data/actions/drag.html'))
132132

133133
let slide = await driver.findElement(By.id('slide'))
134-
assert.equal(await slide.getCssValue('left'), '0px')
135-
assert.equal(await slide.getCssValue('top'), '0px')
134+
assert.strictEqual(await slide.getCssValue('left'), '0px')
135+
assert.strictEqual(await slide.getCssValue('top'), '0px')
136136

137137
await driver
138138
.actions()
@@ -141,8 +141,8 @@ test.suite(function (env) {
141141
.move({ x: 100, y: 100, origin: Origin.POINTER })
142142
.release()
143143
.perform()
144-
assert.equal(await slide.getCssValue('left'), '101px')
145-
assert.equal(await slide.getCssValue('top'), '101px')
144+
assert.strictEqual(await slide.getCssValue('left'), '101px')
145+
assert.strictEqual(await slide.getCssValue('top'), '101px')
146146
})
147147

148148
it('can move to and click element in an iframe', async function () {
@@ -163,33 +163,33 @@ test.suite(function (env) {
163163
await driver.get(test.Pages.formPage)
164164

165165
let el = await driver.findElement(By.id('email'))
166-
assert.equal(await el.getAttribute('value'), '')
166+
assert.strictEqual(await el.getAttribute('value'), '')
167167

168168
await driver.executeScript('arguments[0].focus()', el)
169169

170170
await driver.actions().sendKeys('foobar').perform()
171171

172-
assert.equal(await el.getAttribute('value'), 'foobar')
172+
assert.strictEqual(await el.getAttribute('value'), 'foobar')
173173
})
174174

175175
it('can get the property of element', async function () {
176176
await driver.get(test.Pages.formPage)
177177

178178
let el = await driver.findElement(By.id('email'))
179-
assert.equal(await el.getProperty('value'), '')
179+
assert.strictEqual(await el.getProperty('value'), '')
180180

181181
await driver.executeScript('arguments[0].focus()', el)
182182

183183
await driver.actions().sendKeys('foobar').perform()
184184

185-
assert.equal(await el.getProperty('value'), 'foobar')
185+
assert.strictEqual(await el.getProperty('value'), 'foobar')
186186
})
187187

188188
it('can send keys to focused element (with modifiers)', async function () {
189189
await driver.get(test.Pages.formPage)
190190

191191
let el = await driver.findElement(By.id('email'))
192-
assert.equal(await el.getAttribute('value'), '')
192+
assert.strictEqual(await el.getAttribute('value'), '')
193193

194194
await driver.executeScript('arguments[0].focus()', el)
195195

@@ -202,18 +202,18 @@ test.suite(function (env) {
202202
.sendKeys('ar')
203203
.perform()
204204

205-
assert.equal(await el.getAttribute('value'), 'foOBar')
205+
assert.strictEqual(await el.getAttribute('value'), 'foOBar')
206206
})
207207

208208
it('can interact with simple form elements', async function () {
209209
await driver.get(test.Pages.formPage)
210210

211211
let el = await driver.findElement(By.id('email'))
212-
assert.equal(await el.getAttribute('value'), '')
212+
assert.strictEqual(await el.getAttribute('value'), '')
213213

214214
await driver.actions().click(el).sendKeys('foobar').perform()
215215

216-
assert.equal(await el.getAttribute('value'), 'foobar')
216+
assert.strictEqual(await el.getAttribute('value'), 'foobar')
217217
})
218218
})
219219
})

javascript/node/selenium-webdriver/test/builder_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ test.suite(function (env) {
5353
driver instanceof want,
5454
`want ${want.name}, but got ${driver.name}`
5555
)
56-
assert.equal(typeof driver.then, 'function')
56+
assert.strictEqual(typeof driver.then, 'function')
5757

5858
return (
5959
driver

javascript/node/selenium-webdriver/test/chrome/devtools_test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ test.suite(
146146
await driver.register('random', 'random', pageCdpConnection)
147147
await driver.get(fileServer.Pages.basicAuth)
148148
let source = await driver.getPageSource()
149-
assert.ok(!source.includes('Access granted!'), `The Source is \n ${source}`)
149+
assert.strictEqual(source.includes('Access granted!'), false)
150150
})
151151

152152
it('grants access if username and password are a match', async function () {
@@ -156,6 +156,7 @@ test.suite(
156156
await driver.get(fileServer.Pages.basicAuth)
157157
let source = await driver.getPageSource()
158158
assert.strictEqual(source.includes('Access granted!'), true)
159+
await server.stop()
159160
})
160161
})
161162

javascript/node/selenium-webdriver/test/chrome/options_test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ describe('chrome.Options', function () {
8585
[symbols.serialize]()
8686

8787
assert.strictEqual(wire['goog:chromeOptions'].extensions.length, 1)
88-
assert.strictEqual(await wire['goog:chromeOptions'].extensions[0], expected)
88+
assert.strictEqual(
89+
await wire['goog:chromeOptions'].extensions[0],
90+
expected
91+
)
8992
})
9093
})
9194
})

0 commit comments

Comments
 (0)