From a928f65713b21cd48129e4288fe589011684464e Mon Sep 17 00:00:00 2001 From: Binayak Ghosh Date: Tue, 16 May 2023 06:36:47 -0700 Subject: [PATCH] fix #3571: return correct status code for waitForElementNotPresent (#3678) * fix nightwatchjs/nightwatch#3571; return correct status code for waitForElementNotPresent * --------- Co-authored-by: Harshit Agrawal Co-authored-by: Harshit Agrawal <94462364+harshit-bs@users.noreply.github.com> Co-authored-by: Binayak Ghosh * update tests * Revert "Feat/rerun (#3703)" This reverts commit 020601e08b5875509018f3777ba840705b50c454. * revert and skip hasAttribute test * refactor tests --------- Co-authored-by: Prudhvi Co-authored-by: Harshit Agrawal Co-authored-by: Harshit Agrawal <94462364+harshit-bs@users.noreply.github.com> --- .../waitForElementNotPresent.js | 2 + lib/reporter/index.js | 4 +- test/lib/globals.js | 5 --- .../sampletests/withwait/elementNotPresent.js | 18 ++++++++ .../api/commands/element/testSubmitForm.js | 5 +-- .../element/testWaitForElementNotPresent.js | 41 +++++++++++++++++++ 6 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 test/sampletests/withwait/elementNotPresent.js diff --git a/lib/api/element-commands/waitForElementNotPresent.js b/lib/api/element-commands/waitForElementNotPresent.js index 3442f2474e..7f053eec3f 100644 --- a/lib/api/element-commands/waitForElementNotPresent.js +++ b/lib/api/element-commands/waitForElementNotPresent.js @@ -100,12 +100,14 @@ class WaitForElementNotPresent extends WaitForElement { */ elementFound(result) { let defaultMsg = 'Timed out while waiting for element <%s> to be removed for %d milliseconds.'; + result.passed = false; return this.fail(result, 'found', this.expectedValue, defaultMsg); } elementNotFound(result) { let defaultMsg = 'Element <%s> was not present after %d milliseconds.'; + result.passed = true; return this.pass(result, defaultMsg, this.executor.elapsedTime); } diff --git a/lib/reporter/index.js b/lib/reporter/index.js index 7e6b03e873..041a268146 100644 --- a/lib/reporter/index.js +++ b/lib/reporter/index.js @@ -188,8 +188,8 @@ class Reporter extends SimplifiedReporter { try { let commandResult = result; - const isSuccess = result === undefined || - result == null || + let isSuccess = result === undefined || + result == null || result.passed || !((result instanceof Error) || (result.error instanceof Error) || result.status === -1 diff --git a/test/lib/globals.js b/test/lib/globals.js index 6d4d1c9444..73e05eabf7 100644 --- a/test/lib/globals.js +++ b/test/lib/globals.js @@ -132,11 +132,6 @@ class Globals { } client.queue.tree.empty().createRootNode(); - client.queue.once('queue:finished', err => { - if (err) { - reject(err); - } - }); client.isES6AsyncTestcase = true; diff --git a/test/sampletests/withwait/elementNotPresent.js b/test/sampletests/withwait/elementNotPresent.js new file mode 100644 index 0000000000..e074ab8bba --- /dev/null +++ b/test/sampletests/withwait/elementNotPresent.js @@ -0,0 +1,18 @@ +module.exports = { + before(client) { + client.globals.calls++; + }, + + demoTest(client) { + client.url('http://localhost') + .waitForElementNotPresent('#badElement') + .waitForElementNotPresent('#weblogin') + .end(); + }, + + after(client, callback) { + client.globals.calls++; + callback(); + } +}; + \ No newline at end of file diff --git a/test/src/api/commands/element/testSubmitForm.js b/test/src/api/commands/element/testSubmitForm.js index 225f033f95..cde56a3dd3 100644 --- a/test/src/api/commands/element/testSubmitForm.js +++ b/test/src/api/commands/element/testSubmitForm.js @@ -16,9 +16,8 @@ describe('submitForm', function() { url: '/wd/hub/session/1352110219202/element/0/submit', method: 'POST', response: JSON.stringify({ - sessionId: '1352110219202', - status: 0 - }) + value: null + }, true, true) }); this.client.api.submitForm('#weblogin', function callback(result) { diff --git a/test/src/api/commands/element/testWaitForElementNotPresent.js b/test/src/api/commands/element/testWaitForElementNotPresent.js index 24918cbc48..37ef29a7f8 100644 --- a/test/src/api/commands/element/testWaitForElementNotPresent.js +++ b/test/src/api/commands/element/testWaitForElementNotPresent.js @@ -1,6 +1,12 @@ +const path = require('path'); const assert = require('assert'); const CommandGlobals = require('../../../../lib/globals/commands.js'); const {strictEqual} = assert; +const common = require('../../../../common.js'); +const {settings} = common; +const NightwatchClient = common.require('index.js'); +const MockServer = require('../../../../lib/mockserver.js'); + describe('waitForElementNotPresent', function () { let commandResult; @@ -73,4 +79,39 @@ describe('waitForElementNotPresent', function () { strictEqual(commandInstance.rescheduleInterval, 10); }); }); + + + it('client.waitForElementNotPresent() report should not contain error in case of success', async function() { + MockServer.addMock({ + url: '/wd/hub/session/1352110219202/elements', + method: 'POST', + postdata: { + using: 'css selector', + value: '#badElement' + }, + response: JSON.stringify({ + value: [] + }), + times: 2 + }); + + const testsPath = [ + path.join(__dirname, '../../../../sampletests/withwait/elementNotPresent.js') + ]; + + const globals = { + calls: 0, + reporter(results) { + assert.strictEqual(globals.calls, 2); + assert.strictEqual(Object.keys(results.modules).length, 1); + assert.ok('elementNotPresent' in results.modules); + assert.strictEqual(results.modulesWithEnv.default.elementNotPresent.completedSections.demoTest.commands[1].status, 'pass'); + assert.strictEqual(results.modulesWithEnv.default.elementNotPresent.completedSections.demoTest.commands[2].status, 'fail'); + } + }; + + await NightwatchClient.runTests(testsPath, settings({ + globals + })); + }); });