Skip to content

Commit

Permalink
improve dynamic wait
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Jelinek <[email protected]>
  • Loading branch information
djelinek committed Jul 30, 2024
1 parent bcc8d4e commit 8f63718
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 94 deletions.
91 changes: 19 additions & 72 deletions packages/page-objects/src/components/editor/TextEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,32 @@ 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<void> {
async moveCursor(line: number, column: number, timeout: number = 10_000): Promise<void> {
if (line < 1 || line > (await this.getNumberOfLines())) {
throw new Error(`Line number ${line} does not exist`);
}
if (column < 1) {
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}`,
);
// await this.moveCursorToLine(inputarea, line);
// await this.moveCursorToColumn(inputarea, column);
}

/**
Expand All @@ -388,7 +404,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)) {
Expand All @@ -397,38 +412,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);
// }
// }
}
}

Expand All @@ -442,7 +425,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)) {
Expand All @@ -451,44 +433,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);
// }
// }
}
}

Expand Down
44 changes: 22 additions & 22 deletions tests/test-project/src/test/editor/textEditor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,17 @@ describe('TextEditor', function () {
].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.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);
}),
);

Expand All @@ -235,17 +235,17 @@ 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.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);
}),
);
}),
Expand Down

0 comments on commit 8f63718

Please sign in to comment.