Skip to content

Commit

Permalink
Add EditorAction Page Object
Browse files Browse the repository at this point in the history
Signed-off-by: Marian Lorinc <mlorinc@redhat.com>
  • Loading branch information
mlorinc committed May 23, 2023
1 parent d5f4f76 commit 3a922f9
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 19 deletions.
8 changes: 8 additions & 0 deletions locators/lib/1.76.0.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { LocatorDiff } from "monaco-page-objects";
export const diff: LocatorDiff = {
locators: {
EditorView: {
attribute: 'aria-label'
}
}
}
16 changes: 16 additions & 0 deletions page-objects/src/components/editor/EditorAction.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
return this.getAttribute(EditorAction.locators.EditorView.attribute);
}
}
43 changes: 26 additions & 17 deletions page-objects/src/components/editor/EditorView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<WebElement[]> {
async getActions(groupIndex: number = 0): Promise<EditorAction[]> {
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<WebElement | undefined> {
async getAction(predicateOrTitle: string | ((action: EditorAction) => boolean | PromiseLike<boolean>), groupIndex: number = 0): Promise<EditorAction | undefined> {
const group = await this.getEditorGroup(groupIndex);
return group.getAction(title);
return group.getAction(predicateOrTitle);
}
}

Expand Down Expand Up @@ -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<WebElement[]> {
return this.findElement(EditorGroup.locators.EditorView.actionContainer).findElements(EditorGroup.locators.EditorView.actionItem);
async getActions(): Promise<EditorAction[]> {
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<WebElement | undefined> {
async getAction(predicateOrTitle: string | ((action: EditorAction) => boolean | PromiseLike<boolean>)): Promise<EditorAction | undefined> {
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;
Expand Down
1 change: 1 addition & 0 deletions page-objects/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
9 changes: 7 additions & 2 deletions test/test-project/src/test/editor/editorView-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 3a922f9

Please sign in to comment.