playwright-performance-reporter
Version:
Measure and publish performance metrics from browser dev-tools when running playwright
81 lines (80 loc) • 2.22 kB
JavaScript
import { ChromiumDevelopmentTools } from '../browsers/chromium/index.js';
import { FirefoxDevelopmentTools } from '../browsers/firefox/index.js';
import { WebkitDevelopmentTools } from '../browsers/webkit/index.js';
export class MetricsEngine {
/**
* Client reference to the browser
*/
browser;
/**
* Options for connection to the browser
*/
browserOptions = {};
/**
* Starts client
*
* @param browser which client to setup
*/
async setupBrowser(browser, options) {
this.browserOptions = options;
switch (browser) {
case 'chromium': {
this.browser = new ChromiumDevelopmentTools(this.browserOptions);
break;
}
case 'firefox': {
this.browser = new FirefoxDevelopmentTools(this.browserOptions);
break;
}
case 'webkit': {
this.browser = new WebkitDevelopmentTools(this.browserOptions);
break;
}
default: {
this.browser = undefined;
return false;
}
}
await this.browser?.connect();
return true;
}
/**
* Shutdown client
*/
destroy() {
this.browser?.destroy();
this.browser = undefined;
}
/**
* Get current running client
*/
getBrowser() {
return this.browser?.getBrowserName();
}
/**
* Dispatches metric fetch from browser and return metric
*
* @param metric which metric to measure
* @param hookOrder step to run
*/
async getMetric(metric, hookOrder) {
try {
return await this.browser?.getMetric(metric, hookOrder);
}
catch { }
return undefined;
}
/**
* Dispatches custom metric fetch from browser and return metric
*
* @param customMetric user defined fetch function
* @param hookOrder step to run
*/
async runCustomMetric(customMetric, hookOrder) {
try {
return await this.browser?.runCustomObserver(customMetric, hookOrder);
}
catch { }
return undefined;
}
}