我需要在调用connection
时传递lighthouse
参数
https://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-core/index.js#L41
async function lighthouse(url, flags = {}, configJSON, connection) {
// verify the url is valid and that protocol is allowed
if (url && (!URL.isValid(url) || !URL.isProtocolAllowed(url))) {
throw new LHError(LHError.errors.INVALID_URL);
}
// set logging preferences, assume quiet
flags.logLevel = flags.logLevel || 'error';
log.setLevel(flags.logLevel);
const config = generateConfig(configJSON, flags);
connection = connection || new ChromeProtocol(flags.port, flags.hostname);
// kick off a lighthouse run
return Runner.run(connection, {url, config});
}
在我的testcafe中,我的测试看起来像
test('Run lighthouse, async t => {
lighthouse('https://www.youtube.com', {}, {}, ????)
})
我无法检索testcafe打开的chrome实例的connection
,而不是生成新的chromeRunner
我做了类似的事情,我使用CLI在特定端口上使用谷歌浏览器启动灯塔
npm run testcafe -- chrome:headless:cdpPort=1234
然后我使灯塔功能将端口作为参数
export default async function lighthouseAudit(url, browser_port){
let result = await lighthouse(url, {
port: browser_port, // Google Chrome port Number
output: 'json',
logLevel: 'info',
});
return result;
};
然后你可以简单地运行审计
test(`Generate Light House Result `, async t => {
auditResult = await lighthouseAudit('https://www.youtube.com',1234);
});
希望它有所帮助