Skip to content

Commit

Permalink
Add .attr() and .attribute() as alias for .getAttribute() comma…
Browse files Browse the repository at this point in the history
…nd. (#4172)

Co-authored-by: Priyansh Garg <[email protected]>
  • Loading branch information
Ayush-Vish and garg3133 authored Apr 19, 2024
1 parent fb78d12 commit 4efc6e3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/api/web-element/scoped-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ScopedWebElement {
'findAllByPlaceholderText': ['getAllByPlaceholderText'],
'findAllByAltText': ['getAllByAltText'],
'getRect': ['getSize', 'getLocation', 'rect'],
'getAttribute': ['attr', 'attribute'],
'getProperty': ['property', 'prop'],
'getText': ['text'],
'getTagName': ['tagName'],
Expand Down
48 changes: 48 additions & 0 deletions test/src/api/commands/web-element/testGetAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 2 additions & 0 deletions types/tests/webElement.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ describe('new element() api', function () {
expectType<ElementValue<string | null>>(elem.prop('property-name'));
expectType<ElementValue<string | null>>(elem.property('property-name'));
expectType<ElementValue<string | null>>(elem.getAttribute('attrib-name'));
expectType<ElementValue<string | null>>(elem.attr('attrib-name'));
expectType<ElementValue<string | null>>(elem.attribute('attrib-name'));
expectType<ElementValue<string | null>>(elem.getValue());
expectType<ElementValue<boolean>>(elem.isEnabled());
expectType<ElementValue<boolean>>(elem.isVisible());
Expand Down
2 changes: 2 additions & 0 deletions types/web-element.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ export interface ScopedElement extends Element, PromiseLike<WebElement> {
setProperty(name: string, value: unknown): Promise<WebElement>;

getAttribute(name: string): ElementValue<string | null>;
attr(name: string): ElementValue<string | null>;
attribute(name: string): ElementValue<string | null>;

setAttribute(name: string, value: string | null): Promise<WebElement>;

Expand Down

0 comments on commit 4efc6e3

Please sign in to comment.