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 missing isPresent command in new Element API #4086

Closed
Closed
Show file tree
Hide file tree
Changes from all 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 the element is present in the DOM.
*
* For more info on working with DOM elements in Nightwatch, refer to the <a href="https://nightwatchjs.org/guide/writing-tests/finding-interacting-with-dom-elements.html">Finding & interacting with DOM 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.0.0
* @method isPresent
* @memberof ScopedWebElement
* @instance
* @syntax browser.element(selector).isPresent()
* @see https://www.w3.org/TR/webdriver/#is-element-present
* @returns {ScopedValue<boolean>}
*/

module.exports.command = function () {
return this.runQueuedCommandScoped('isElementPresent');
};
7 changes: 7 additions & 0 deletions lib/transport/selenium-webdriver/method-mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const NightwatchLocator = require('../../element/locator-factory.js');
const {isString} = require('../../utils');
const fs = require('fs');
const cdp = require('./cdp.js');
const {ShadowRoot} = require('selenium-webdriver/lib/webdriver');

module.exports = class MethodMappings {
get driver() {
Expand Down Expand Up @@ -576,6 +577,12 @@ module.exports = class MethodMappings {
return value;
},

async isElementPresent(webElementOrId) {
const element = await this.getWebElement(webElementOrId);

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

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

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

it('test .element().isPresent()', async function () {
MockServer.addMock({
url: '/session/13521-10219-202/element/0/property/present',
method: 'GET',
response: JSON.stringify({
value: true
})
}, true);

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().find().isPresent()', async function () {
MockServer.addMock({
url: '/session/13521-10219-202/element/1/property/present',
method: 'GET',
response: JSON.stringify({
value: true
})
}, true);

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()', async function () {
MockServer.addMock({
url: '/session/13521-10219-202/element/0/property/present',
method: 'GET',
response: JSON.stringify({
value: true
})
}, true);

const resultPromise = this.client.api.element.find('#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() assert', async function () {
MockServer.addMock({
url: '/session/13521-10219-202/element/0/property/present',
method: 'GET',
response: JSON.stringify({
value: true
})
}, true);

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

assert.strictEqual(await resultPromise.assert.equals(true), true);
assert.strictEqual(await resultPromise.assert.not.equals(false), true);
});

});
2 changes: 2 additions & 0 deletions types/tests/webElement.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ describe('new element() api', function () {
expectType<ElementValue<string | null>>(elem.getAttribute('attrib-name'));
expectType<ElementValue<string | null>>(elem.getValue());
expectType<ElementValue<boolean>>(elem.isEnabled());
expectType<ElementValue<boolean>>(elem.isPresent());


expectType<ElementValue<ScopedElementRect>>(elem.getRect());
expectType<ElementValue<ScopedElementRect>>(elem.getSize());
Expand Down
2 changes: 2 additions & 0 deletions types/web-element.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ export interface ScopedElement extends Element, PromiseLike<WebElement> {
waitUntil(signalOrOptions: WaitUntilActions | WaitUntilOptions, waitOptions?: WaitUntilOptions): Promise<WebElement>;

isEnabled(): ElementValue<boolean>;

isPresent(): ElementValue<boolean>;
}

type WaitUntilOptions = {
Expand Down