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 8 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});
};
14 changes: 13 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 @@ -165,14 +169,18 @@ class ScopedWebElement {

const parentElement = args[0];

if (suppressNotFoundErrors) {
this._suppressNotFoundErrors = true;
}

try {
if (condition.usingRecursion) {
return await this.findElementUsingRecursion({parentElement, recursiveElement: condition, timeout, retryInterval});
}

return await this.findElement({parentElement, selector: condition, index, timeout, retryInterval});
} catch (error) {
if (suppressNotFoundErrors) {
if (this._suppressNotFoundErrors) {
return null;
}

Expand Down Expand Up @@ -273,6 +281,10 @@ class ScopedWebElement {
}

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

const createAction = (actions, webElement) => function () {
if (isFunction(commandName)) {
return commandName(webElement, ...args).then((result) => {
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
7 changes: 7 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,12 @@ module.exports = class MethodMappings {
return value;
},

async isElementPresent(webElement) {
const element = await webElement;

return element instanceof WebElement || element instanceof ShadowRoot;
},

async clearElementValue(webElementOrId) {
const element = this.getWebElement(webElementOrId);
await element.clear();
Expand Down
6 changes: 6 additions & 0 deletions test/sampletests/isPresent/isPresentElementNotPresent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
describe('test', function () {
test('test setPassword', async (browser) => {
browser
.element('#wrong').isPresent().assert.equals(false);
});
});
107 changes: 107 additions & 0 deletions test/src/api/commands/web-element/testIsPresent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const assert = require('assert');
const {WebElement} = require('selenium-webdriver');
const path = require('path');
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');
const Utils = common.require('./utils');
const NightwatchClient = common.require('index.js');
const {settings} = common;

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);
});

it('test .element().find().isPresent() suppressNotFoundErrors should not throw NoSuchElementError', async function() {

MockServer.addMock({
url: '/session/13521-10219-202/elements',
method: 'POST',
postdata: JSON.stringify({using: 'css selector', value: '#wrong'}),
response: JSON.stringify({
value: []
})
});

const globals = {
reporter(results) {
if (Object.prototype.hasOwnProperty.call(results, 'lastError')) {
assert.notStrictEqual(results.lastError.name, 'NoSuchElementError');
}
},
waitForConditionTimeout: 100
};
const testsPath = [
path.join(__dirname, '../../../../sampletests/isPresent/isPresentElementNotPresent.js')
];

await NightwatchClient.runTests(testsPath, settings({
globals,
output_folder: 'output',
selenium_host: null
}));

});

});
Loading