Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat): Added aliases for find() and findAll() command in new Element API #4130

Merged
merged 6 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/api/_loaders/element-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ScopedElementAPILoader {
static get availableElementCommands() {
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 @@ -347,8 +349,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
Loading