Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya123473892 committed Feb 26, 2024
1 parent c990fc6 commit 25197c1
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/tests/duckDuckGo.js
Expand Up @@ -6,6 +6,7 @@ describe('duckduckgo example', function() {
browser
.navigateTo('https://duckduckgo.com')
.waitForElementVisible('body')
.isPresent('button')
.assert.visible('input[name="q"]')
.sendKeys('input[name="q"]', ['Nightwatch.js'])
.assert.visible('button[type=submit]')
Expand Down
30 changes: 30 additions & 0 deletions lib/api/web-element/commands/isPresent.js
@@ -0,0 +1,30 @@
/**
* Determines if an element is present.
*
* 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.5.0
* @method isPresent
* @memberof ScopedWebElement
* @instance
* @syntax browser.element(selector).isPresent()
* @see https://www.w3.org/TR/webdriver/#is-element-selected
* @returns {ScopedValue<boolean>}
*/
module.exports.command = function () {
return this.runQueuedCommandScoped('isElementPresent');
};
105 changes: 105 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -68,6 +68,7 @@
"mochawesome-merge": "^4.2.1",
"mochawesome-report-generator": "^6.2.0",
"mockery": "~2.1.0",
"nightwatch": "^3.4.1",
"nock": "^13.2.9",
"nyc": "^15.1.0",
"react": "^18.2.0",
Expand Down
120 changes: 120 additions & 0 deletions test/src/api/commands/web-element/testisPresent.js
@@ -0,0 +1,120 @@
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() present', async function () {
MockServer.addMock({
url: '/session/13521-10219-202/element/0/displayed',
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().isPresent() not present', async function () {
MockServer.addMock({
url: '/session/13521-10219-202/element/0/displayed',
method: 'GET',
response: JSON.stringify({
value: false
})
}, 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, false);
});

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

it('test .element().isPresent() assert', async function () {
MockServer.addMock({
url: '/session/13521-10219-202/element/0/displayed',
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);
});
});
4 changes: 4 additions & 0 deletions types/tests/webElement.test-d.ts
Expand Up @@ -162,9 +162,13 @@ describe('new element() api', function () {
expectType<Promise<WebElement>>(elem.clickAndHold());
expectType<Promise<WebElement>>(elem.doubleClick());
expectType<Promise<WebElement>>(elem.rightClick());

expectType<Promise<WebElement>>(elem.waitUntil('visible', {timeout: 5000}));

expectType<ElementValue<boolean>>(elem.isPresent());
});


test('test element assertions', async function () {
const elem = browser.element('selector');
expectType<ScopedElement>(elem);
Expand Down
4 changes: 4 additions & 0 deletions types/web-element.d.ts
Expand Up @@ -187,6 +187,10 @@ export interface ScopedElement extends Element, PromiseLike<WebElement> {

rightClick(): Promise<WebElement>;


isPresent(): ElementValue<boolean>;


waitUntil(signalOrOptions: WaitUntilActions | WaitUntilOptions, waitOptions?: WaitUntilOptions): Promise<WebElement>;
}

Expand Down

0 comments on commit 25197c1

Please sign in to comment.