Skip to content

Commit

Permalink
fix(parse): crxApp.recorder.load in now working
Browse files Browse the repository at this point in the history
  • Loading branch information
ruifigueira committed Dec 7, 2024
1 parent a98de51 commit b076f24
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/recorder-crx/src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,4 @@ chrome.runtime.onMessage.addListener((message) => {
});

// for testing
Object.assign(self, { attach, setTestIdAttributeName, _debug, _setUnderTest });
Object.assign(self, { attach, setTestIdAttributeName, getCrxApp, _debug, _setUnderTest });
14 changes: 7 additions & 7 deletions src/server/recorder/crxRecorderApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export type RecorderMessage = { type: 'recorder' } & (
| { method: 'setMode', mode: Mode }
| { method: 'setSources', sources: Source[] }
| { method: 'setActions', actions: ActionInContext[], sources: Source[] }
| { method: 'setRunningFile', file?: string }
| { method: 'elementPicked', elementInfo: ElementInfo, userGesture?: boolean }
);

Expand Down Expand Up @@ -92,7 +91,11 @@ export class CrxRecorderApp extends EventEmitter implements IRecorderApp {
}

this.setMode(mode);
this.setRunningFile(language);
}

load(code: string) {
this._updateCode(code);
this._editedCode?.load();
}

async close() {
Expand Down Expand Up @@ -126,11 +129,8 @@ export class CrxRecorderApp extends EventEmitter implements IRecorderApp {
this._sendMessage({ type: 'recorder', method: 'setMode', mode });
}

async setRunningFile(file?: string) {
// hack to prevent recorder from opening files
if (file?.endsWith('.js'))
return;
this._sendMessage({ type: 'recorder', method: 'setRunningFile', file });
async setRunningFile() {
// this doesn't make sense in crx, it only runs recorded files
}

async setSources(sources: Source[]) {
Expand Down
33 changes: 32 additions & 1 deletion tests/crx/recorder-edit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

import { Page } from '@playwright/test';
import { test, expect } from './crxRecorderTest';
import type { CrxApplication } from '../../test';

async function editCode(recorderPage: Page, code: string) {
const editor = recorderPage.locator('.CodeMirror textarea');
await editor.press('ControlOrMeta+a');
await editor.fill(code);
}

async function getCode(recorderPage: Page) {
async function getCode(recorderPage: Page): Promise<string> {
return await recorderPage.locator('.CodeMirror').evaluate(elem => (elem as any).CodeMirror.getValue());
}

Expand Down Expand Up @@ -218,3 +219,33 @@ def test_example(page: Page) -> None:
await recorderPage.getByTitle('Step Over (F10)').click();
await expect(recorderPage.locator('.source-line-paused .CodeMirror-line')).toHaveText(` await page.locator('textarea')`);
});

test('should load script using api', async ({ page, attachRecorder, extensionServiceWorker, baseURL }) => {
const recorderPage = await attachRecorder(page);
await recorderPage.getByTitle('Record').click();

const code = `import { test, expect } from '@playwright/test';
test('test', async ({ page }) => {
await page.goto('${baseURL}/input/textarea.html');
await page.locator('textarea').fill('modified test');
});`;

await extensionServiceWorker.evaluate(async code => {
const crxApp = await (globalThis as any).getCrxApp() as CrxApplication;
await crxApp.recorder.load(code);
}, code);

expect(await getCode(recorderPage)).toBe(code);

await extensionServiceWorker.evaluate(async () => {
const crxApp = await (globalThis as any).getCrxApp() as CrxApplication;
await crxApp.recorder.load(`import { test, expect } from '@playwright/test';
test('test', async ({ page }) => {
await page.gotoError();
});`);
});

await expect(recorderPage.locator('.source-line-error-widget')).toHaveText('Invalid locator (4:8)');
});

0 comments on commit b076f24

Please sign in to comment.