Skip to content

Commit

Permalink
E2E test: positronSettings POM (#5870)
Browse files Browse the repository at this point in the history
Implement the settings set/unset functionality in pure playwright.

### QA Notes

All tests should pass.
  • Loading branch information
testlabauto authored Jan 3, 2025
1 parent 0841d7b commit 05a6a6e
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 3 deletions.
1 change: 1 addition & 0 deletions test/automation/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ export * from './positron/positronOutline';
export * from './positron/positronClipboard';
export * from './positron/positronExtensions';
export * from './positron/positronEditors';
export * from './positron/positronSettings';
// --- End Positron ---
export { getDevElectronPath, getBuildElectronPath, getBuildVersion } from './electron';
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class PositronUserSettingsFixtures {
}

// Set the user settings
await this.app.workbench.settingsEditor.addUserSettings([
await this.app.workbench.positronSettings.addUserSettings([
// Set editor.wordWrap to "on" to avoid issues with long settings.
// See test/automation/src/settings.ts for more explanation.
['editor.wordWrap', '"on"'],
Expand All @@ -56,7 +56,7 @@ export class PositronUserSettingsFixtures {
*/
async unsetUserSettings(restartApp = false) {
// Clear all user settings
await this.app.workbench.settingsEditor.clearUserSettings();
await this.app.workbench.positronSettings.clearUserSettings();

// Restart the application if requested, to apply the settings
if (restartApp) {
Expand Down
25 changes: 24 additions & 1 deletion test/automation/src/positron/positronEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,18 @@ export class PositronEditor {
const textarea = `${editor} textarea`;
await expect(this.code.driver.page.locator(textarea)).toBeFocused();

await this.code.driver.page.locator(textarea).fill(text);
await this.code.driver.page.locator(textarea).evaluate((textarea, text) => {
const input = textarea as HTMLTextAreaElement;
const start = input.selectionStart || 0;
const value = input.value;
const newValue = value.substring(0, start) + text + value.substring(start);

input.value = newValue;
input.setSelectionRange(start + text.length, start + text.length);

const event = new Event('input', { bubbles: true, cancelable: true });
input.dispatchEvent(event);
}, text);

await this.waitForEditorContents(filename, c => c.indexOf(text) > -1, selectorPrefix);
}
Expand All @@ -110,4 +121,16 @@ export class PositronEditor {
return content;
}

async waitForEditorFocus(filename: string, lineNumber: number, selectorPrefix = ''): Promise<void> {
const editor = [selectorPrefix || '', EDITOR(filename)].join(' ');
const line = `${editor} .view-lines > .view-line:nth-child(${lineNumber})`;
const textarea = `${editor} textarea`;

await this.code.driver.page.locator(line).click();

await expect(async () => {
await expect(this.code.driver.page.locator(textarea)).toBeFocused();
}).toPass();
}

}
16 changes: 16 additions & 0 deletions test/automation/src/positron/positronEditors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ export class PositronEditors {
await expect(this.code.driver.page.locator(`.tabs-container div.tab.active${isDirty ? '.dirty' : ''}[aria-selected="true"][data-resource-name$="${fileName}"]`)).toBeVisible();
}

async waitForActiveTabNotDirty(fileName: string): Promise<void> {
await expect(
this.code.driver.page.locator(
`.tabs-container div.tab.active:not(.dirty)[aria-selected="true"][data-resource-name$="${fileName}"]`
)
).toBeVisible();
}

async newUntitledFile(): Promise<void> {
if (process.platform === 'darwin') {
await this.code.driver.page.keyboard.press('Meta+N');
Expand Down Expand Up @@ -56,4 +64,12 @@ export class PositronEditors {
async waitForTab(fileName: string, isDirty: boolean = false): Promise<void> {
await expect(this.code.driver.page.locator(`.tabs-container div.tab${isDirty ? '.dirty' : ''}[data-resource-name$="${fileName}"]`)).toBeVisible();
}

async saveOpenedFile(): Promise<any> {
if (process.platform === 'darwin') {
await this.code.driver.page.keyboard.press('Meta+S');
} else {
await this.code.driver.page.keyboard.press('Control+S');
}
}
}
40 changes: 40 additions & 0 deletions test/automation/src/positron/positronSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*---------------------------------------------------------------------------------------------
* Copyright (C) 2024 Posit Software, PBC. All rights reserved.
* Licensed under the Elastic License 2.0. See LICENSE.txt for license information.
*--------------------------------------------------------------------------------------------*/


import { Code } from '../code';
import { PositronEditor } from './positronEditor';
import { PositronEditors } from './positronEditors';
import { PositronQuickAccess } from './positronQuickaccess';

export class PositronSettings {

constructor(private code: Code, private editors: PositronEditors, private editor: PositronEditor, private quickaccess: PositronQuickAccess) { }

async addUserSettings(settings: [key: string, value: string][]): Promise<void> {
await this.openUserSettingsFile();
const file = 'settings.json';
await this.code.driver.page.keyboard.press('ArrowRight');
await this.editor.waitForTypeInEditor(file, settings.map(v => `"${v[0]}": ${v[1]},`).join(''));
await this.editors.saveOpenedFile();
await this.editors.waitForActiveTabNotDirty(file);
}

async clearUserSettings(): Promise<void> {
await this.openUserSettingsFile();
const file = 'settings.json';
await this.quickaccess.runCommand('editor.action.selectAll');
await this.code.driver.page.keyboard.press('Delete');
await this.editor.waitForTypeInEditor(file, `{`); // will auto close }
await this.editors.saveOpenedFile();
await this.editors.waitForActiveTabNotDirty(file);
await this.quickaccess.runCommand('workbench.action.closeActiveEditor');
}

async openUserSettingsFile(): Promise<void> {
await this.quickaccess.runCommand('workbench.action.openSettingsJson');
await this.editor.waitForEditorFocus('settings.json', 1);
}
}
3 changes: 3 additions & 0 deletions test/automation/src/workbench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import { PositronOutline } from './positron/positronOutline';
import { PositronClipboard } from './positron/positronClipboard';
import { PositronQuickInput } from './positron/positronQuickInput';
import { PositronExtensions } from './positron/positronExtensions';
import { PositronSettings } from './positron/positronSettings';
// --- End Positron ---

export interface Commands {
Expand Down Expand Up @@ -104,6 +105,7 @@ export class Workbench {
readonly positronQuickInput: PositronQuickInput;
readonly positronExtensions: PositronExtensions;
readonly positronEditors: PositronEditors;
readonly positronSettings: PositronSettings;
// --- End Positron ---

constructor(code: Code) {
Expand Down Expand Up @@ -153,6 +155,7 @@ export class Workbench {
this.positronOutline = new PositronOutline(code, this.positronQuickaccess);
this.positronClipboard = new PositronClipboard(code);
this.positronExtensions = new PositronExtensions(code, this.positronQuickaccess);
this.positronSettings = new PositronSettings(code, this.positronEditors, this.positronEditor, this.positronQuickaccess);
// --- End Positron ---
}
}

0 comments on commit 05a6a6e

Please sign in to comment.