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

Added isPresent command #4060

Closed
wants to merge 12 commits into from
Closed
30 changes: 30 additions & 0 deletions lib/api/web-element/commands/isPresent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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.
*/
piyushmishra1416 marked this conversation as resolved.
Show resolved Hide resolved
module.exports.command = function () {
return this.runQueuedCommandScoped('isElementPresent');
};
17 changes: 17 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} = 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,22 @@ 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
}
},



async clearElementValue(webElementOrId) {
const element = this.getWebElement(webElementOrId);
await element.clear();
Expand Down
55 changes: 55 additions & 0 deletions test/src/api/commands/web-element/testIsPresent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const assert = require('assert');
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() {
before(function(done) {
CommandGlobals.beforeEach.call(this, done);
});

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

it('test .element().isPresent() for element 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, true, 'Expected element to be present in the DOM');
});

it('test .element().isPresent() for element not present', async function() {

const resultPromise = this.client.api.element('#signupSecti').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, false, 'Expected element not to be present in the DOM');
});

// Example of a test for an async scenario using .isPresent()
it('test async .element().isPresent() for element present', async function() {

const result = await this.client.api.element('#signupSection').isPresent();
assert.strictEqual(result, true, 'Expected element to be present in the DOM using async/await');
});

// Test to ensure .isPresent() correctly handles WebDriver errors
it('test .element().isPresent() handles errors', async function() {

const resultPromise = this.client.api.element('#badElement').isPresent();
const result = await resultPromise;
assert.strictEqual(result, false, 'Expected .isPresent() to gracefully handle WebDriver errors');
});
});
1 change: 1 addition & 0 deletions types/tests/webElement.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ describe('new element() api', function () {
expectType<ElementValue<string | null>>(elem.getProperty('property-name'));
expectType<ElementValue<string | null>>(elem.getAttribute('attrib-name'));
expectType<ElementValue<string | null>>(elem.getValue());
expectType<ElementValue<boolean>>(elem.isPresent());
expectType<ElementValue<boolean>>(elem.isEnabled());

expectType<ElementValue<ScopedElementRect>>(elem.getRect());
Expand Down
3 changes: 3 additions & 0 deletions types/web-element.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,12 @@ export interface ScopedElement extends Element, PromiseLike<WebElement> {

clickAndHold(): Promise<WebElement>;


doubleClick(): Promise<WebElement>;

rightClick(): Promise<WebElement>;

isPresent(): ElementValue<boolean>;

isSelected(): ElementValue<boolean>;

Expand Down