Skip to content

Commit

Permalink
test(player): play in incognito
Browse files Browse the repository at this point in the history
  • Loading branch information
ruifigueira committed Dec 21, 2024
1 parent 7cdf7ea commit a0f9e7b
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 28 deletions.
13 changes: 11 additions & 2 deletions tests/crx/crxRecorderTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export { expect } from './crxTest';
declare function attach(tab: chrome.tabs.Tab): Promise<void>;
declare function _setUnderTest(): void;

type SettingOptions = {
testIdAttributeName?: string,
targetLanguage?: string,
playInIncognito?: boolean,
experimental?: boolean
};

export function dumpLogHeaders(recorderPage: Page) {
return async () => {
return await recorderPage.evaluate(() => {
Expand Down Expand Up @@ -75,7 +82,7 @@ export const test = crxTest.extend<{
recorderPage: Page;
recordAction<T = void>(action: () => Promise<T>): Promise<T>;
recordAssertion(locator: Locator, type: AssertAction['name']): Promise<void>;
configureRecorder: (config: { testIdAttributeName?: string, targetLanguage?: string, experimental?: boolean }) => Promise<void>;
configureRecorder: (config: SettingOptions) => Promise<void>;
}>({
extensionPath: path.join(__dirname, '../../examples/recorder-crx/dist'),

Expand Down Expand Up @@ -160,14 +167,16 @@ export const test = crxTest.extend<{
},

configureRecorder: async ({ context, extensionId }, run) => {
await run(async ({ testIdAttributeName, targetLanguage, experimental }: { testIdAttributeName?: string, targetLanguage?: string, experimental?: boolean }) => {
await run(async ({ testIdAttributeName, targetLanguage, playInIncognito, experimental }: SettingOptions) => {
const configPage = await context.newPage();
try {
await configPage.goto(`chrome-extension://${extensionId}/preferences.html`);
if (targetLanguage)
await configPage.locator('#target-language').selectOption(targetLanguage);
if (testIdAttributeName)
await configPage.locator('#test-id').fill(testIdAttributeName);
if (playInIncognito !== undefined)
await configPage.locator('#playInIncognito').setChecked(playInIncognito);
if (experimental !== undefined)
await configPage.locator('#experimental').setChecked(experimental);
await configPage.locator('#submit').click();
Expand Down
76 changes: 76 additions & 0 deletions tests/crx/player-incognito.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Copyright (c) Rui Figueira.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { dumpLogHeaders, expect, test } from './crxRecorderTest';
import { editCode } from './utils';

test('should play in incognito', async ({ configureRecorder, attachRecorder, page, baseURL, extensionServiceWorker }) => {
await configureRecorder({ playInIncognito: true });

const recorderPage = await attachRecorder(page);
await recorderPage.getByTitle('Record').click();

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

const incognitoPagePromise = extensionServiceWorker.evaluate(url => new Promise<void>(resolve => {
chrome.tabs.onUpdated.addListener((_, changes, tab) => {
if (!tab.incognito || changes.url !== url)
return;
resolve();
});
}), `${baseURL}/input/textarea.html`);

await recorderPage.getByTitle('Resume (F8)').click();
await incognitoPagePromise;

await expect.poll(dumpLogHeaders(recorderPage)).toEqual([
`► page.goto( ${baseURL}/input/textarea.html ) ✅ — XXms`,
`► page.locator('textarea') .fill() ✅ — XXms`,
]);
});

test('should close and reopen incognito window on replay', async ({ configureRecorder, attachRecorder, page, baseURL, extensionServiceWorker }) => {
await configureRecorder({ playInIncognito: true });

const recorderPage = await attachRecorder(page);
await recorderPage.getByTitle('Record').click();

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

await recorderPage.getByTitle('Resume (F8)').click();
await expect(recorderPage.getByTitle('Resume (F8)')).not.toBeDisabled();

const getIncognitoTabIds = async () => await extensionServiceWorker.evaluate(() => chrome.tabs.query({}).then(ts => ts.filter(t => t.incognito).map(t => t.id!)));
const tabIds = await getIncognitoTabIds();
expect(tabIds).toHaveLength(1);

await recorderPage.getByTitle('Resume (F8)').click();
await expect(recorderPage.getByTitle('Resume (F8)')).not.toBeDisabled();

const newTabIds = await getIncognitoTabIds();
expect(newTabIds).toHaveLength(1);
expect(newTabIds[0]).not.toEqual(tabIds[0]);
});
26 changes: 1 addition & 25 deletions tests/crx/recorder-edit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,9 @@
* limitations under the License.
*/

import type { 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').first();
await editor.press('ControlOrMeta+a');
await editor.fill(code);
}

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

async function moveCursorToLine(recorderPage: Page, line: number) {
await recorderPage.locator('.CodeMirror').first().evaluate((elem: any, line) => elem.CodeMirror.setCursor({
// codemirror line is 0-based
line: line - 1,
ch: 0,
}), line);
}

function editorLine(recorderPage: Page, linenumber: number) {
return recorderPage.locator('.CodeMirror-code > div')
.filter({ has: recorderPage.locator('.CodeMirror-linenumber', { hasText: String(linenumber) }) })
.locator('.CodeMirror-line');
}
import { editCode, editorLine, getCode, moveCursorToLine } from './utils';

test('should edit @smoke', async ({ page, attachRecorder, baseURL }) => {
const recorderPage = await attachRecorder(page);
Expand Down
26 changes: 25 additions & 1 deletion tests/crx/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,28 @@ export async function parseTraceRaw(file: string): Promise<{ events: any[], reso
actionObjects,
stacks,
};
}
}

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

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

export async function moveCursorToLine(recorderPage: Page, line: number) {
await recorderPage.locator('.CodeMirror').first().evaluate((elem: any, line) => elem.CodeMirror.setCursor({
// codemirror line is 0-based
line: line - 1,
ch: 0,
}), line);
}

export function editorLine(recorderPage: Page, linenumber: number) {
return recorderPage.locator('.CodeMirror-code > div')
.filter({ has: recorderPage.locator('.CodeMirror-linenumber', { hasText: String(linenumber) }) })
.locator('.CodeMirror-line');
}

0 comments on commit a0f9e7b

Please sign in to comment.