From 5915fa3a34e9a689dac5be0124f16703f6a0d988 Mon Sep 17 00:00:00 2001 From: mohitejaikumar Date: Tue, 31 Dec 2024 19:11:09 +0530 Subject: [PATCH] Fix NoSuchElementError suppressed in assert.not.present() --- lib/api/web-element/assert/element-assertions.js | 15 +++++++++++---- lib/api/web-element/scoped-element.js | 4 ++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/api/web-element/assert/element-assertions.js b/lib/api/web-element/assert/element-assertions.js index 170e8b3fb..a25aa2927 100644 --- a/lib/api/web-element/assert/element-assertions.js +++ b/lib/api/web-element/assert/element-assertions.js @@ -1,12 +1,17 @@ const {WebElement} = require('selenium-webdriver'); class ScopedElementAssertions { - constructor(scopedElement, {negated, nightwatchInstance}) { + constructor(scopedElement, {negated, nightwatchInstance}, suppressNotFoundErrors = false) { this.negated = negated; this.scopedElement = scopedElement; + this.scopedElement.suppressNotFoundErrors = suppressNotFoundErrors; this.nightwatchInstance = nightwatchInstance; } + set suppressNotFoundErrors(value){ + this.scopedElement.suppressNotFoundErrors = value; + } + assert(callback) { // The below promise is returned to the test case by the assertion, so // it should fail in case the actual assertion (callback) fails. In case @@ -41,11 +46,11 @@ class ScopedElementAssertions { } } -module.exports.create = function createAssertions(scopedElement, {negated = false, nightwatchInstance}) { +module.exports.create = function createAssertions(scopedElement, {negated = false, nightwatchInstance}, suppressNotFoundErrors = false) { const instance = new ScopedElementAssertions(scopedElement, { negated, nightwatchInstance - }); + }, suppressNotFoundErrors); const exported = {}; Object.defineProperty(exported, 'not', { @@ -53,7 +58,7 @@ module.exports.create = function createAssertions(scopedElement, {negated = fals return createAssertions(scopedElement, { negated: true, nightwatchInstance - }); + }, suppressNotFoundErrors = true); } }); @@ -88,6 +93,8 @@ module.exports.create = function createAssertions(scopedElement, {negated = fals }, present: { value(message) { + instance.suppressNotFoundErrors = true; + return instance.assert(async (assertApi, element) => { const el = await element; const id = await el.getId(); diff --git a/lib/api/web-element/scoped-element.js b/lib/api/web-element/scoped-element.js index 49c3a5b77..fc90ef6ec 100644 --- a/lib/api/web-element/scoped-element.js +++ b/lib/api/web-element/scoped-element.js @@ -74,6 +74,10 @@ class ScopedWebElement { return this._suppressNotFoundErrors; } + set suppressNotFoundErrors(value) { + this._suppressNotFoundErrors = value; + } + constructor(selector = 'html', parentElement, nightwatchInstance) { this.nightwatchInstance = nightwatchInstance; this.parentScopedElement = parentElement;