Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isPresent command to new Element API. #4216

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/api/web-element/commands/isPresent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Checks if an element is present in the DOM.
*
* This command is useful for verifying the presence of elements that may not be visible or interactable.
* For more information on working with DOM elements in Nightwatch, refer to the <a href="https://nightwatchjs.org/guide/working-with-page-elements/finding-elements.html">Finding Elements</a> guide page.
*
* @example
* describe('isPresent Demo', function() {
* it('test isPresent', function(browser) {
* browser.element('#search')
* .isPresent()
* .assert.equals(true);
* });
*
* it('test async isPresent', async function(browser) {
* const result = await browser.element('#search').isPresent();
* browser.assert.equal(result, true);
* });
* });
*
* @since 3.5.0
* @method isPresent
* @memberof ScopedWebElement
* @instance
* @syntax browser.element(selector).isPresent()
* @returns {ScopedValue<boolean>} A boolean value indicating if the element is present in the DOM.
*/

module.exports.command = function () {
return this.runQueuedCommandScoped('isElementPresent', {suppressNotFoundErrors: true});
};
10 changes: 9 additions & 1 deletion lib/api/web-element/scoped-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ class ScopedWebElement {
return true;
}

get suppressNotFoundErrors() {
return this._suppressNotFoundErrors;
}

constructor(selector = 'html', parentElement, nightwatchInstance) {
this.nightwatchInstance = nightwatchInstance;
this.parentScopedElement = parentElement;
Expand Down Expand Up @@ -172,7 +176,7 @@ class ScopedWebElement {

return await this.findElement({parentElement, selector: condition, index, timeout, retryInterval});
} catch (error) {
if (suppressNotFoundErrors) {
if (this.suppressNotFoundErrors ?? suppressNotFoundErrors) {
garg3133 marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

Expand Down Expand Up @@ -286,6 +290,10 @@ class ScopedWebElement {
});
};

if (args[0]?.suppressNotFoundErrors) {
this._suppressNotFoundErrors = true;
}

garg3133 marked this conversation as resolved.
Show resolved Hide resolved
const node = this.queueAction({name: commandName, createAction});

return node;
Expand Down
4 changes: 2 additions & 2 deletions lib/core/treenode.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ class TreeNode {
err.abortOnFailure = err.abortOnFailure || err.abortOnFailure === undefined;
}

let errorName = err.name !== 'Error' ? `[${err.name}] ` : '';
let originalError = `${errorName}${err.message}`;
const errorName = err.name !== 'Error' ? `[${err.name}] ` : '';
const originalError = `${errorName}${err.message}`;

if (this.stackTrace && Utils.shouldReplaceStack(err)) {
err.stack = this.stackTrace;
Expand Down
15 changes: 15 additions & 0 deletions lib/transport/selenium-webdriver/method-mappings.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {WebElement, WebDriver, Origin, By, until, Condition, Key} = require('selenium-webdriver');
const {ShadowRoot} = require('selenium-webdriver/lib/webdriver');
const {Locator} = require('../../element');
const NightwatchLocator = require('../../element/locator-factory.js');
const {isString} = require('../../utils');
Expand Down Expand Up @@ -583,6 +584,20 @@ module.exports = class MethodMappings {
return value;
},

async isElementPresent(webElement) {
try {
// New element apis contains only webElement argument
const element = await webElement;

return element instanceof WebElement || element instanceof ShadowRoot;
} catch (error) {
if (error.name === 'NoSuchElementError') {
return false; // Element does not exist
}
throw error; // Re-throw other errors for further handling
}
garg3133 marked this conversation as resolved.
Show resolved Hide resolved
},

async clearElementValue(webElementOrId) {
const element = this.getWebElement(webElementOrId);
await element.clear();
Expand Down
71 changes: 71 additions & 0 deletions test/src/api/commands/web-element/testIsPresent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const assert = require('assert');
const {WebElement} = require('selenium-webdriver');
const MockServer = require('../../../../lib/mockserver.js');
const CommandGlobals = require('../../../../lib/globals/commands-w3c.js');
const common = require('../../../../common.js');
const Element = common.require('element/index.js');

describe('element().isPresent() command', function() {
garg3133 marked this conversation as resolved.
Show resolved Hide resolved
before(function (done) {
CommandGlobals.beforeEach.call(this, done);
});

after(function (done) {
CommandGlobals.afterEach.call(this, done);
});

it('test .element().isPresent() present', async function() {
const resultPromise = this.client.api.element('#signupSection').isPresent();
assert.strictEqual(resultPromise instanceof Element, false);
assert.strictEqual(typeof resultPromise.find, 'undefined');

assert.strictEqual(resultPromise instanceof Promise, false);
assert.strictEqual(typeof resultPromise.then, 'function');

const result = await resultPromise;
assert.strictEqual(result instanceof WebElement, false);
assert.strictEqual(result, true);

});

it('test .element().isPresent() not present', async function() {
const resultPromise = this.client.api.element('#wrong').isPresent();
assert.strictEqual(resultPromise instanceof Element, false);
assert.strictEqual(typeof resultPromise.find, 'undefined');

assert.strictEqual(resultPromise instanceof Promise, false);
assert.strictEqual(typeof resultPromise.then, 'function');

const result = await resultPromise;
assert.strictEqual(result instanceof WebElement, false);
assert.strictEqual(result, false);

});

it('test .element().find().isPresent() present', async function() {
const resultPromise = this.client.api.element('#signupSection').find('#helpBtn').isPresent();
assert.strictEqual(resultPromise instanceof Element, false);
assert.strictEqual(typeof resultPromise.find, 'undefined');

assert.strictEqual(resultPromise instanceof Promise, false);
assert.strictEqual(typeof resultPromise.then, 'function');

const result = await resultPromise;
assert.strictEqual(result instanceof WebElement, false);
assert.strictEqual(result, true);
});

it('test .element().find().isPresent() not present', async function() {
const resultPromise = this.client.api.element('#signupSection').find('#wrong').isPresent();
assert.strictEqual(resultPromise instanceof Element, false);
assert.strictEqual(typeof resultPromise.find, 'undefined');

assert.strictEqual(resultPromise instanceof Promise, false);
assert.strictEqual(typeof resultPromise.then, 'function');

const result = await resultPromise;
assert.strictEqual(result instanceof WebElement, false);
assert.strictEqual(result, false);
});

});
Loading