From f9615e51618b7a3301409aa346235c257f1ef419 Mon Sep 17 00:00:00 2001 From: Subhajit Ghosh <99127578+subhajit20@users.noreply.github.com> Date: Wed, 28 Feb 2024 22:52:34 +0530 Subject: [PATCH] Add missing `isEnabled` command in new Element API (#4041) Co-authored-by: Priyansh Garg --- lib/api/web-element/commands/isEnabled.js | 30 +++++ .../api/commands/web-element/testIsEnabled.js | 122 ++++++++++++++++++ types/tests/webElement.test-d.ts | 1 + types/web-element.d.ts | 2 + 4 files changed, 155 insertions(+) create mode 100644 lib/api/web-element/commands/isEnabled.js create mode 100644 test/src/api/commands/web-element/testIsEnabled.js diff --git a/lib/api/web-element/commands/isEnabled.js b/lib/api/web-element/commands/isEnabled.js new file mode 100644 index 0000000000..349029dc66 --- /dev/null +++ b/lib/api/web-element/commands/isEnabled.js @@ -0,0 +1,30 @@ +/** + * Determines if an element is enabled. + * + * For more info on working with DOM elements in Nightwatch, refer to the Finding & interacting with DOM Elements guide page. + * + * @example + * describe('isEnabled Demo', function() { + * it('test isEnabled', function(browser) { + * browser.element('#search') + * .isEnabled() + * .assert.equals(true); + * }); + * + * it('test async isEnabled', async function(browser) { + * const result = await browser.element('#search').isEnabled(); + * browser.assert.equal(result, true); + * }); + * }); + * + * @since 3.5.0 + * @method isEnabled + * @memberof ScopedWebElement + * @instance + * @syntax browser.element(selector).isEnabled() + * @see https://www.w3.org/TR/webdriver/#is-element-enabled + * @returns {ScopedValue} + */ +module.exports.command = function () { + return this.runQueuedCommandScoped('isElementEnabled'); +}; diff --git a/test/src/api/commands/web-element/testIsEnabled.js b/test/src/api/commands/web-element/testIsEnabled.js new file mode 100644 index 0000000000..e807742aae --- /dev/null +++ b/test/src/api/commands/web-element/testIsEnabled.js @@ -0,0 +1,122 @@ +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().isEnabled() command', function() { + before(function (done) { + CommandGlobals.beforeEach.call(this, done); + }); + + after(function (done) { + CommandGlobals.afterEach.call(this, done); + }); + + it('test .element().isEnabled() enabled', async function() { + MockServer.addMock({ + url: '/session/13521-10219-202/element/0/enabled', + method: 'GET', + response: JSON.stringify({ + value: true + }) + }, true); + + const resultPromise = this.client.api.element('#signupSection').isEnabled(); + 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().isEnabled() not enabled', async function() { + MockServer.addMock({ + url: '/session/13521-10219-202/element/0/enabled', + method: 'GET', + response: JSON.stringify({ + value: false + }) + }, true); + + const resultPromise = this.client.api.element('#signupSection').isEnabled(); + 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().isEnabled()', async function() { + MockServer.addMock({ + url: '/session/13521-10219-202/element/1/enabled', + method: 'GET', + response: JSON.stringify({ + value: true + }) + }, true); + + const resultPromise = this.client.api.element('#signupSection').find('#helpBtn').isEnabled(); + 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().isEnabled() not enabled', async function() { + MockServer.addMock({ + url: '/session/13521-10219-202/element/0/enabled', + method: 'GET', + response: JSON.stringify({ + value: false + }) + }, true); + + const resultPromise = this.client.api.element.find('#signupSection').isEnabled(); + 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().isEnabled() assert', async function() { + MockServer.addMock({ + url: '/session/13521-10219-202/element/0/enabled', + method: 'GET', + response: JSON.stringify({ + value: true + }) + }, true); + + const resultPromise = this.client.api.element('#signupSection').isEnabled(); + 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); + }); + +}); diff --git a/types/tests/webElement.test-d.ts b/types/tests/webElement.test-d.ts index c73b892691..f179296b6f 100644 --- a/types/tests/webElement.test-d.ts +++ b/types/tests/webElement.test-d.ts @@ -140,6 +140,7 @@ describe('new element() api', function () { expectType>(elem.getProperty('property-name')); expectType>(elem.getAttribute('attrib-name')); expectType>(elem.getValue()); + expectType>(elem.isEnabled()); expectType>(elem.getRect()); expectType>(elem.getSize()); diff --git a/types/web-element.d.ts b/types/web-element.d.ts index 025e2f2f84..ea28dc8516 100644 --- a/types/web-element.d.ts +++ b/types/web-element.d.ts @@ -190,6 +190,8 @@ export interface ScopedElement extends Element, PromiseLike { isSelected(): ElementValue; waitUntil(signalOrOptions: WaitUntilActions | WaitUntilOptions, waitOptions?: WaitUntilOptions): Promise; + + isEnabled(): ElementValue; } type WaitUntilOptions = {