From 4efc6e3c4b0bd991a07c4f2054eb85cc5eaad764 Mon Sep 17 00:00:00 2001 From: Ayush Vishwakarma <127919475+Ayush-Vish@users.noreply.github.com> Date: Fri, 19 Apr 2024 18:21:06 +0530 Subject: [PATCH] Add `.attr()` and `.attribute()` as alias for `.getAttribute()` command. (#4172) Co-authored-by: Priyansh Garg --- lib/api/web-element/scoped-element.js | 1 + .../commands/web-element/testGetAttribute.js | 48 +++++++++++++++++++ types/tests/webElement.test-d.ts | 2 + types/web-element.d.ts | 2 + 4 files changed, 53 insertions(+) diff --git a/lib/api/web-element/scoped-element.js b/lib/api/web-element/scoped-element.js index 9eafa3ffaa..341778bec7 100644 --- a/lib/api/web-element/scoped-element.js +++ b/lib/api/web-element/scoped-element.js @@ -23,6 +23,7 @@ class ScopedWebElement { 'findAllByPlaceholderText': ['getAllByPlaceholderText'], 'findAllByAltText': ['getAllByAltText'], 'getRect': ['getSize', 'getLocation', 'rect'], + 'getAttribute': ['attr', 'attribute'], 'getProperty': ['property', 'prop'], 'getText': ['text'], 'getTagName': ['tagName'], diff --git a/test/src/api/commands/web-element/testGetAttribute.js b/test/src/api/commands/web-element/testGetAttribute.js index 720866a23c..7a3ddc6dda 100644 --- a/test/src/api/commands/web-element/testGetAttribute.js +++ b/test/src/api/commands/web-element/testGetAttribute.js @@ -38,6 +38,54 @@ describe('element().getAttribute() command', function () { assert.strictEqual(resultValue, 'text'); }); + it('test .element.attr() alias', async function() { + MockServer.addMock({ + url: '/session/13521-10219-202/execute/sync', + method: 'POST', + response: JSON.stringify({ + value: 'text' + }) + }, true); + + const resultPromise = this.client.api.element('#signupSection').attr('type'); + 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, 'text'); + + const resultValue = await resultPromise.value; + assert.strictEqual(resultValue, 'text'); + }); + + it('test .element().attribute() alias', async function() { + MockServer.addMock({ + url: '/session/13521-10219-202/execute/sync', + method: 'POST', + response: JSON.stringify({ + value: 'text' + }) + }, true); + + const resultPromise = this.client.api.element('#signupSection').attribute('type'); + 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, 'text'); + + const resultValue = await resultPromise.value; + assert.strictEqual(resultValue, 'text'); + }); + it('test .element().find().getAttribute()', async function() { MockServer.addMock({ url: '/session/13521-10219-202/execute/sync', diff --git a/types/tests/webElement.test-d.ts b/types/tests/webElement.test-d.ts index 4cd24eb6fb..e7a3d0ff69 100644 --- a/types/tests/webElement.test-d.ts +++ b/types/tests/webElement.test-d.ts @@ -152,6 +152,8 @@ describe('new element() api', function () { expectType>(elem.prop('property-name')); expectType>(elem.property('property-name')); expectType>(elem.getAttribute('attrib-name')); + expectType>(elem.attr('attrib-name')); + expectType>(elem.attribute('attrib-name')); expectType>(elem.getValue()); expectType>(elem.isEnabled()); expectType>(elem.isVisible()); diff --git a/types/web-element.d.ts b/types/web-element.d.ts index 90ddf1efa3..a5e2c77866 100644 --- a/types/web-element.d.ts +++ b/types/web-element.d.ts @@ -168,6 +168,8 @@ export interface ScopedElement extends Element, PromiseLike { setProperty(name: string, value: unknown): Promise; getAttribute(name: string): ElementValue; + attr(name: string): ElementValue; + attribute(name: string): ElementValue; setAttribute(name: string, value: string | null): Promise;