From ce47563d78e9cb34125b6e5432fa7b141a6a9cdd Mon Sep 17 00:00:00 2001 From: Madhav Date: Fri, 23 Feb 2024 14:50:15 +0530 Subject: [PATCH] fixed formatting issues --- .../selenium-webdriver/method-mappings.js | 203 ++++++------------ .../api/commands/element/testClearValue.js | 176 +++++++-------- 2 files changed, 142 insertions(+), 237 deletions(-) diff --git a/lib/transport/selenium-webdriver/method-mappings.js b/lib/transport/selenium-webdriver/method-mappings.js index c824734071..5611157b71 100644 --- a/lib/transport/selenium-webdriver/method-mappings.js +++ b/lib/transport/selenium-webdriver/method-mappings.js @@ -1,12 +1,4 @@ -const { - WebElement, - WebDriver, - Origin, - Key, - By, - until, - Condition -} = require('selenium-webdriver'); +const {WebElement, WebDriver, Origin, Key, By, until, Condition} = require('selenium-webdriver'); const {Locator} = require('../../element'); const NightwatchLocator = require('../../element/locator-factory.js'); const {isString} = require('../../utils'); @@ -23,10 +15,7 @@ module.exports = class MethodMappings { } getWebElement(webElementOrString) { - if ( - webElementOrString && - webElementOrString.webElement instanceof WebElement - ) { + if (webElementOrString && (webElementOrString.webElement instanceof WebElement)) { webElementOrString = webElementOrString.webElement; } @@ -46,9 +35,7 @@ module.exports = class MethodMappings { const parentElementId = await element.getId(); const {elementKey} = this.transport; - return this.driver.executeScript(scriptFn, { - [elementKey]: parentElementId - }); + return this.driver.executeScript(scriptFn, {[elementKey]: parentElementId}); } async getElementByJs(scriptFn, webElementOrId) { @@ -167,10 +154,7 @@ module.exports = class MethodMappings { // Session log /////////////////////////////////////////////////////////// async getSessionLogTypes() { - const value = await this.driver - .manage() - .logs() - .getAvailableLogTypes(); + const value = await this.driver.manage().logs().getAvailableLogTypes(); return { value @@ -181,7 +165,7 @@ module.exports = class MethodMappings { const value = await this.driver.manage().logs().get(type); return { - value: value.map((item) => { + value: value.map(item => { return { level: { value: item.level.value, @@ -385,10 +369,7 @@ module.exports = class MethodMappings { // Elements /////////////////////////////////////////////////////////// async locateSingleElement(element) { - const locator = NightwatchLocator.create( - element, - this.transport.api.isAppiumClient() - ); + const locator = NightwatchLocator.create(element, this.transport.api.isAppiumClient()); const webElement = await this.driver.findElement(locator); const elementId = await webElement.getId(); @@ -400,10 +381,7 @@ module.exports = class MethodMappings { }, async locateMultipleElements(element) { - const locator = NightwatchLocator.create( - element, - this.transport.api.isAppiumClient() - ); + const locator = NightwatchLocator.create(element, this.transport.api.isAppiumClient()); const resultValue = await this.driver.findElements(locator); if (Array.isArray(resultValue) && resultValue.length === 0) { @@ -416,17 +394,16 @@ module.exports = class MethodMappings { } const {elementKey} = this.transport; - const value = await Promise.all( - resultValue.map(async (webElement) => { - const elementId = await webElement.getId(); + const value = await Promise.all(resultValue.map(async webElement => { + const elementId = await webElement.getId(); - return {[elementKey]: elementId}; - }) - ); + return {[elementKey]: elementId}; + })); return value; }, + elementIdEquals(id, otherId) { return `/element/${id}/equals/${otherId}`; }, @@ -452,13 +429,11 @@ module.exports = class MethodMappings { const resultValue = await element.findElements(locator); const {elementKey} = this.transport; - const resultProcessed = await Promise.all( - resultValue.map(async (webElement) => { - const elementId = await webElement.getId(); + const resultProcessed = await Promise.all(resultValue.map(async webElement => { + const elementId = await webElement.getId(); - return {[elementKey]: elementId}; - }) - ); + return {[elementKey]: elementId}; + })); return resultProcessed; }, @@ -520,45 +495,20 @@ module.exports = class MethodMappings { async setElementProperty(webElementOrId, name, value) { const element = this.getWebElement(webElementOrId); - await this.driver.executeScript( - function (element, name, value) { - element[name] = value; - }, - element, - name, - value - ); + await this.driver.executeScript(function (element, name, value) { + element[name] = value; + }, element, name, value); }, async setElementAttribute(webElement, attrName, value) { const element = this.getWebElement(webElement); // eslint-disable-next-line - /* istanbul ignore next */ const fn = function (e, a, v) { - try { - if (e && typeof e.setAttribute == 'function') { - e.setAttribute(a, v); - } - - return true; - } catch (err) { - return { - error: err.message, - message: err.name + ': ' + err.message - }; - } - }; + /* istanbul ignore next */const fn = function (e, a, v) { try { if (e && typeof e.setAttribute == 'function') { e.setAttribute(a, v); } return true; } catch (err) { return { error: err.message, message: err.name + ': ' + err.message }; } }; const elementId = await element.getId(); - const result = await this.driver.executeScript( - 'var passedArgs = Array.prototype.slice.call(arguments,0); ' + - 'return (' + - fn.toString() + - ').apply(window, passedArgs);', - {[this.transport.elementKey]: elementId}, - attrName, - value - ); + const result = await this.driver.executeScript('var passedArgs = Array.prototype.slice.call(arguments,0); ' + + 'return (' + fn.toString() + ').apply(window, passedArgs);', {[this.transport.elementKey]: elementId}, attrName, value); return result; }, @@ -679,9 +629,7 @@ module.exports = class MethodMappings { async sendKeysToElement(webElementOrId, value) { if (Array.isArray(value)) { if (value.includes(undefined) || value.includes(null)) { - throw TypeError( - 'each key must be a number or string; got ' + value - ); + throw TypeError('each key must be a number or string; got ' + value); } value = value.join(''); } @@ -746,14 +694,10 @@ module.exports = class MethodMappings { * @param {WebElement} webElement */ inspectInDevTools(webElement, content = 'Element') { - return this.driver.executeScript( - function (element, content) { - // eslint-disable-next-line no-console - console.log(content + ':', element); - }, - webElement, - content - ); + return this.driver.executeScript(function (element, content) { + // eslint-disable-next-line no-console + console.log(content + ':', element); + }, webElement, content); }, async getLastElementChild(webElementOrId) { @@ -866,10 +810,7 @@ module.exports = class MethodMappings { if (webElement) { webElement = this.getWebElement(webElement); } - await this.driver - .actions({async: true}) - .doubleClick(webElement) - .perform(); + await this.driver.actions({async: true}).doubleClick(webElement).perform(); return null; }, @@ -907,6 +848,7 @@ module.exports = class MethodMappings { }; }, + /** * @param {string|WebElement|null} origin * @param {number} x @@ -944,31 +886,22 @@ module.exports = class MethodMappings { destination = this.getWebElement(destination); } - await this.driver - .actions({async: true}) - .dragAndDrop(source, destination) - .perform(); + await this.driver.actions({async: true}).dragAndDrop(source, destination).perform(); return null; }, async contextClick(webElementOrId) { const element = this.getWebElement(webElementOrId); - await this.driver - .actions({async: true}) - .contextClick(element) - .perform(); + await this.driver.actions({async: true}).contextClick(element).perform(); return null; }, async pressAndHold(webElementOrId) { + const element = this.getWebElement(webElementOrId); - await this.driver - .actions({async: true}) - .move({origin: element}) - .press() - .perform(); + await this.driver.actions({async: true}).move({origin: element}).press().perform(); return null; }, @@ -1120,18 +1053,14 @@ module.exports = class MethodMappings { /////////////////////////////////////////////////////////////////////////// async wait(conditionFn, timeMs, message, retryInterval) { const driver = new WebDriver(Promise.resolve()); - await driver.wait( - conditionFn instanceof Condition ? conditionFn : conditionFn(), - timeMs, - message, - retryInterval - ); + await driver.wait(conditionFn instanceof Condition ? conditionFn : conditionFn(), timeMs, message, retryInterval); return { value: null }; }, + async setNetworkConditions(spec) { await this.driver.setNetworkConditions(spec); @@ -1141,12 +1070,7 @@ module.exports = class MethodMappings { }, waitUntilElementsLocated({condition, timeout, retryInterval}) { - return this.driver.wait( - until.elementsLocated(condition), - timeout, - null, - retryInterval - ); + return this.driver.wait(until.elementsLocated(condition), timeout, null, retryInterval); }, /////////////////////////////////////////////////////////////////////////// @@ -1200,7 +1124,10 @@ module.exports = class MethodMappings { async clearGeolocation() { const cdpConnection = await cdp.getConnection(this.driver); - await cdpConnection.execute('Emulation.clearGeolocationOverride', {}); + await cdpConnection.execute( + 'Emulation.clearGeolocationOverride', + {} + ); return { value: null @@ -1232,7 +1159,10 @@ module.exports = class MethodMappings { } }); - await cdpConnection.execute('Network.enable', {}); + await cdpConnection.execute( + 'Network.enable', + {} + ); return { value: null @@ -1242,11 +1172,7 @@ module.exports = class MethodMappings { async mockNetworkResponse(urlToIntercept, response) { const cdpConnection = await cdp.getConnection(this.driver); - const { - status = 200, - headers: headersObject, - body: bodyPlain = '' - } = response; + const {status = 200, headers: headersObject, body: bodyPlain = ''} = response; const headers = []; if (headersObject) { for (const [name, value] of Object.entries(headersObject)) { @@ -1256,11 +1182,7 @@ module.exports = class MethodMappings { // Convert body to base64 const bodyBase64 = Buffer.from(bodyPlain, 'utf-8').toString('base64'); - cdp.addNetworkMock(urlToIntercept, { - status, - headers, - body: bodyBase64 - }); + cdp.addNetworkMock(urlToIntercept, {status, headers, body: bodyBase64}); // Add event listener only the first time. if (Object.keys(cdp.networkMocks).length === 1) { @@ -1288,10 +1210,14 @@ module.exports = class MethodMappings { }); } - await cdpConnection.execute('Fetch.enable', {}); - await cdpConnection.execute('Network.setCacheDisabled', { - cacheDisabled: true - }); + await cdpConnection.execute( + 'Fetch.enable', + {} + ); + await cdpConnection.execute( + 'Network.setCacheDisabled', + {cacheDisabled: true} + ); return { value: null @@ -1311,18 +1237,21 @@ module.exports = class MethodMappings { } }); - await cdpConnection.execute('HeapProfiler.enable', {}); + await cdpConnection.execute( + 'HeapProfiler.enable', + {} + ); - await cdpConnection.execute('HeapProfiler.takeHeapSnapshot', {}); + await cdpConnection.execute( + 'HeapProfiler.takeHeapSnapshot', + {} + ); let prevChunks = []; return new Promise((resolve) => { const intervalId = setInterval(() => { - if ( - prevChunks.length !== 0 && - prevChunks.length === chunks.length - ) { + if (prevChunks.length !== 0 && prevChunks.length === chunks.length) { resolveAndClearInterval(); } prevChunks = [...chunks]; @@ -1355,10 +1284,7 @@ module.exports = class MethodMappings { }, async getPerformanceMetrics() { - const {metrics: metricsReturned} = - await this.driver.sendAndGetDevToolsCommand( - 'Performance.getMetrics' - ); + const {metrics: metricsReturned} = await this.driver.sendAndGetDevToolsCommand('Performance.getMetrics'); const metrics = {}; for (const metric of metricsReturned) { @@ -1369,6 +1295,7 @@ module.exports = class MethodMappings { value: metrics }; } + } }; } diff --git a/test/src/api/commands/element/testClearValue.js b/test/src/api/commands/element/testClearValue.js index ca89fe02f8..9fc869ee51 100644 --- a/test/src/api/commands/element/testClearValue.js +++ b/test/src/api/commands/element/testClearValue.js @@ -1,95 +1,91 @@ const assert = require('assert'); -const MockServer = require('../../../../lib/mockserver.js'); +const MockServer = require('../../../../lib/mockserver.js'); const Nightwatch = require('../../../../lib/nightwatch.js'); const {Key} = require('selenium-webdriver'); -describe('clearValue', function () { - before(function (done) { +describe('clearValue', function() { + + before(function(done) { this.server = MockServer.init(); this.server.on('listening', () => done()); }); - after(function (done) { + after(function(done) { this.server.close(() => done()); }); - it('client.clearValue()', function (done) { + it('client.clearValue()', function(done) { Nightwatch.initClient({ output: false, silent: false - }).then((client) => { - MockServer.addMock({ - url: '/wd/hub/session/1352110219202/element/0/clear', - response: { - sessionId: '1352110219202', - status: 0 - } - }); + }) + .then(client => { + MockServer.addMock({ + 'url': '/wd/hub/session/1352110219202/element/0/clear', + 'response': { + sessionId: '1352110219202', + status: 0 + } + }); - client.api - .clearValue('#weblogin', function callback(result) { + client.api.clearValue('#weblogin', function callback(result) { assert.strictEqual(result.status, 0); assert.strictEqual(this, client.api); }) - .clearValue('css selector', '#weblogin', function callback(result) { - assert.strictEqual(result.status, 0); - }); + .clearValue('css selector', '#weblogin', function callback(result) { + assert.strictEqual(result.status, 0); + }); - client.start(done); - }); + client.start(done); + }); }); - it('client.clearValue() with invalid locate strategy', function (done) { + it('client.clearValue() with invalid locate strategy', function(done) { Nightwatch.initClient({ output: false, silent: false - }).then((client) => { - client.api.clearValue( - 'invalid strategy', - '#weblogin', - function callback(result) { + }) + .then(client => { + client.api.clearValue('invalid strategy', '#weblogin', function callback(result) { assert.strictEqual(result.status, 0); - } - ); + }); - client.start(function (err) { - try { - assert.ok(err instanceof Error); - assert.ok( - err.message.includes( - 'Provided locating strategy "invalid strategy" is not supported for .clearValue()' - ) - ); - done(); - } catch (e) { - done(e); - } + client.start(function(err) { + try { + assert.ok(err instanceof Error); + assert.ok(err.message.includes('Provided locating strategy "invalid strategy" is not supported for .clearValue()')); + done(); + } catch (e) { + done(e); + } + }); }); - }); }); - it('client.clearValue() with no callback', function (done) { + it('client.clearValue() with no callback', function(done) { Nightwatch.initClient({ output: false, silent: false - }).then((client) => { - MockServer.addMock({ - url: '/wd/hub/session/1352110219202/element/0/clear', - response: { - sessionId: '1352110219202', - status: 0 - } - }); + }) + .then(client => { + MockServer.addMock({ + 'url': '/wd/hub/session/1352110219202/element/0/clear', + 'response': { + sessionId: '1352110219202', + status: 0 + } + }); - client.api.elements('css selector', 'body', (res) => { - client.api.clearValue('#weblogin'); - }); - client.start(done); - }); + client.api.elements('css selector', 'body', res => { + client.api.clearValue('#weblogin'); + }); + + client.start(done); + }); }); - it('client.clearValue() with webdriver protocol', function (done) { + it('client.clearValue() with webdriver protocol', function(done) { Nightwatch.initClient({ selenium: { version2: false, @@ -102,35 +98,27 @@ describe('clearValue', function () { host: 'localhost', start_process: false } - }).then((client) => { - MockServer.addMock( - { - url: '/session/13521-10219-202/element/5cc459b8-36a8-3042-8b4a-258883ea642b/clear', - response: {value: null} - }, - true - ); + }).then(client => { + MockServer.addMock({ + url: '/session/13521-10219-202/element/5cc459b8-36a8-3042-8b4a-258883ea642b/clear', + response: {value: null} + }, true); - MockServer.addMock( - { - url: '/session/13521-10219-202/element/5cc459b8-36a8-3042-8b4a-258883ea642b/clear', - response: {value: null} - }, - true - ); + MockServer.addMock({ + url: '/session/13521-10219-202/element/5cc459b8-36a8-3042-8b4a-258883ea642b/clear', + response: {value: null} + }, true); - client.api - .clearValue('#webdriver', function (result) { - assert.strictEqual(result.value, null); - }) - .clearValue('css selector', '#webdriver', function (result) { - assert.strictEqual(result.value, null); - }); + client.api.clearValue('#webdriver', function(result) { + assert.strictEqual(result.value, null); + }).clearValue('css selector', '#webdriver', function(result) { + assert.strictEqual(result.value, null); + }); client.start(done); }); }); - + it('client.clearValue() with webdriver protocol - ensuring working of sendKeys()', function (done) { Nightwatch.initClient({ selenium: { @@ -170,7 +158,7 @@ describe('clearValue', function () { }); }); - it('client.clearValue() with webdriver protocol - element not found', function (done) { + it('client.clearValue() with webdriver protocol - element not found', function(done) { Nightwatch.initClient({ selenium: { version2: false, @@ -186,7 +174,7 @@ describe('clearValue', function () { globals: { waitForConditionPollInterval: 5 } - }).then((client) => { + }).then(client => { let retries = 0; MockServer.addMock({ statusCode: 404, @@ -200,29 +188,19 @@ describe('clearValue', function () { value: { error: 'no such element', message: 'Unable to locate element: #webdriver-notfound', - stacktrace: - 'WebDriverError@chrome://marionette/content/error.js:172:5\nNoSuchElementError@chrome://marionette/content/error.js:400:5' + stacktrace: 'WebDriverError@chrome://marionette/content/error.js:172:5\nNoSuchElementError@chrome://marionette/content/error.js:400:5' } } }); - client.api.clearValue( - {selector: '#webdriver-notfound', timeout: 11}, - function (result) { - assert.strictEqual(result.status, -1); - assert.strictEqual(retries, 2); - assert.strictEqual( - result.value.error, - 'An error occurred while running .clearValue() command on <#webdriver-notfound>: Timed out while waiting for element "#webdriver-notfound" with "css selector" to be present for 11 milliseconds.' - ); - assert.strictEqual( - result.value.message, - 'An error occurred while running .clearValue() command on <#webdriver-notfound>: Timed out while waiting for element "#webdriver-notfound" with "css selector" to be present for 11 milliseconds.' - ); - } - ); + client.api.clearValue({selector: '#webdriver-notfound', timeout: 11}, function(result) { + assert.strictEqual(result.status, -1); + assert.strictEqual(retries, 2); + assert.strictEqual(result.value.error, 'An error occurred while running .clearValue() command on <#webdriver-notfound>: Timed out while waiting for element "#webdriver-notfound" with "css selector" to be present for 11 milliseconds.'); + assert.strictEqual(result.value.message, 'An error occurred while running .clearValue() command on <#webdriver-notfound>: Timed out while waiting for element "#webdriver-notfound" with "css selector" to be present for 11 milliseconds.'); + }); client.start(done); }); }); -}); +}); \ No newline at end of file