fix: use logger from config if specified (#8697)

This commit is contained in:
Yury Semikhatsky 2021-09-03 14:21:25 -07:00 committed by GitHub
parent 5d278db17b
commit e5e461c0de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 3 deletions

View File

@ -69,7 +69,7 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel, chann
}
async launch(options: LaunchOptions = {}): Promise<Browser> {
const logger = options.logger;
const logger = options.logger || this._defaultLaunchOptions.logger;
return this._wrapApiCall(async (channel: channels.BrowserTypeChannel) => {
assert(!(options as any).userDataDir, 'userDataDir option is not supported in `browserType.launch`. Use `browserType.launchPersistentContext` instead');
assert(!(options as any).port, 'Cannot specify a port without launching as a server.');
@ -94,6 +94,7 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel, chann
}
async launchPersistentContext(userDataDir: string, options: LaunchPersistentContextOptions = {}): Promise<BrowserContext> {
const logger = options.logger || this._defaultLaunchOptions.logger;
return this._wrapApiCall(async (channel: channels.BrowserTypeChannel) => {
assert(!(options as any).port, 'Cannot specify a port without launching as a server.');
options = { ...this._defaultLaunchOptions, ...this._defaultContextOptions, ...options };
@ -109,11 +110,11 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel, chann
const result = await channel.launchPersistentContext(persistentParams);
const context = BrowserContext.from(result.context);
context._options = contextParams;
context._logger = options.logger;
context._logger = logger;
context._setBrowserType(this);
await this._onDidCreateContext?.(context);
return context;
}, options.logger);
}, logger);
}
connect(options: api.ConnectOptions & { wsEndpoint?: string }): Promise<api.Browser>;

View File

@ -220,6 +220,34 @@ test('should respect context options in various contexts', async ({ runInlineTes
expect(result.passed).toBe(4);
});
test('should call logger from launchOptions config', async ({ runInlineTest }, testInfo) => {
const result = await runInlineTest({
'a.test.ts': `
const { test } = pwt;
const log = [];
test.use({
launchOptions: {
logger: {
log: (name, severity, message) => log.push({name, severity, message}),
isEnabled: (name, severity) => severity !== 'verbose'
}
}
});
test('should support config logger', async ({browser}) => {
expect(browser.version()).toBeTruthy();
expect(log.length > 0).toBeTruthy();
expect(log.filter(item => item.severity === 'info').length > 0).toBeTruthy();
expect(log.filter(item => item.message.includes('browserType.launch started')).length > 0).toBeTruthy();
expect(log.filter(item => item.message.includes('browserType.launch succeeded')).length > 0).toBeTruthy();
});
`,
}, { workers: 1 });
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
});
test('should report error and pending operations on timeout', async ({ runInlineTest }, testInfo) => {
const result = await runInlineTest({
'a.test.ts': `