Skip to content

Commit e180a8d

Browse files
committed
Move editor utility methods out of EditorPage into editorUtils.ts
EditorPage now only models the page object (openFile, getEditor). The utility methods clear, moveCursorToEnd, replaceTextWithinLineContaining, and hoverOver have been moved to src/test/utils/editorUtils.ts as standalone exported functions, per code review feedback.
1 parent 2b9cec9 commit e180a8d

6 files changed

Lines changed: 77 additions & 67 deletions

File tree

src/test/GradleTestLSPHover.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
* Copyright IBM Corp. 2026
44
*/
55
import { expect } from 'chai';
6-
import { EditorView, TextEditor, VSBrowser, Workbench, WebDriver } from 'vscode-extension-tester';
6+
import { EditorView, VSBrowser } from 'vscode-extension-tester';
77
import * as utils from './utils/testUtils';
88
import { logger } from './utils/testLogger';
99
import * as path from 'path';
1010
import { EditorPage } from './pages/EditorPage';
11+
import * as editorUtils from './utils/editorUtils';
1112

1213
describe('LSP Hover tests for Gradle Project', () => {
1314
let serverXml: EditorPage;
@@ -92,7 +93,7 @@ describe('LSP Hover tests for Gradle Project', () => {
9293
logger.testStart(`Hover over ${testCase.element} shows Liberty Language Server documentation`);
9394

9495
try {
95-
const hoverText = await serverXml.hoverOver(testCase.line, testCase.column, testCase.element);
96+
const hoverText = await editorUtils.hoverOver(serverXml.getEditor(), testCase.line, testCase.column, testCase.element);
9697
expect(hoverText).to.not.be.empty;
9798
logger.stepSuccess(3, `Hover widget displayed with Liberty Language Server content for ${testCase.element}`);
9899

@@ -164,7 +165,7 @@ describe('LSP Hover tests for Gradle Project', () => {
164165
logger.testStart(`Hover over ${testCase.element} shows LSP4Jakarta documentation`);
165166

166167
try {
167-
const hoverText = await javaFile.hoverOver(testCase.line, testCase.column, testCase.element);
168+
const hoverText = await editorUtils.hoverOver(javaFile.getEditor(), testCase.line, testCase.column, testCase.element);
168169

169170
expect(hoverText).to.not.be.empty;
170171
logger.stepSuccess(3, `Hover widget displayed with LSP4Jakarta content for ${testCase.element}`);

src/test/GradleTestLSPRestSnippetAndDiagnostic.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
* Copyright IBM Corp. 2026
55
*/
66
import { expect } from 'chai';
7-
import { EditorView, TextEditor, VSBrowser, Workbench, WebDriver, BottomBarPanel, MarkerType, Key, By } from 'vscode-extension-tester';
7+
import { EditorView, VSBrowser, WebDriver } from 'vscode-extension-tester';
88
import * as utils from './utils/testUtils';
99
import { logger } from './utils/testLogger';
1010
import * as path from 'path';
1111
import { ProblemsPage } from './pages/ProblemsPage';
1212
import { EditorPage } from './pages/EditorPage';
1313
import { CodeAssistPage } from './pages/CodeAssistPage';
1414
import { QuickFixPage } from './pages/QuickFixPage';
15+
import * as editorUtils from './utils/editorUtils';
1516

1617
describe('Rest Class Snippet Test for Gradle Project', () => {
1718
let editorPage: EditorPage;
@@ -61,7 +62,7 @@ describe('Rest Class Snippet Test for Gradle Project', () => {
6162
// selectText may fail but setText still works
6263
}
6364

64-
await editorPage.clear();
65+
await editorUtils.clearEditor(editorPage.getEditor());
6566
logger.info('Reset TestRest.java to empty after test');
6667
}
6768
} catch (error) {
@@ -101,15 +102,15 @@ describe('Rest Class Snippet Test for Gradle Project', () => {
101102
logger.testStart('rest_class snippet inserts correct REST class');
102103

103104
// at the top of the rest_class snippet test, before positioning the cursor
104-
await editorPage.clear();
105+
await editorUtils.clearEditor(editorPage.getEditor());
105106
await wait.sleep(1500); // let any auto-stub settle, then confirm
106107
const check = await editorPage.getEditor().getText();
107108
logger.info('Buffer before snippet: ' + JSON.stringify(check));
108109

109110
try {
110111
logger.step(1, 'Positioning cursor for snippet insertion');
111112
// Position cursor at end of file
112-
await editorPage.moveCursorToEnd();
113+
await editorUtils.moveCursorToEnd(editorPage.getEditor());
113114

114115
// type rest and insert snippet
115116
logger.step(2, 'Opening content assist');
@@ -139,7 +140,7 @@ describe('Rest Class Snippet Test for Gradle Project', () => {
139140
logger.testStart('Diagnostic for a private @GET method and quick fix clears it');
140141
try {
141142
// Find the method with @GET and change it to private
142-
await editorPage.replaceTextWithinLineContaining('methodname', 'public' , 'private');
143+
await editorUtils.replaceTextWithinLineContaining(editorPage.getEditor(), 'methodname', 'public', 'private');
143144

144145
// Save file and wait for reanalysis
145146
await editorPage.getEditor().save();

src/test/MavenTestLSPHover.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
* Copyright IBM Corp. 2026
44
*/
55
import { expect } from 'chai';
6-
import { EditorView, TextEditor, VSBrowser, Workbench, WebDriver } from 'vscode-extension-tester';
6+
import { EditorView, VSBrowser } from 'vscode-extension-tester';
77
import * as utils from './utils/testUtils';
88
import { logger } from './utils/testLogger';
99
import * as path from 'path';
1010
import { EditorPage } from './pages/EditorPage';
11+
import * as editorUtils from './utils/editorUtils';
1112

1213
describe('LSP Hover tests for Maven Project', () => {
1314
let serverXml: EditorPage;
@@ -91,7 +92,7 @@ describe('LSP Hover tests for Maven Project', () => {
9192
logger.testStart(`Hover over ${testCase.element} shows Liberty Language Server documentation`);
9293

9394
try {
94-
const hoverText = await serverXml.hoverOver(testCase.line, testCase.column, testCase.element);
95+
const hoverText = await editorUtils.hoverOver(serverXml.getEditor(), testCase.line, testCase.column, testCase.element);
9596
expect(hoverText).to.not.be.empty;
9697
logger.stepSuccess(3, `Hover widget displayed with Liberty Language Server content for ${testCase.element}`);
9798

@@ -163,7 +164,7 @@ describe('LSP Hover tests for Maven Project', () => {
163164
logger.testStart(`Hover over ${testCase.element} shows LSP4Jakarta documentation`);
164165

165166
try {
166-
const hoverText = await javaFile.hoverOver(testCase.line, testCase.column, testCase.element);
167+
const hoverText = await editorUtils.hoverOver(javaFile.getEditor(), testCase.line, testCase.column, testCase.element);
167168

168169
expect(hoverText).to.not.be.empty;
169170
logger.stepSuccess(3, `Hover widget displayed with LSP4Jakarta content for ${testCase.element}`);

src/test/MavenTestLSPRestSnippetAndDiagnostic.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
* Copyright IBM Corp. 2026
55
*/
66
import { expect } from 'chai';
7-
import { EditorView, TextEditor, VSBrowser, Workbench, WebDriver, BottomBarPanel, MarkerType, Key, By } from 'vscode-extension-tester';
7+
import { EditorView, VSBrowser, WebDriver } from 'vscode-extension-tester';
88
import * as utils from './utils/testUtils';
99
import { logger } from './utils/testLogger';
1010
import * as path from 'path';
1111
import { ProblemsPage } from './pages/ProblemsPage';
1212
import { EditorPage } from './pages/EditorPage';
1313
import { CodeAssistPage } from './pages/CodeAssistPage';
1414
import { QuickFixPage } from './pages/QuickFixPage';
15+
import * as editorUtils from './utils/editorUtils';
1516

1617
describe('Rest Class Snippet Test for Maven Project', () => {
1718
let editorPage: EditorPage;
@@ -61,7 +62,7 @@ describe('Rest Class Snippet Test for Maven Project', () => {
6162
// selectText may fail but setText still works
6263
}
6364

64-
await editorPage.clear();
65+
await editorUtils.clearEditor(editorPage.getEditor());
6566
logger.info('Reset TestRest.java to empty after test');
6667
}
6768
} catch (error) {
@@ -101,15 +102,15 @@ describe('Rest Class Snippet Test for Maven Project', () => {
101102
logger.testStart('rest_class snippet inserts correct REST class');
102103

103104
// at the top of the rest_class snippet test, before positioning the cursor
104-
await editorPage.clear();
105+
await editorUtils.clearEditor(editorPage.getEditor());
105106
await wait.sleep(1500); // let any auto-stub settle, then confirm
106107
const check = await editorPage.getEditor().getText();
107108
logger.info('Buffer before snippet: ' + JSON.stringify(check));
108109

109110
try {
110111
logger.step(1, 'Positioning cursor for snippet insertion');
111112
// Position cursor at end of file
112-
await editorPage.moveCursorToEnd();
113+
await editorUtils.moveCursorToEnd(editorPage.getEditor());
113114

114115
// Type rest and trigger snippet insertion
115116
logger.step(2, 'Opening content assist');
@@ -139,7 +140,7 @@ describe('Rest Class Snippet Test for Maven Project', () => {
139140
logger.testStart('Diagnostic for a private @GET method and quick fix clears it');
140141
try {
141142
// Find the method with @GET and change it to private
142-
await editorPage.replaceTextWithinLineContaining('methodname', 'public' , 'private');
143+
await editorUtils.replaceTextWithinLineContaining(editorPage.getEditor(), 'methodname', 'public', 'private');
143144

144145
// Save file and wait for reanalysis
145146
await editorPage.getEditor().save();

src/test/pages/EditorPage.ts

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EditorView, TextEditor, VSBrowser, Workbench } from 'vscode-extension-tester';
1+
import { EditorView, TextEditor, VSBrowser } from 'vscode-extension-tester';
22
import * as utils from '../utils/testUtils';
33

44
export class EditorPage {
@@ -11,8 +11,8 @@ export class EditorPage {
1111
* @param tabTitle The editor tab title (usually the file name).
1212
* @param loadDelayMs Time to let the file/language server settle.
1313
*/
14-
async openFile(filePath: string, tabTitle: string, loadDelay = 3000) : Promise<this> {
15-
await VSBrowser.instance.openResources(filePath, async () => {
14+
async openFile(filePath: string, tabTitle: string, loadDelay = 3000): Promise<this> {
15+
await VSBrowser.instance.openResources(filePath, async () => {
1616
await utils.getWaitHelper().sleep(loadDelay);
1717
});
1818
this.editor = await this.editorView.openEditor(tabTitle) as TextEditor;
@@ -22,51 +22,4 @@ export class EditorPage {
2222
getEditor(): TextEditor {
2323
return this.editor;
2424
}
25-
26-
/** Reset the editor to an empty, saved buffer. */
27-
async clear(): Promise<void> {
28-
await this.editor.setText('');
29-
await this.editor.save();
30-
}
31-
32-
/**
33-
* Place the cursor at the start of the last line of content.
34-
* Note: split('\n').length counts a trailing empty segment when the file
35-
* ends in a newline, so `lastLine - 1` lands on the last real line.
36-
*/
37-
async moveCursorToEnd(): Promise<void> {
38-
const lastLine = (await this.editor.getText()).split('\n').length;
39-
await this.editor.setCursor(lastLine - 1, 1);
40-
41-
}
42-
43-
/**
44-
* On the first line containing `token`, replace `find` with `replace`.
45-
* Does not save.
46-
* @throws if no line contains `token`.
47-
*/
48-
async replaceTextWithinLineContaining(token: string, find: string, replace: string) : Promise<void>{
49-
const lineNum = await this.editor.getLineOfText(token);
50-
if (lineNum < 1) throw new Error(`Could not find a line containing "${token}"`);
51-
const oldLine = await this.editor.getTextAtLine(lineNum);
52-
await this.editor.setTextAtLine(lineNum, oldLine.replace(find, replace));
53-
}
54-
55-
/**
56-
* Position the cursor, trigger the hover widget, and return its text.
57-
* @param elementDescription Human-readable element name, for logging.
58-
*/
59-
async hoverOver(
60-
line: number,
61-
column: number,
62-
elementDescription: string,
63-
timeoutMs = 15000
64-
): Promise<string> {
65-
await this.editor.setCursor(line, column);
66-
await new Workbench().executeCommand('editor.action.showHover');
67-
return utils.waitForHoverWidget(VSBrowser.instance.driver, elementDescription, timeoutMs);
68-
}
69-
70-
71-
72-
}
25+
}

src/test/utils/editorUtils.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { TextEditor, VSBrowser, Workbench } from 'vscode-extension-tester';
2+
import * as utils from './testUtils';
3+
4+
/**
5+
* Reset the editor to an empty, saved buffer.
6+
*/
7+
export async function clearEditor(editor: TextEditor): Promise<void> {
8+
await editor.setText('');
9+
await editor.save();
10+
}
11+
12+
/**
13+
* Place the cursor at the start of the last line of content.
14+
* Note: split('\n').length counts a trailing empty segment when the file
15+
* ends in a newline, so `lastLine - 1` lands on the last real line.
16+
*/
17+
export async function moveCursorToEnd(editor: TextEditor): Promise<void> {
18+
const lastLine = (await editor.getText()).split('\n').length;
19+
await editor.setCursor(lastLine - 1, 1);
20+
}
21+
22+
/**
23+
* On the first line containing `token`, replace `find` with `replace`.
24+
* Does not save.
25+
* @throws if no line contains `token`.
26+
*/
27+
export async function replaceTextWithinLineContaining(
28+
editor: TextEditor,
29+
token: string,
30+
find: string,
31+
replace: string
32+
): Promise<void> {
33+
const lineNum = await editor.getLineOfText(token);
34+
if (lineNum < 1) throw new Error(`Could not find a line containing "${token}"`);
35+
const oldLine = await editor.getTextAtLine(lineNum);
36+
await editor.setTextAtLine(lineNum, oldLine.replace(find, replace));
37+
}
38+
39+
/**
40+
* Position the cursor, trigger the hover widget, and return its text.
41+
* @param elementDescription Human-readable element name, for logging.
42+
*/
43+
export async function hoverOver(
44+
editor: TextEditor,
45+
line: number,
46+
column: number,
47+
elementDescription: string,
48+
timeoutMs = 15000
49+
): Promise<string> {
50+
await editor.setCursor(line, column);
51+
await new Workbench().executeCommand('editor.action.showHover');
52+
return utils.waitForHoverWidget(VSBrowser.instance.driver, elementDescription, timeoutMs);
53+
}

0 commit comments

Comments
 (0)