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 8638578
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 107 deletions.
105 changes: 25 additions & 80 deletions packages/page-objects/src/components/editor/TextEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
async setCursor(line: number, column: number, timeout: number = 2_500): Promise<void> {
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);
}

/**
Expand All @@ -366,16 +366,30 @@ 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}`,
);
}

/**
Expand All @@ -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)) {
Expand All @@ -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);
// }
// }
}
}

Expand All @@ -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)) {
Expand All @@ -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);
// }
// }
}
}

Expand Down Expand Up @@ -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<void> {
(await this.waitForCursorPositionAtLine(line)) && (await this.waitForCursorPositionAtColumn(column));
private async waitForCursorPositionAt(line: number, column: number, timeout: number = 2_500): Promise<void> {
(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<boolean> {
private async waitForCursorPositionAtLine(line: number, timeout: number): Promise<boolean> {
return await this.getDriver().wait(
async () => {
return await this.isLine(line);
Expand All @@ -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<boolean> {
private async waitForCursorPositionAtColumn(column: number, timeout: number): Promise<boolean> {
return await this.getDriver().wait(
async () => {
return await this.isColumn(column);
Expand Down
31 changes: 4 additions & 27 deletions tests/test-project/src/test/editor/textEditor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}),
);

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

0 comments on commit 8638578

Please sign in to comment.