From 863857872c4f34c4f097f94b05a18eed9b14772d Mon Sep 17 00:00:00 2001 From: Dominik Jelinek Date: Tue, 30 Jul 2024 12:48:10 +0200 Subject: [PATCH] improve dynamic wait Signed-off-by: Dominik Jelinek --- .../src/components/editor/TextEditor.ts | 105 +++++------------- .../src/test/editor/textEditor.test.ts | 31 +----- 2 files changed, 29 insertions(+), 107 deletions(-) diff --git a/packages/page-objects/src/components/editor/TextEditor.ts b/packages/page-objects/src/components/editor/TextEditor.ts index 4b0a8d682..6c2bf3d5b 100644 --- a/packages/page-objects/src/components/editor/TextEditor.ts +++ b/packages/page-objects/src/components/editor/TextEditor.ts @@ -342,11 +342,11 @@ export class TextEditor extends Editor { * @param column column number to set to * @returns Promise resolving when the cursor has reached the given coordinates */ - async setCursor(line: number, column: number): Promise { + async setCursor(line: number, column: number, timeout: number = 2_500): Promise { const input = await new Workbench().openCommandPrompt(); await input.setText(`:${line},${column}`); await input.confirm(); - await this.waitForCursorPositionAt(line, column); + await this.waitForCursorPositionAt(line, column, timeout); } /** @@ -366,7 +366,7 @@ export class TextEditor extends Editor { * @param column column number to move to * @returns Promise resolving when the cursor has reached the given coordinates */ - async moveCursor(line: number, column: number): Promise { + async moveCursor(line: number, column: number, timeout: number = 10_000): Promise { if (line < 1 || line > (await this.getNumberOfLines())) { throw new Error(`Line number ${line} does not exist`); } @@ -374,8 +374,22 @@ export class TextEditor extends Editor { throw new Error(`Column number ${column} does not exist`); } const inputarea = await this.findElement(TextEditor.locators.Editor.inputArea); - await this.moveCursorToLine(inputarea, line); - await this.moveCursorToColumn(inputarea, column); + await inputarea.getDriver().wait( + async () => { + await this.moveCursorToLine(inputarea, line); + return await this.isLine(line); + }, + timeout, + `Unable to move cursor to line: ${line}`, + ); + await inputarea.getDriver().wait( + async () => { + await this.moveCursorToColumn(inputarea, column); + return await this.isColumn(column); + }, + timeout, + `Unable to move cursor to column: ${column}`, + ); } /** @@ -388,7 +402,6 @@ export class TextEditor extends Editor { const coordinates = await this.getCoordinates(); const lineGap = coordinates[0] - line; const lineKey = lineGap >= 0 ? Key.UP : Key.DOWN; - // let nextLine = coordinates[0]; for (let i = 0; i < Math.abs(lineGap); i++) { if (await this.isLine(line)) { @@ -397,38 +410,6 @@ export class TextEditor extends Editor { await inputArea.getDriver().actions().clear(); await inputArea.sendKeys(lineKey); await inputArea.getDriver().sleep(500); - // if (await this.isLine(line)) { - // break; - // } - - // switch (lineKey) { - // case Key.UP: - // nextLine = nextLine - 1; - // break; - // case Key.DOWN: - // nextLine = nextLine + 1; - // break; - // } - - // try { - // await this.waitForCursorPositionAtLine(nextLine); - // } catch (error) { - // if (await this.isLine(line)) { - // break; - // } - // await inputArea.getDriver().actions().clear(); - // await inputArea.sendKeys(lineKey); - // await inputArea.getDriver().sleep(500); - // if (await this.isLine(line)) { - // break; - // } - - // if (lineKey === Key.DOWN && nextLine < line) { - // await this.waitForCursorPositionAtLine(nextLine); - // } else { - // await this.waitForCursorPositionAtLine(line); - // } - // } } } @@ -442,7 +423,6 @@ export class TextEditor extends Editor { const coordinates = await this.getCoordinates(); const columnGap = coordinates[1] - column; const columnKey = columnGap >= 0 ? Key.LEFT : Key.RIGHT; - // let nextCol = coordinates[1]; for (let i = 0; i < Math.abs(columnGap); i++) { if (await this.isColumn(column)) { @@ -451,44 +431,9 @@ export class TextEditor extends Editor { await inputArea.getDriver().actions().clear(); await inputArea.sendKeys(columnKey); await inputArea.getDriver().sleep(500); - // if (await this.isColumn(column)) { - // break; - // } if ((await this.getCoordinates())[0] !== coordinates[0]) { throw new Error(`Column number ${column} is not accessible on line ${coordinates[0]}`); } - - // switch (columnKey) { - // case Key.LEFT: - // nextCol = nextCol - 1; - // break; - // case Key.RIGHT: - // nextCol = nextCol + 1; - // break; - // } - - // try { - // await this.waitForCursorPositionAtColumn(nextCol); - // } catch (error) { - // if (await this.isColumn(column)) { - // break; - // } - // await inputArea.getDriver().actions().clear(); - // await inputArea.sendKeys(columnKey); - // await inputArea.getDriver().sleep(500); - // if (await this.isColumn(column)) { - // break; - // } - // if ((await this.getCoordinates())[0] !== coordinates[0]) { - // throw new Error(`Column number ${column} is not accessible on line ${coordinates[0]}`); - // } - - // if (columnKey === Key.RIGHT && nextCol < column) { - // await this.waitForCursorPositionAtColumn(nextCol); - // } else { - // await this.waitForCursorPositionAtColumn(column); - // } - // } } } @@ -524,16 +469,16 @@ export class TextEditor extends Editor { * @param column column number to wait * @param timeout default timeout */ - private async waitForCursorPositionAt(line: number, column: number): Promise { - (await this.waitForCursorPositionAtLine(line)) && (await this.waitForCursorPositionAtColumn(column)); + private async waitForCursorPositionAt(line: number, column: number, timeout: number = 2_500): Promise { + (await this.waitForCursorPositionAtLine(line, timeout)) && (await this.waitForCursorPositionAtColumn(column, timeout)); } /** * (private) Dynamic waiting for cursor position movements at line * @param line line number to wait - * @param timeout default timeout is wait for 1.5s + * @param timeout */ - private async waitForCursorPositionAtLine(line: number, timeout: number = 2_500): Promise { + private async waitForCursorPositionAtLine(line: number, timeout: number): Promise { return await this.getDriver().wait( async () => { return await this.isLine(line); @@ -547,9 +492,9 @@ export class TextEditor extends Editor { /** * (private) Dynamic waiting for cursor position movements at column * @param column column number to wait - * @param timeout default timeout is wait for 1.5s + * @param timeout */ - private async waitForCursorPositionAtColumn(column: number, timeout: number = 2_500): Promise { + private async waitForCursorPositionAtColumn(column: number, timeout: number): Promise { return await this.getDriver().wait( async () => { return await this.isColumn(column); diff --git a/tests/test-project/src/test/editor/textEditor.test.ts b/tests/test-project/src/test/editor/textEditor.test.ts index 4b401adb8..84f6082a9 100644 --- a/tests/test-project/src/test/editor/textEditor.test.ts +++ b/tests/test-project/src/test/editor/textEditor.test.ts @@ -203,28 +203,14 @@ describe('TextEditor', function () { editor = (await ew.openEditor(param.file)) as TextEditor; }); - // afterEach(async function () { - // await editor.setCursor(1, 1); - // await ew.closeEditor(param.file); - // }); - [ [2, 5], [3, 9], ].forEach((coor) => it(`move cursor to position [Ln ${coor[0]}, Col ${coor[1]}]`, async function () { this.timeout(30000); - await editor.getDriver().wait( - async () => { - await editor.moveCursor(coor[0], coor[1]); - const current = await editor.getCoordinates(); - return current[0] === coor[0] && current[1] === coor[1]; - }, - 10_000, - `Unable to move cursor at position: ${coor}`, - ); - // await editor.moveCursor(coor[0], coor[1]); - // expect(await editor.getCoordinates()).to.deep.equal(coor); + await editor.moveCursor(coor[0], coor[1]); + expect(await editor.getCoordinates()).to.deep.equal(coor); }), ); @@ -235,17 +221,8 @@ describe('TextEditor', function () { ].forEach((coor) => (param.indent === 'tabs' ? it.skip : it)(`set cursor to position [Ln ${coor[0]}, Col ${coor[1]}]`, async function () { this.timeout(30000); - await editor.getDriver().wait( - async () => { - await editor.setCursor(coor[0], coor[1]); - const current = await editor.getCoordinates(); - return current[0] === coor[0] && current[1] === coor[1]; - }, - 10_000, - `Unable to set cursor at position: ${coor}`, - ); - // await editor.setCursor(coor[0], coor[1]); - // expect(await editor.getCoordinates()).to.deep.equal(coor); + await editor.setCursor(coor[0], coor[1]); + expect(await editor.getCoordinates()).to.deep.equal(coor); }), ); }),