Skip to content

Commit

Permalink
[nodejs] code cleanup first iteration (#9158)
Browse files Browse the repository at this point in the history
* code cleanup first iteration

* [JS] Specifying encoding to read data in string

Signed-off-by: Sri Harsha <[email protected]>

Co-authored-by: Sri Harsha <[email protected]>
  • Loading branch information
potapovDim and harsha509 authored Mar 24, 2021
1 parent 777cf25 commit 84dd610
Show file tree
Hide file tree
Showing 39 changed files with 334 additions and 276 deletions.
6 changes: 3 additions & 3 deletions javascript/node/selenium-webdriver/io/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ class Command {
* @return {!Command} The launched command.
*/
module.exports = function exec(command, opt_options) {
var options = opt_options || {}
const options = opt_options || {}

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

let result = new Promise((resolve) => {
const result = new Promise((resolve) => {
proc.once('exit', (code, signal) => {
proc = null
process.removeListener('exit', onProcessExit)
Expand Down
17 changes: 8 additions & 9 deletions javascript/node/selenium-webdriver/io/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,12 @@ exports.tmpDir = function () {
*/
exports.tmpFile = function (opt_options) {
return checkedCall((callback) => {
// |tmp.file| checks arguments length to detect options rather than doing a
// truthy check, so we must only pass options if there are some to pass.
if (opt_options) {
tmp.file(opt_options, callback)
} else {
tmp.file(callback)
}
/** check fixed in v > 0.2.1 if
* (typeof options === 'function') {
* return [{}, options];
* }
*/
tmp.file(opt_options, callback)
})
}

Expand All @@ -224,7 +223,7 @@ exports.tmpFile = function (opt_options) {
* not be found.
*/
exports.findInPath = function (file, opt_checkCwd) {
let dirs = []
const dirs = []
if (opt_checkCwd) {
dirs.push(process.cwd())
}
Expand Down Expand Up @@ -329,7 +328,7 @@ exports.mkdirp = function mkdirp(dir) {
* will be relative to `rootPath`.
*/
exports.walkDir = function (rootPath) {
let seen = []
const seen = []
return (function walk(dir) {
return checkedCall((callback) => fs.readdir(dir, callback)).then((files) =>
Promise.all(
Expand Down
11 changes: 5 additions & 6 deletions javascript/node/selenium-webdriver/io/zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ function load(path) {
*/
function unzip(src, dst) {
return load(src).then((zip) => {
let promisedDirs = new Map()
let promises = []
const promisedDirs = new Map()
const promises = []

zip.z_.forEach((relPath, file) => {
let p
Expand Down Expand Up @@ -218,7 +218,6 @@ function unzip(src, dst) {
}

// PUBLIC API

exports.Zip = Zip
exports.load = load
exports.unzip = unzip
module.exports.Zip = Zip
module.exports.load = load
module.exports.unzip = unzip
2 changes: 1 addition & 1 deletion javascript/node/selenium-webdriver/lib/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ class LegacyActionSequence {
* @private
*/
scheduleKeyboardAction_(description, keys) {
let cmd = new command.Command(
const cmd = new command.Command(
command.Name.LEGACY_ACTION_SEND_KEYS
).setParameter('value', keys)
this.schedule_(description, cmd)
Expand Down
20 changes: 4 additions & 16 deletions javascript/node/selenium-webdriver/lib/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,7 @@
* @return {boolean} Whether the value is a promise.
*/
function isPromise(value) {
try {
// Use array notation so the Closure compiler does not obfuscate away our
// contract.
return (
value &&
(typeof value === 'object' || typeof value === 'function') &&
typeof value['then'] === 'function'
)
} catch (ex) {
return false
}
return Object.prototype.toString.call(value) === '[object Promise]'

This comment has been minimized.

Copy link
@cstringer

cstringer Sep 20, 2022

Contributor

this seems to have created a bug – a WebElementPromise for example returns [object Object] from Object.prototype.toString() – however the JSDoc above states that any object with a then() method will be considered a Promise... so either this change needs to be reverted, or the documentation needs to be updated

This comment has been minimized.

Copy link
@potapovDim

potapovDim Sep 30, 2022

Author Contributor

@cstringer good catch, thanks!

}

/**
Expand All @@ -50,9 +40,7 @@ function isPromise(value) {
* @return {!Promise<void>} The promise.
*/
function delayed(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
return new Promise((resolve) => setTimeout(resolve, ms))
}

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

for (let i = 0; i < n; i++) {
if (i in arr) {
let value = arr[i]
let include = await fn.call(self, value, i, arr)
const value = arr[i]
const include = await fn.call(self, value, i, arr)
if (include) {
values[valuesLength++] = value
}
Expand Down
13 changes: 6 additions & 7 deletions javascript/node/selenium-webdriver/remote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,16 @@ class DriverService {
}

return resolveCommandLineFlags(this.args_).then((args) => {
var command = exec(self.executable_, {
const command = exec(self.executable_, {
args: args,
env: self.env_,
stdio: self.stdio_,
})

resolveCommand(command)

var earlyTermination = command.result().then(function (result) {
var error =
const earlyTermination = command.result().then(function (result) {
const error =
result.code == null
? Error('Server was killed with ' + result.signal)
: Error('Server terminated early with status ' + result.code)
Expand All @@ -229,7 +229,7 @@ class DriverService {
throw error
})

var hostname = self.hostname_
let hostname = self.hostname_
if (!hostname) {
hostname =
(!self.loopbackOnly_ && net.getAddress()) ||
Expand Down Expand Up @@ -338,9 +338,8 @@ DriverService.Builder = class {
* @this {THIS}
* @template THIS
*/
addArguments(var_args) { // eslint-disable-line
let args = Array.prototype.slice.call(arguments, 0)
this.options_.args = this.options_.args.concat(args)
addArguments(...arguments_) {
this.options_.args = this.options_.args.concat(arguments_)
return this
}

Expand Down
52 changes: 26 additions & 26 deletions javascript/node/selenium-webdriver/test/actions_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,31 @@ test.suite(function (env) {
await driver.get(fileServer.whereIs('/data/actions/click.html'))

let box = await driver.findElement(By.id('box'))
assert.equal(await box.getAttribute('class'), '')
assert.strictEqual(await box.getAttribute('class'), '')

await driver.actions().click(box).perform()
assert.equal(await box.getAttribute('class'), 'green')
assert.strictEqual(await box.getAttribute('class'), 'green')
})

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

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

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

const clicks = await driver.executeScript('return clicks')
assert.deepEqual(clicks, [[250, 250]])
assert.deepStrictEqual(clicks, [[250, 250]])
})

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

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

await driver
.actions()
Expand All @@ -87,7 +87,7 @@ test.suite(function (env) {
.perform()

const clicks = await driver.executeScript('return clicks')
assert.deepEqual(clicks, [[260, 260]])
assert.deepStrictEqual(clicks, [[260, 260]])
})

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

let box = await driver.findElement(By.id('box'))
assert.equal(await box.getAttribute('class'), '')
assert.strictEqual(await box.getAttribute('class'), '')

await driver.actions().doubleClick(box).perform()
assert.equal(await box.getAttribute('class'), 'blue')
assert.strictEqual(await box.getAttribute('class'), 'blue')
})

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

let slide = await driver.findElement(By.id('slide'))
assert.equal(await slide.getCssValue('left'), '0px')
assert.equal(await slide.getCssValue('top'), '0px')
assert.strictEqual(await slide.getCssValue('left'), '0px')
assert.strictEqual(await slide.getCssValue('top'), '0px')

let br = await driver.findElement(By.id('BR'))
await driver.actions().dragAndDrop(slide, br).perform()
assert.equal(await slide.getCssValue('left'), '206px')
assert.equal(await slide.getCssValue('top'), '206px')
assert.strictEqual(await slide.getCssValue('left'), '206px')
assert.strictEqual(await slide.getCssValue('top'), '206px')

let tr = await driver.findElement(By.id('TR'))
await driver.actions().dragAndDrop(slide, tr).perform()
assert.equal(await slide.getCssValue('left'), '206px')
assert.equal(await slide.getCssValue('top'), '1px')
assert.strictEqual(await slide.getCssValue('left'), '206px')
assert.strictEqual(await slide.getCssValue('top'), '1px')
})
})

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

let slide = await driver.findElement(By.id('slide'))
assert.equal(await slide.getCssValue('left'), '0px')
assert.equal(await slide.getCssValue('top'), '0px')
assert.strictEqual(await slide.getCssValue('left'), '0px')
assert.strictEqual(await slide.getCssValue('top'), '0px')

await driver
.actions()
Expand All @@ -141,8 +141,8 @@ test.suite(function (env) {
.move({ x: 100, y: 100, origin: Origin.POINTER })
.release()
.perform()
assert.equal(await slide.getCssValue('left'), '101px')
assert.equal(await slide.getCssValue('top'), '101px')
assert.strictEqual(await slide.getCssValue('left'), '101px')
assert.strictEqual(await slide.getCssValue('top'), '101px')
})

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

let el = await driver.findElement(By.id('email'))
assert.equal(await el.getAttribute('value'), '')
assert.strictEqual(await el.getAttribute('value'), '')

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

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

assert.equal(await el.getAttribute('value'), 'foobar')
assert.strictEqual(await el.getAttribute('value'), 'foobar')
})

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

let el = await driver.findElement(By.id('email'))
assert.equal(await el.getProperty('value'), '')
assert.strictEqual(await el.getProperty('value'), '')

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

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

assert.equal(await el.getProperty('value'), 'foobar')
assert.strictEqual(await el.getProperty('value'), 'foobar')
})

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

let el = await driver.findElement(By.id('email'))
assert.equal(await el.getAttribute('value'), '')
assert.strictEqual(await el.getAttribute('value'), '')

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

Expand All @@ -202,18 +202,18 @@ test.suite(function (env) {
.sendKeys('ar')
.perform()

assert.equal(await el.getAttribute('value'), 'foOBar')
assert.strictEqual(await el.getAttribute('value'), 'foOBar')
})

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

let el = await driver.findElement(By.id('email'))
assert.equal(await el.getAttribute('value'), '')
assert.strictEqual(await el.getAttribute('value'), '')

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

assert.equal(await el.getAttribute('value'), 'foobar')
assert.strictEqual(await el.getAttribute('value'), 'foobar')
})
})
})
2 changes: 1 addition & 1 deletion javascript/node/selenium-webdriver/test/builder_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ test.suite(function (env) {
driver instanceof want,
`want ${want.name}, but got ${driver.name}`
)
assert.equal(typeof driver.then, 'function')
assert.strictEqual(typeof driver.then, 'function')

return (
driver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ test.suite(
await driver.register('random', 'random', pageCdpConnection)
await driver.get(fileServer.Pages.basicAuth)
let source = await driver.getPageSource()
assert.ok(!source.includes('Access granted!'), `The Source is \n ${source}`)
assert.strictEqual(source.includes('Access granted!'), false)
})

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ describe('chrome.Options', function () {
[symbols.serialize]()

assert.strictEqual(wire['goog:chromeOptions'].extensions.length, 1)
assert.strictEqual(await wire['goog:chromeOptions'].extensions[0], expected)
assert.strictEqual(
await wire['goog:chromeOptions'].extensions[0],
expected
)
})
})
})
Expand Down
Loading

0 comments on commit 84dd610

Please sign in to comment.