forked from nightwatchjs/nightwatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
isPresent
command to new Element API. (nightwatchjs#4216)
Co-authored-by: Priyansh Garg <[email protected]>
- Loading branch information
1 parent
cb54217
commit 92f7c8e
Showing
6 changed files
with
172 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* 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.7.1 | ||
* @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}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
describe('test', function () { | ||
test('test isPresent', async (browser) => { | ||
browser | ||
.element('#wrong').isPresent().assert.equals(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
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() { | ||
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().isPresent() suppresses 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: [] | ||
}) | ||
}); | ||
|
||
let globalReporterCalled = false; | ||
|
||
const globals = { | ||
reporter(results) { | ||
globalReporterCalled = true; | ||
if (Object.prototype.hasOwnProperty.call(results, 'lastError')) { | ||
assert.notStrictEqual(results.lastError.name, 'NoSuchElementError'); | ||
} | ||
}, | ||
waitForConditionTimeout: 100 | ||
}; | ||
const testsPath = [ | ||
path.join(__dirname, '../../../../sampletests/isPresent/elementNotPresent.js') | ||
]; | ||
|
||
await NightwatchClient.runTests(testsPath, settings({ | ||
globals, | ||
output_folder: 'output', | ||
selenium_host: null | ||
})); | ||
|
||
assert.strictEqual(globalReporterCalled, true); | ||
}); | ||
}); |