Skip to content

Commit

Permalink
Add aliases for find() and findAll() command in new Element API. (n…
Browse files Browse the repository at this point in the history
…ightwatchjs#4130)



Co-authored-by: Priyansh Garg <[email protected]>
  • Loading branch information
2 people authored and dikwickley committed Oct 15, 2024
1 parent 0511f85 commit 66145ce
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/api/_loaders/element-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ScopedElementAPILoader {
// commands directly available on `browser.element`
return [
['findElement', 'find', 'get'],
['findAll'],
['findAll', 'findElements', 'getAll'],
['findByText'],
['findAllByText'],
['findByRole'],
Expand Down
2 changes: 2 additions & 0 deletions lib/api/web-element/commands/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
* @syntax browser.element.find(selector)
* @param {String|Object} selector
* @returns {ScopedWebElement}
* @alias get
* @alias findElement
*/
module.exports.command = function(selector) {
return this.createScopedElement(selector, this);
Expand Down
2 changes: 2 additions & 0 deletions lib/api/web-element/commands/findAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* @syntax browser.element.findAll(syntax)
* @param {Selector} selector
* @returns {Array.<ScopeWebElement>}
* @alias getAll
* @alias findElements
*/
module.exports.command = function(selector) {
return this.createScopedElements(selector, {parentElement: this, commandName: 'findAll'});
Expand Down
20 changes: 20 additions & 0 deletions test/src/api/commands/web-element/testFind.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,24 @@ describe('element().find() commands', function () {
assert.strictEqual(signupWebElement instanceof WebElement, true);
assert.strictEqual(await signupWebElement.getId(), '0');
});

it('test .element.get()', async function() {
const signupElement = this.client.api.element.get('#signupSection');
assert.strictEqual(signupElement instanceof Element, true);
assert.strictEqual(await signupElement.getId(), '0');

const signupWebElement = await signupElement;
assert.strictEqual(signupWebElement instanceof WebElement, true);
assert.strictEqual(await signupWebElement.getId(), '0');
});

it('test .element.findElement()', async function() {
const signupElement = this.client.api.element.findElement('#signupSection');
assert.strictEqual(signupElement instanceof Element, true);
assert.strictEqual(await signupElement.getId(), '0');

const signupWebElement = await signupElement;
assert.strictEqual(signupWebElement instanceof WebElement, true);
assert.strictEqual(await signupWebElement.getId(), '0');
});
});
34 changes: 34 additions & 0 deletions test/src/api/commands/web-element/testFindAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,40 @@ describe('element().findAll() commands', function () {
assert.strictEqual(await btnWEbElements[0].getId(), '5cc459b8-36a8-3042-8b4a-258883ea642b');
});

it('test .element.getAll()', async function() {
const webLoginElements = this.client.api.element.getAll('#weblogin');
assert.strictEqual(webLoginElements instanceof Element, false);
assert.strictEqual(typeof webLoginElements.find, 'undefined');
assert.strictEqual(typeof webLoginElements.click, 'undefined');

assert.strictEqual(webLoginElements instanceof Promise, false);
assert.strictEqual(typeof webLoginElements.then, 'function');
assert.strictEqual(typeof webLoginElements.nth, 'function');
assert.strictEqual(typeof webLoginElements.count, 'function');

const btnWEbElements = await webLoginElements;
assert.strictEqual(btnWEbElements.length, 2);
assert.strictEqual(btnWEbElements[0] instanceof WebElement, true);
assert.strictEqual(await btnWEbElements[0].getId(), '5cc459b8-36a8-3042-8b4a-258883ea642b');
});

it('test .element.findElements()', async function() {
const webLoginElements = this.client.api.element.findElements('#weblogin');
assert.strictEqual(webLoginElements instanceof Element, false);
assert.strictEqual(typeof webLoginElements.find, 'undefined');
assert.strictEqual(typeof webLoginElements.click, 'undefined');

assert.strictEqual(webLoginElements instanceof Promise, false);
assert.strictEqual(typeof webLoginElements.then, 'function');
assert.strictEqual(typeof webLoginElements.nth, 'function');
assert.strictEqual(typeof webLoginElements.count, 'function');

const btnWEbElements = await webLoginElements;
assert.strictEqual(btnWEbElements.length, 2);
assert.strictEqual(btnWEbElements[0] instanceof WebElement, true);
assert.strictEqual(await btnWEbElements[0].getId(), '5cc459b8-36a8-3042-8b4a-258883ea642b');
});

it('test .element().findAll().count()', async function() {
const resultPromise = this.client.api.element('#signupSection').findAll('.btn').count();
assert.strictEqual(resultPromise instanceof Element, false);
Expand Down
9 changes: 9 additions & 0 deletions types/tests/webElement.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ describe('new element() api', function () {
expectType<ScopedElement>(elementFind);
expectType<WebElement>(await elementFind);

expectType<ScopedElement>(browser.element.get('selector'));
expectType<ScopedElement>(browser.element.findElement('selector'));

const elementFindByText = browser.element.findByText('some-text', {exact: true, abortOnFailure: false});
expectType<ScopedElement>(elementFindByText);

Expand All @@ -65,9 +68,13 @@ describe('new element() api', function () {
const elementFindAll = browser.element.findAll('selector');
expectType<Elements>(elementFindAll);
expectType<WebElement[]>(await elementFindAll);

expectType<ScopedElement>(elementFindAll.nth(2));
expectType<number>(await elementFindAll.count());

expectType<Elements>(browser.element.getAll('selector'));
expectType<Elements>(browser.element.findElements('selector'));

const elementFindAllByText = browser.element.findAllByText('some-text', {exact: true, abortOnFailure: false});
expectType<Elements>(elementFindAllByText);

Expand Down Expand Up @@ -102,6 +109,7 @@ describe('new element() api', function () {

expectType<ScopedElement>(elem.find('selector'));
expectType<ScopedElement>(elem.get('selector'));
expectType<ScopedElement>(elem.findElement('selector'));

expectType<ScopedElement>(elem.findByText('some-text', {exact: true, abortOnFailure: false}));

Expand All @@ -114,6 +122,7 @@ describe('new element() api', function () {
expectType<ScopedElement>(elem.findByAltText('some-text', {exact: true, abortOnFailure: false}));

expectType<Elements>(elem.findAll('selector'));
expectType<Elements>(elem.findElements('selector'));
expectType<Elements>(elem.getAll('selector'));

expectType<Elements>(elem.findAllByText('some-text', {exact: true, abortOnFailure: false}));
Expand Down
14 changes: 8 additions & 6 deletions types/web-element.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface ScopedElement extends Element, PromiseLike<WebElement> {

find(selector: ScopedElementSelector): ScopedElement;
get(selector: ScopedElementSelector): ScopedElement;
findElement(selector: ScopedElementSelector): ScopedElement;

findByText(
text: string,
Expand Down Expand Up @@ -45,30 +46,31 @@ export interface ScopedElement extends Element, PromiseLike<WebElement> {
readonly expanded?: boolean;
}
): ScopedElement;

findByPlaceholderText(
text: string,
options?: Omit<ScopedSelectorObject, 'selector'> & {
readonly exact?: boolean;
}
): ScopedElement;

findByLabelText(
text: string,
options?: Omit<ScopedSelectorObject, 'selector'> & {
readonly exact?: boolean;
}
): ScopedElement;

findByAltText(
text: string,
options?: Omit<ScopedSelectorObject, 'selector'> & {
readonly exact?: boolean;
}
): ScopedElement;

findAll(selector: ScopedSelector | Promise<ScopedSelector>): Elements;
getAll(selector: ScopedSelector | Promise<ScopedSelector>): Elements;
findElements(selector: ScopedSelector | Promise<ScopedSelector>): Elements;

findAllByText(
text: string,
Expand Down Expand Up @@ -351,8 +353,8 @@ export type DragAndDropDestination = {
export interface ElementFunction
extends Pick<
ScopedElement,
'find' | 'findByText' | 'findByRole' | 'findByPlaceholderText' | 'findByLabelText' | 'findByAltText' |
'findAll' | 'findAllByText' | 'findAllByRole' | 'findAllByPlaceholderText' | 'findAllByAltText'
'find' | 'get' | 'findElement' | 'findByText' | 'findByRole' | 'findByPlaceholderText' | 'findByLabelText' | 'findByAltText' |
'findAll' | 'getAll' | 'findElements' | 'findAllByText' | 'findAllByRole' | 'findAllByPlaceholderText' | 'findAllByAltText'
> {
(selector: ScopedElementSelector): ScopedElement;
(
Expand Down

0 comments on commit 66145ce

Please sign in to comment.