From 3a922f9a48602908fa535bb770eef46190283b36 Mon Sep 17 00:00:00 2001 From: Marian Lorinc Date: Tue, 23 May 2023 17:58:54 +0200 Subject: [PATCH] Add EditorAction Page Object Signed-off-by: Marian Lorinc --- locators/lib/1.76.0.ts | 8 ++++ .../src/components/editor/EditorAction.ts | 16 +++++++ .../src/components/editor/EditorView.ts | 43 +++++++++++-------- page-objects/src/index.ts | 1 + .../src/test/editor/editorView-test.ts | 9 +++- 5 files changed, 58 insertions(+), 19 deletions(-) create mode 100644 locators/lib/1.76.0.ts create mode 100644 page-objects/src/components/editor/EditorAction.ts diff --git a/locators/lib/1.76.0.ts b/locators/lib/1.76.0.ts new file mode 100644 index 000000000..33d021f58 --- /dev/null +++ b/locators/lib/1.76.0.ts @@ -0,0 +1,8 @@ +import { LocatorDiff } from "monaco-page-objects"; +export const diff: LocatorDiff = { + locators: { + EditorView: { + attribute: 'aria-label' + } + } +} diff --git a/page-objects/src/components/editor/EditorAction.ts b/page-objects/src/components/editor/EditorAction.ts new file mode 100644 index 000000000..0622bc6c1 --- /dev/null +++ b/page-objects/src/components/editor/EditorAction.ts @@ -0,0 +1,16 @@ +import { EditorGroup } from "./EditorView"; +import { AbstractElement } from "../AbstractElement"; +import { WebElement } from "../.."; + +export class EditorAction extends AbstractElement { + constructor(element: WebElement, parent: EditorGroup) { + super(element, parent); + } + + /** + * Get text description of the action. + */ + async getTitle(): Promise { + return this.getAttribute(EditorAction.locators.EditorView.attribute); + } +} diff --git a/page-objects/src/components/editor/EditorView.ts b/page-objects/src/components/editor/EditorView.ts index 793d03ee3..b5d899134 100644 --- a/page-objects/src/components/editor/EditorView.ts +++ b/page-objects/src/components/editor/EditorView.ts @@ -6,6 +6,7 @@ import { SettingsEditor } from "./SettingsEditor"; import { WebView } from "./WebView"; import { DiffEditor } from './DiffEditor'; import { ElementWithContexMenu } from "../ElementWithContextMenu"; +import { EditorAction } from "./EditorAction"; export class EditorTabNotFound extends Error { constructor(title: string, group: number) { @@ -156,21 +157,22 @@ export class EditorView extends AbstractElement { /** * Get editor actions of a select editor group * @param groupIndex zero based index of the editor group (leftmost group has index 0), default 0 - * @returns promise resolving to list of WebElement objects + * @returns promise resolving to list of EditorAction objects */ - async getActions(groupIndex = 0): Promise { + async getActions(groupIndex: number = 0): Promise { const group = await this.getEditorGroup(groupIndex); return group.getActions(); } /** - * Get editor action of a select editor group, search by title + * Get editor action of a select editor group, search by title or predicate + * @param predicateOrTitle title or predicate to be used in search process * @param groupIndex zero based index of the editor group (leftmost group has index 0), default 0 - * @returns promise resolving to WebElement object if found, undefined otherwise + * @returns promise resolving to EditorAction object if found, undefined otherwise */ - async getAction(title: string, groupIndex = 0): Promise { + async getAction(predicateOrTitle: string | ((action: EditorAction) => boolean | PromiseLike), groupIndex: number = 0): Promise { const group = await this.getEditorGroup(groupIndex); - return group.getAction(title); + return group.getAction(predicateOrTitle); } } @@ -312,23 +314,30 @@ export class EditorGroup extends AbstractElement { } /** - * Retrieve the editor action buttons as WebElements - * @returns promise resolving to list of WebElement objects + * Retrieve the editor action buttons as EditorActions + * @returns promise resolving to list of EditorAction objects */ - async getActions(): Promise { - return this.findElement(EditorGroup.locators.EditorView.actionContainer).findElements(EditorGroup.locators.EditorView.actionItem); + async getActions(): Promise { + const actions = await + this.findElement(EditorGroup.locators.EditorView.actionContainer) + .findElements(EditorGroup.locators.EditorView.actionItem); + return actions.map((action) => new EditorAction(action, this)); } /** - * Find an editor action button by title - * @param title title of the button - * @returns promise resolving to WebElement representing the button if found, undefined otherwise + * Find an editor action button by predicate or title + * @param predicateOrTitle predicate/title to be used + * @returns promise resolving to EditorAction representing the button if found, undefined otherwise */ - async getAction(title: string): Promise { + async getAction(predicateOrTitle: string | ((action: EditorAction) => boolean | PromiseLike)): Promise { + const predicate = (typeof predicateOrTitle === 'string') ? + (async (action: EditorAction) => await action.getTitle() === predicateOrTitle) : (predicateOrTitle); + const actions = await this.getActions(); - for (const item of actions) { - if (await item.getAttribute(EditorGroup.locators.EditorView.attribute) === title) { - return item; + + for (const action of actions) { + if (await predicate(action)) { + return action; } } return undefined; diff --git a/page-objects/src/index.ts b/page-objects/src/index.ts index 3458b813d..8d47a1c51 100644 --- a/page-objects/src/index.ts +++ b/page-objects/src/index.ts @@ -40,6 +40,7 @@ export * from './components/bottomBar/Views'; export * from './components/statusBar/StatusBar'; export * from './components/editor/EditorView'; +export * from './components/editor/EditorAction'; export * from './components/editor/Breakpoint'; export * from './components/editor/TextEditor'; export * from './components/editor/Editor'; diff --git a/test/test-project/src/test/editor/editorView-test.ts b/test/test-project/src/test/editor/editorView-test.ts index eb3e01ccf..1e7aae429 100644 --- a/test/test-project/src/test/editor/editorView-test.ts +++ b/test/test-project/src/test/editor/editorView-test.ts @@ -88,9 +88,14 @@ describe('EditorView', function () { expect(actions).not.empty; }); - it('getAction works', async function () { + it('getAction(title: string) works', async function () { const action = await view.getAction('More Actions...'); - expect(action).not.undefined; + expect(await action.getTitle()).equal('More Actions...'); + }); + + it('getAction(predicate: PredicateFunction) works', async function () { + const action = await view.getAction(async (action) => await action.getTitle() === 'More Actions...'); + expect(await action.getTitle()).equal('More Actions...'); }); it('Editor getAction works', async function () {