From 5f87d1dde366fbef7c3d74399cfa3f69eb08e3ae Mon Sep 17 00:00:00 2001 From: yashPratp983 Date: Thu, 27 Jul 2023 14:10:15 +0530 Subject: [PATCH 01/15] enabled global element api to accept element properties as argument --- lib/api/_loaders/element-global.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/api/_loaders/element-global.js b/lib/api/_loaders/element-global.js index ebed36155d..6ae3b8f882 100644 --- a/lib/api/_loaders/element-global.js +++ b/lib/api/_loaders/element-global.js @@ -34,7 +34,8 @@ class ElementGlobal { static element({locator, testSuite, client, options}) { const instance = new ElementGlobal({testSuite, client, options}); - instance.setLocator(locator); + const byInstance=Locator.create(locator); + instance.setLocator(byInstance); return instance.exported(); } From d45e64fa5c7b02d5215ebc2605c8fe7a0ebda983 Mon Sep 17 00:00:00 2001 From: Praying Mantis Date: Mon, 7 Aug 2023 14:48:59 +0530 Subject: [PATCH 02/15] fixed global element api to work with element properties as argument --- lib/api/_loaders/element-global.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/api/_loaders/element-global.js b/lib/api/_loaders/element-global.js index 6ae3b8f882..c0ab172c35 100644 --- a/lib/api/_loaders/element-global.js +++ b/lib/api/_loaders/element-global.js @@ -34,8 +34,7 @@ class ElementGlobal { static element({locator, testSuite, client, options}) { const instance = new ElementGlobal({testSuite, client, options}); - const byInstance=Locator.create(locator); - instance.setLocator(byInstance); + instance.setLocator(locator); return instance.exported(); } @@ -88,7 +87,9 @@ class ElementGlobal { return; } - this.element = await this.transport.driver.wait(until.elementLocated(locator), this.timeout, null, this.retryInterval); + const byLocator=Locator.create(locator); + + this.element = await this.transport.driver.wait(until.elementLocated(byLocator), this.timeout, null, this.retryInterval); } static isElementObject(element) { From 14d51fe4e994cab4bd15b5dad9dbf905b9fadc0c Mon Sep 17 00:00:00 2001 From: Priyansh Garg <39924567+garg3133@users.noreply.github.com> Date: Tue, 8 Aug 2023 21:20:51 +0530 Subject: [PATCH 03/15] Linting fix --- lib/api/_loaders/element-global.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/api/_loaders/element-global.js b/lib/api/_loaders/element-global.js index c0ab172c35..fa1c2a28b1 100644 --- a/lib/api/_loaders/element-global.js +++ b/lib/api/_loaders/element-global.js @@ -87,7 +87,7 @@ class ElementGlobal { return; } - const byLocator=Locator.create(locator); + const byLocator = Locator.create(locator); this.element = await this.transport.driver.wait(until.elementLocated(byLocator), this.timeout, null, this.retryInterval); } From b15e01fd869410d587d78c9d53666d8d427bd948 Mon Sep 17 00:00:00 2001 From: Praying Mantis Date: Wed, 9 Aug 2023 22:00:18 +0530 Subject: [PATCH 04/15] added test for global element api to accept element property as argument --- test/apidemos/elements/elementGlobalTest.js | 7 +++++++ test/lib/command-mocks.js | 7 ++++--- .../src/apidemos/elements/testElementGlobal.js | 18 ++++++++++++++---- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/test/apidemos/elements/elementGlobalTest.js b/test/apidemos/elements/elementGlobalTest.js index b52d7521a1..e0e5a59127 100644 --- a/test/apidemos/elements/elementGlobalTest.js +++ b/test/apidemos/elements/elementGlobalTest.js @@ -53,4 +53,11 @@ describe('get text using element-global', function () { assert.strictEqual(signupSectionId, '0'); }); + test('test for element properties as argument', async function () { + const weblogin = element({selector: '#weblogin', index: 1}); + const id = await weblogin.getId(); + + assert.strictEqual(id, '5cc459b8-36a8-3042-8b4a-258883ea642b'); + }); + }); diff --git a/test/lib/command-mocks.js b/test/lib/command-mocks.js index 8c08da65b3..1dadb64fd8 100644 --- a/test/lib/command-mocks.js +++ b/test/lib/command-mocks.js @@ -259,14 +259,15 @@ module.exports = { return this; }, - w3cVisible(value = true) { + w3cVisible(value = true, {times = 0} = {}) { MockServer.addMock({ url: '/session/13521-10219-202/execute/sync', method: 'POST', response: JSON.stringify({ value - }) - }, true); + }), + times + }, times === 0); }, w3cSelected(elementId ='5cc459b8-36a8-3042-8b4a-258883ea642b', value = true) { diff --git a/test/src/apidemos/elements/testElementGlobal.js b/test/src/apidemos/elements/testElementGlobal.js index b203409685..ddf83f3021 100644 --- a/test/src/apidemos/elements/testElementGlobal.js +++ b/test/src/apidemos/elements/testElementGlobal.js @@ -21,11 +21,20 @@ describe('element global demos', function() { it('getText on element global instance', function() { const testsPath = path.join(__dirname, '../../../apidemos/elements/elementGlobalTest.js'); - Mocks.elementText(); - Mocks.tagName('0', 'div'); - Mocks.visible('0', true, { + MockServer.addMock({ + url: '/session/13521-10219-202/element/5cc459b8-36a8-3042-8b4a-258883ea642b/name', + method: 'GET', + response: JSON.stringify({ + value: 'div' + }) + }, true); + Mocks.w3cVisible(true, { times: 2 }); + Mocks.getElementText({ + elementId: '5cc459b8-36a8-3042-8b4a-258883ea642b', + responseText: 'sample text' + }); const globals = { waitForConditionPollInterval: 50, @@ -40,7 +49,8 @@ describe('element global demos', function() { }; return NightwatchClient.runTests(testsPath, settings({ - output: false, + selenium_host: null, + output: true, skip_testcases_on_fail: false, globals })); From 125d6078b3f5e39f7512a85880e360b5d05ca2cb Mon Sep 17 00:00:00 2001 From: Praying Mantis Date: Wed, 9 Aug 2023 22:16:43 +0530 Subject: [PATCH 05/15] set the output to false for the tests of element api --- test/src/apidemos/elements/testElementGlobal.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/src/apidemos/elements/testElementGlobal.js b/test/src/apidemos/elements/testElementGlobal.js index ddf83f3021..190e98a84f 100644 --- a/test/src/apidemos/elements/testElementGlobal.js +++ b/test/src/apidemos/elements/testElementGlobal.js @@ -50,7 +50,7 @@ describe('element global demos', function() { return NightwatchClient.runTests(testsPath, settings({ selenium_host: null, - output: true, + output: false, skip_testcases_on_fail: false, globals })); From 5050d29e632c08468956589e0207d34a1127bb12 Mon Sep 17 00:00:00 2001 From: yashPratp983 Date: Thu, 27 Jul 2023 14:10:15 +0530 Subject: [PATCH 06/15] enabled global element api to accept element properties as argument --- lib/api/_loaders/element-global.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/api/_loaders/element-global.js b/lib/api/_loaders/element-global.js index ebed36155d..6ae3b8f882 100644 --- a/lib/api/_loaders/element-global.js +++ b/lib/api/_loaders/element-global.js @@ -34,7 +34,8 @@ class ElementGlobal { static element({locator, testSuite, client, options}) { const instance = new ElementGlobal({testSuite, client, options}); - instance.setLocator(locator); + const byInstance=Locator.create(locator); + instance.setLocator(byInstance); return instance.exported(); } From 2420d96a7bee79e10e4c22350dfb24c9e7e4f470 Mon Sep 17 00:00:00 2001 From: Praying Mantis Date: Mon, 7 Aug 2023 14:48:59 +0530 Subject: [PATCH 07/15] fixed global element api to work with element properties as argument --- lib/api/_loaders/element-global.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/api/_loaders/element-global.js b/lib/api/_loaders/element-global.js index 6ae3b8f882..c0ab172c35 100644 --- a/lib/api/_loaders/element-global.js +++ b/lib/api/_loaders/element-global.js @@ -34,8 +34,7 @@ class ElementGlobal { static element({locator, testSuite, client, options}) { const instance = new ElementGlobal({testSuite, client, options}); - const byInstance=Locator.create(locator); - instance.setLocator(byInstance); + instance.setLocator(locator); return instance.exported(); } @@ -88,7 +87,9 @@ class ElementGlobal { return; } - this.element = await this.transport.driver.wait(until.elementLocated(locator), this.timeout, null, this.retryInterval); + const byLocator=Locator.create(locator); + + this.element = await this.transport.driver.wait(until.elementLocated(byLocator), this.timeout, null, this.retryInterval); } static isElementObject(element) { From 9e8100b6c50c909cb6a0eaf375afdafc97a6ffb9 Mon Sep 17 00:00:00 2001 From: Praying Mantis Date: Wed, 9 Aug 2023 22:00:18 +0530 Subject: [PATCH 08/15] added test for global element api to accept element property as argument --- test/apidemos/elements/elementGlobalTest.js | 7 +++++++ test/lib/command-mocks.js | 7 ++++--- .../src/apidemos/elements/testElementGlobal.js | 18 ++++++++++++++---- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/test/apidemos/elements/elementGlobalTest.js b/test/apidemos/elements/elementGlobalTest.js index b52d7521a1..e0e5a59127 100644 --- a/test/apidemos/elements/elementGlobalTest.js +++ b/test/apidemos/elements/elementGlobalTest.js @@ -53,4 +53,11 @@ describe('get text using element-global', function () { assert.strictEqual(signupSectionId, '0'); }); + test('test for element properties as argument', async function () { + const weblogin = element({selector: '#weblogin', index: 1}); + const id = await weblogin.getId(); + + assert.strictEqual(id, '5cc459b8-36a8-3042-8b4a-258883ea642b'); + }); + }); diff --git a/test/lib/command-mocks.js b/test/lib/command-mocks.js index 8c08da65b3..1dadb64fd8 100644 --- a/test/lib/command-mocks.js +++ b/test/lib/command-mocks.js @@ -259,14 +259,15 @@ module.exports = { return this; }, - w3cVisible(value = true) { + w3cVisible(value = true, {times = 0} = {}) { MockServer.addMock({ url: '/session/13521-10219-202/execute/sync', method: 'POST', response: JSON.stringify({ value - }) - }, true); + }), + times + }, times === 0); }, w3cSelected(elementId ='5cc459b8-36a8-3042-8b4a-258883ea642b', value = true) { diff --git a/test/src/apidemos/elements/testElementGlobal.js b/test/src/apidemos/elements/testElementGlobal.js index b203409685..ddf83f3021 100644 --- a/test/src/apidemos/elements/testElementGlobal.js +++ b/test/src/apidemos/elements/testElementGlobal.js @@ -21,11 +21,20 @@ describe('element global demos', function() { it('getText on element global instance', function() { const testsPath = path.join(__dirname, '../../../apidemos/elements/elementGlobalTest.js'); - Mocks.elementText(); - Mocks.tagName('0', 'div'); - Mocks.visible('0', true, { + MockServer.addMock({ + url: '/session/13521-10219-202/element/5cc459b8-36a8-3042-8b4a-258883ea642b/name', + method: 'GET', + response: JSON.stringify({ + value: 'div' + }) + }, true); + Mocks.w3cVisible(true, { times: 2 }); + Mocks.getElementText({ + elementId: '5cc459b8-36a8-3042-8b4a-258883ea642b', + responseText: 'sample text' + }); const globals = { waitForConditionPollInterval: 50, @@ -40,7 +49,8 @@ describe('element global demos', function() { }; return NightwatchClient.runTests(testsPath, settings({ - output: false, + selenium_host: null, + output: true, skip_testcases_on_fail: false, globals })); From 16097712f0d17f1eb4083c0c2abb78f78eb76e0e Mon Sep 17 00:00:00 2001 From: Priyansh Garg <39924567+garg3133@users.noreply.github.com> Date: Tue, 8 Aug 2023 21:20:51 +0530 Subject: [PATCH 09/15] Linting fix --- lib/api/_loaders/element-global.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/api/_loaders/element-global.js b/lib/api/_loaders/element-global.js index c0ab172c35..fa1c2a28b1 100644 --- a/lib/api/_loaders/element-global.js +++ b/lib/api/_loaders/element-global.js @@ -87,7 +87,7 @@ class ElementGlobal { return; } - const byLocator=Locator.create(locator); + const byLocator = Locator.create(locator); this.element = await this.transport.driver.wait(until.elementLocated(byLocator), this.timeout, null, this.retryInterval); } From 33476a72c3da3e2b17a6b05a5ceb58a1ca3ac5cb Mon Sep 17 00:00:00 2001 From: Praying Mantis Date: Wed, 9 Aug 2023 22:16:43 +0530 Subject: [PATCH 10/15] set the output to false for the tests of element api --- test/src/apidemos/elements/testElementGlobal.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/src/apidemos/elements/testElementGlobal.js b/test/src/apidemos/elements/testElementGlobal.js index ddf83f3021..190e98a84f 100644 --- a/test/src/apidemos/elements/testElementGlobal.js +++ b/test/src/apidemos/elements/testElementGlobal.js @@ -50,7 +50,7 @@ describe('element global demos', function() { return NightwatchClient.runTests(testsPath, settings({ selenium_host: null, - output: true, + output: false, skip_testcases_on_fail: false, globals })); From b6de5d525287984463a7f2d7219e780d556baf1a Mon Sep 17 00:00:00 2001 From: Praying Mantis Date: Fri, 11 Aug 2023 16:23:01 +0530 Subject: [PATCH 11/15] made element api to return the web element at the provided index --- lib/api/_loaders/element-global.js | 31 ++++++++++++++++++--- test/apidemos/elements/elementGlobalTest.js | 4 +-- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/api/_loaders/element-global.js b/lib/api/_loaders/element-global.js index fa1c2a28b1..74c967bec5 100644 --- a/lib/api/_loaders/element-global.js +++ b/lib/api/_loaders/element-global.js @@ -68,6 +68,7 @@ class ElementGlobal { this.abortOnFailure = this.settings.globals.abortOnElementLocateError; this.timeout = this.settings.globals.waitForConditionTimeout; this.retryInterval = this.settings.globals.waitForConditionPollInterval; + this.index = 0; this.init(); } @@ -89,7 +90,30 @@ class ElementGlobal { const byLocator = Locator.create(locator); - this.element = await this.transport.driver.wait(until.elementLocated(byLocator), this.timeout, null, this.retryInterval); + this.element = await this.transport.driver.wait(until.elementsLocated(byLocator), this.timeout, null, this.retryInterval); + + this.index = locator.__index; + + if (this.element.length === 0) { + throw new Error(`Element ${locator} was not found.`); + } + + if (!this.index){ + this.element = this.element[0]; + + return; + } + + if (typeof(locator.__index) != 'number'){ + throw new Error('Index should be of type number'); + } + + if (this.element.lenght < this.index){ + throw new Error('Index is out of bounds.'); + } + + this.element = this.element[this.index]; + } static isElementObject(element) { @@ -163,7 +187,7 @@ class ElementGlobal { value = { value: locator, using: this.client.locateStrategy - }; + }; } else { value = locator; } @@ -242,7 +266,6 @@ class ElementGlobal { if (commandName === 'findElement' && args.length === 0) { return this.element; } - args = this.computeArguments(args, commandName); let value; let error; @@ -269,7 +292,7 @@ class ElementGlobal { }); } - const lastArg = args[args.length-1]; + const lastArg = args[args.length - 1]; if (isFunction(lastArg)) { if (error) { return lastArg.call(this.api, { diff --git a/test/apidemos/elements/elementGlobalTest.js b/test/apidemos/elements/elementGlobalTest.js index e0e5a59127..e96925fe82 100644 --- a/test/apidemos/elements/elementGlobalTest.js +++ b/test/apidemos/elements/elementGlobalTest.js @@ -56,8 +56,8 @@ describe('get text using element-global', function () { test('test for element properties as argument', async function () { const weblogin = element({selector: '#weblogin', index: 1}); const id = await weblogin.getId(); - - assert.strictEqual(id, '5cc459b8-36a8-3042-8b4a-258883ea642b'); + + assert.strictEqual(id, '3783b042-7001-0740-a2c0-afdaac732e9f'); }); }); From 1633f8ebee9876a02d023ec6d6e7136c06025605 Mon Sep 17 00:00:00 2001 From: Praying Mantis Date: Sat, 12 Aug 2023 02:03:36 +0530 Subject: [PATCH 12/15] refactored code of findElement() in element-global.js file --- lib/api/_loaders/element-global.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/api/_loaders/element-global.js b/lib/api/_loaders/element-global.js index 482a21616f..ed8ab7afec 100644 --- a/lib/api/_loaders/element-global.js +++ b/lib/api/_loaders/element-global.js @@ -81,11 +81,12 @@ class ElementGlobal { return; } - const {locator} = this; - if ((locator instanceof Element) && locator.resolvedElement) { - this.element = this.createWebElement(locator.resolvedElement); + let {locator} = this; - return; + if (locator instanceof Element) { + this.setPropertiesFromElement(locator); + + locator = Locator.create(locator); } const byLocator = Locator.create(locator); @@ -95,14 +96,10 @@ class ElementGlobal { const elements = await this.transport.driver.wait(until.elementsLocated(byLocator), this.timeout, null, this.retryInterval); if (elements.length === 0) { - throw new Error(`Element ${byLocator} was not found.`); - } - - if (typeof(this.index) != 'number'){ - throw new Error(`Index ${this.index} is not of type number for locator: ${byLocator}`); + throw new Error(`No element was found for locator: ${byLocator}`); } - if (elements.length < this.index){ + if (elements.length <= this.index){ throw new Error(`Index ${this.index} out of bounds for locator: ${byLocator}`); } @@ -130,7 +127,7 @@ class ElementGlobal { abortOnFailure, retryInterval, timeout, suppressNotFoundErrors, index } = element; - if (isDefined(index)) { + if (isDefined(index) && typeof(index) == 'number') { this.index = index; } From d7194dd0b287eb55c75034ff700cf1c95f31b2ef Mon Sep 17 00:00:00 2001 From: Praying Mantis Date: Sat, 12 Aug 2023 02:22:51 +0530 Subject: [PATCH 13/15] made element api to return the web element at the provided index --- lib/api/_loaders/element-global.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/api/_loaders/element-global.js b/lib/api/_loaders/element-global.js index ed8ab7afec..d59c15c349 100644 --- a/lib/api/_loaders/element-global.js +++ b/lib/api/_loaders/element-global.js @@ -82,6 +82,11 @@ class ElementGlobal { } let {locator} = this; + if ((locator instanceof Element) && locator.resolvedElement) { + this.element = this.createWebElement(locator.resolvedElement); + + return; + } if (locator instanceof Element) { this.setPropertiesFromElement(locator); @@ -89,18 +94,14 @@ class ElementGlobal { locator = Locator.create(locator); } - const byLocator = Locator.create(locator); - - this.setPropertiesFromElement(locator); - - const elements = await this.transport.driver.wait(until.elementsLocated(byLocator), this.timeout, null, this.retryInterval); + const elements = await this.transport.driver.wait(until.elementsLocated(locator), this.timeout, null, this.retryInterval); if (elements.length === 0) { - throw new Error(`No element was found for locator: ${byLocator}`); + throw new Error(`No element was found for locator: ${locator}`); } if (elements.length <= this.index){ - throw new Error(`Index ${this.index} out of bounds for locator: ${byLocator}`); + throw new Error(`Index ${this.index} out of bounds for locator: ${locator}`); } this.element = elements[this.index]; From 2f42697c1ff92a8dc1229515e61daa00609ad0ac Mon Sep 17 00:00:00 2001 From: Praying Mantis Date: Sat, 12 Aug 2023 12:57:26 +0530 Subject: [PATCH 14/15] refactored the way of checking the type of index from using typeof to isNumber --- lib/api/_loaders/element-global.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/api/_loaders/element-global.js b/lib/api/_loaders/element-global.js index d59c15c349..28826b14bf 100644 --- a/lib/api/_loaders/element-global.js +++ b/lib/api/_loaders/element-global.js @@ -1,6 +1,6 @@ const {until, WebElement} = require('selenium-webdriver'); const Utils = require('../../utils'); -const {Logger, isUndefined, isObject, isString, isFunction} = Utils; +const {Logger, isUndefined, isObject, isString, isFunction, isNumber} = Utils; const isDefined = function(val) { return !isUndefined(val); }; @@ -128,7 +128,7 @@ class ElementGlobal { abortOnFailure, retryInterval, timeout, suppressNotFoundErrors, index } = element; - if (isDefined(index) && typeof(index) == 'number') { + if (isDefined(index) && isNumber(index)) { this.index = index; } From 5d6c1699bf8a4d91a5cc5a62342bfbbe97d9b18c Mon Sep 17 00:00:00 2001 From: Yash Pratap Singh <102533457+yashPratp983@users.noreply.github.com> Date: Tue, 5 Sep 2023 19:40:52 +0530 Subject: [PATCH 15/15] moved condition for handling for an element having instance of Element from findElement to setLocator method --- lib/api/_loaders/element-global.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/api/_loaders/element-global.js b/lib/api/_loaders/element-global.js index 28826b14bf..01984ff3ad 100644 --- a/lib/api/_loaders/element-global.js +++ b/lib/api/_loaders/element-global.js @@ -81,19 +81,13 @@ class ElementGlobal { return; } - let {locator} = this; + const {locator} = this; if ((locator instanceof Element) && locator.resolvedElement) { this.element = this.createWebElement(locator.resolvedElement); return; } - if (locator instanceof Element) { - this.setPropertiesFromElement(locator); - - locator = Locator.create(locator); - } - const elements = await this.transport.driver.wait(until.elementsLocated(locator), this.timeout, null, this.retryInterval); if (elements.length === 0) { @@ -128,7 +122,7 @@ class ElementGlobal { abortOnFailure, retryInterval, timeout, suppressNotFoundErrors, index } = element; - if (isDefined(index) && isNumber(index)) { + if (isDefined(index) && isNumber(index) && !isNaN(index)) { this.index = index; } @@ -171,7 +165,9 @@ class ElementGlobal { } if (locator instanceof Element) { - this.locator = locator; + this.setPropertiesFromElement(locator); + + this.locator = Locator.create(locator); } else { let value; if (isString(locator) && this.client && this.client.locateStrategy) {