Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add exposeFunction to page #77

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add exposeFunction to page
aeschli committed Apr 6, 2023

Verified

This commit was signed with the committer’s verified signature.
aeschli Martin Aeschlimann
commit d9a3b7110fbc07dd9ad36f78aac6e8639aae7d8e
36 changes: 15 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -183,22 +183,16 @@ export async function runTests(options: Options & { extensionTestsPath: string }

const endpoint = `http://${host}:${port}`;

const configContext = async (context: playwright.BrowserContext) => {
context.once('close', () => server.close());

const configPage = async (page: playwright.Page, browser: playwright.Browser) => {
type Severity = 'error' | 'warning' | 'info';
const unreportedOutput: { type: Severity, args: unknown[] }[] = [];
await context.exposeFunction('codeAutomationLog', (type: Severity, args: unknown[]) => {
try {
console[type](...args);
} catch (_e) {
unreportedOutput.push({ type, args });
}
await page.exposeFunction('codeAutomationLog', (type: Severity, args: unknown[]) => {
console[type](...args);
});

await context.exposeFunction('codeAutomationExit', async (code: number) => {
await page.exposeFunction('codeAutomationExit', async (code: number) => {
try {
await context.browser()?.close();
await browser.close();
} catch (error) {
console.error(`Error when closing browser: ${error}`);
}
@@ -216,8 +210,10 @@ export async function runTests(options: Options & { extensionTestsPath: string }

}
console.log(`Opening browser on ${endpoint}...`);
const context = await openBrowser(endpoint, options, configContext);
if (!context) {
const context = await openBrowser(endpoint, options, configPage);
if (context) {
context.once('close', () => server.close());
} else {
server.close();
e(new Error('Can not run test as opening of browser failed.'));
}
@@ -256,11 +252,8 @@ export async function open(options: Options): Promise<Disposable> {

const endpoint = `http://${host}:${port}`;

const configContext = async (context: playwright.BrowserContext) => {
context.once('close', () => server.close());
};

const context = await openBrowser(endpoint, options, configContext);
const context = await openBrowser(endpoint, options);
context?.once('close', () => server.close());
return {
dispose: () => {
server.close();
@@ -270,7 +263,7 @@ export async function open(options: Options): Promise<Disposable> {

}

async function openBrowser(endpoint: string, options: Options, configureContext: (context: playwright.BrowserContext) => Promise<void>): Promise<playwright.BrowserContext | undefined> {
async function openBrowser(endpoint: string, options: Options, configPage?: (page: playwright.Page, browser: playwright.Browser) => Promise<void>): Promise<playwright.BrowserContext | undefined> {
if (options.browserType === 'none') {
return undefined;
}
@@ -298,8 +291,6 @@ async function openBrowser(endpoint: string, options: Options, configureContext:
context.grantPermissions(options.permissions);
}

await configureContext(context);

// forcefully close browser if last page is closed. workaround for https://github.com/microsoft/playwright/issues/2946
let openPages = 0;
context.on('page', page => {
@@ -314,6 +305,9 @@ async function openBrowser(endpoint: string, options: Options, configureContext:


const page = context.pages()[0] ?? await context.newPage();
if (configPage) {
await configPage(page, browser);
}
if (options.waitForDebugger) {
await page.waitForFunction(() => '__jsDebugIsReady' in globalThis);
}