From 8462c1602bd48af70e8022fd06bc81e0a5c5cb71 Mon Sep 17 00:00:00 2001 From: Aniket Singh Rawat <31622972+dikwickley@users.noreply.github.com> Date: Wed, 28 Feb 2024 22:51:34 +0530 Subject: [PATCH] Add missing `isSelected` command to new Element API. (#4038) Co-authored-by: Priyansh Garg Co-authored-by: Vaibhav Singh --- lib/api/web-element/commands/isSelected.js | 30 +++++ .../commands/web-element/testIsSelected.js | 123 ++++++++++++++++++ types/tests/webElement.test-d.ts | 1 + types/web-element.d.ts | 2 + 4 files changed, 156 insertions(+) create mode 100644 lib/api/web-element/commands/isSelected.js create mode 100644 test/src/api/commands/web-element/testIsSelected.js diff --git a/lib/api/web-element/commands/isSelected.js b/lib/api/web-element/commands/isSelected.js new file mode 100644 index 0000000000..6e9bb605e9 --- /dev/null +++ b/lib/api/web-element/commands/isSelected.js @@ -0,0 +1,30 @@ +/** + * Determines if an element is selected. + * + * For more info on working with DOM elements in Nightwatch, refer to the Finding & interacting with DOM Elements guide page. + * + * @example + * describe('isSelected Demo', function() { + * it('test isSelected', function(browser) { + * browser.element('#search') + * .isSelected() + * .assert.equals(true); + * }); + * + * it('test async isSelected', async function(browser) { + * const result = await browser.element('#search').isSelected(); + * browser.assert.equal(result, true); + * }); + * }); + * + * @since 3.5.0 + * @method isSelected + * @memberof ScopedWebElement + * @instance + * @syntax browser.element(selector).isSelected() + * @see https://www.w3.org/TR/webdriver/#is-element-selected + * @returns {ScopedValue} + */ +module.exports.command = function () { + return this.runQueuedCommandScoped('isElementSelected'); +}; diff --git a/test/src/api/commands/web-element/testIsSelected.js b/test/src/api/commands/web-element/testIsSelected.js new file mode 100644 index 0000000000..c9813f0383 --- /dev/null +++ b/test/src/api/commands/web-element/testIsSelected.js @@ -0,0 +1,123 @@ +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().isSelected() command', function() { + before(function (done) { + CommandGlobals.beforeEach.call(this, done); + }); + + after(function (done) { + CommandGlobals.afterEach.call(this, done); + }); + + it('test .element().isSelected() selected', async function() { + MockServer.addMock({ + url: '/session/13521-10219-202/element/0/selected', + method: 'GET', + response: JSON.stringify({ + value: true + }) + }, true); + + const resultPromise = this.client.api.element('#signupSection').isSelected(); + 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().isSelected() not selected', async function() { + MockServer.addMock({ + url: '/session/13521-10219-202/element/0/selected', + method: 'GET', + response: JSON.stringify({ + value: false + }) + }, true); + + const resultPromise = this.client.api.element('#signupSection').isSelected(); + 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().isSelected()', async function() { + MockServer.addMock({ + url: '/session/13521-10219-202/element/1/selected', + method: 'GET', + response: JSON.stringify({ + value: true + }) + }, true); + + const resultPromise = this.client.api.element('#signupSection').find('#helpBtn').isSelected(); + 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().isSelected() not selected', async function() { + MockServer.addMock({ + url: '/session/13521-10219-202/element/0/selected', + method: 'GET', + response: JSON.stringify({ + value: false + }) + }, true); + + const resultPromise = this.client.api.element.find('#signupSection').isSelected(); + 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().isSelected() assert', async function() { + MockServer.addMock({ + url: '/session/13521-10219-202/element/0/selected', + method: 'GET', + response: JSON.stringify({ + value: true + }) + }, true); + + const resultPromise = this.client.api.element('#signupSection').isSelected(); + 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 a85627953f..c73b892691 100644 --- a/types/tests/webElement.test-d.ts +++ b/types/tests/webElement.test-d.ts @@ -163,6 +163,7 @@ describe('new element() api', function () { expectType>(elem.doubleClick()); expectType>(elem.rightClick()); expectType>(elem.waitUntil('visible', {timeout: 5000})); + expectType>(elem.isSelected()); }); test('test element assertions', async function () { diff --git a/types/web-element.d.ts b/types/web-element.d.ts index 05d68b02f2..025e2f2f84 100644 --- a/types/web-element.d.ts +++ b/types/web-element.d.ts @@ -187,6 +187,8 @@ export interface ScopedElement extends Element, PromiseLike { rightClick(): Promise; + isSelected(): ElementValue; + waitUntil(signalOrOptions: WaitUntilActions | WaitUntilOptions, waitOptions?: WaitUntilOptions): Promise; }