我正在尝试使用chrome-remote-interface
example作为起点,在chrome --headless
(Chrome版本59)中模拟常规3G连接
const CDP = require('chrome-remote-interface');
const fs = require('fs');
CDP(async (client) => {
try {
const {Page, Tracing, Network} = client;
Network.emulateNetworkConditions({
offline: false,
latency: 100,
downloadThroughput: 750 * 1024 / 8,
uploadThroughput: 250 * 1024 / 8
});
await Promise.all([Network.enable(), Page.enable()]);
const { result } = await Network.canEmulateNetworkConditions();
console.log(result); // always false
// trace a page load
const events = [];
Tracing.dataCollected(({value}) => {
events.push(...value);
});
await Tracing.start();
await Page.navigate({url: 'https://github.com'});
await Page.loadEventFired();
await Tracing.end();
await Tracing.tracingComplete();
// save the tracing data
fs.writeFileSync('./timeline.json', JSON.stringify(events));
} catch (err) {
console.error(err);
} finally {
await client.close();
}
}).on('error', (err) => {
console.error(err);
});
但Network.emulateNetworkConditions
似乎没有用。要证明Network.canEmulateNetworkConditions()
始终返回false
。没有--headless
标记Network.canEmulateNetworkConditions()
会返回true
。
chrome-canary --headless
(第61版)给出了相同的结果。
我的示例是否包含错误或chrome 59目前缺少此功能?