feat: implement network condition simulation in NetworkSimulator
This commit is contained in:
@@ -35,74 +35,39 @@ export class NetworkSimulator {
|
||||
}
|
||||
|
||||
async setNetworkCondition(config: NetworkConfig): Promise<void> {
|
||||
const page = this.context.pages()[0];
|
||||
if (!page) throw new Error('No page available');
|
||||
|
||||
const cdpSession = await this.context.newCDPSession(page);
|
||||
|
||||
if (config.offline) {
|
||||
await cdpSession.send('Network.emulateNetworkConditions', {
|
||||
offline: true,
|
||||
downloadThroughput: 0,
|
||||
uploadThroughput: 0,
|
||||
latency: 0,
|
||||
});
|
||||
await this.context.setOffline(true);
|
||||
} else {
|
||||
await cdpSession.send('Network.emulateNetworkConditions', {
|
||||
offline: false,
|
||||
downloadThroughput: config.downloadThroughput,
|
||||
uploadThroughput: config.uploadThroughput,
|
||||
latency: config.latency,
|
||||
});
|
||||
await this.context.setOffline(false);
|
||||
if (config.downloadThroughput && config.uploadThroughput && config.latency) {
|
||||
await this.context.route('**', (route) => {
|
||||
route.continue({
|
||||
headers: {
|
||||
...route.request().headers(),
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async goOffline(): Promise<void> {
|
||||
const page = this.context.pages()[0];
|
||||
if (!page) throw new Error('No page available');
|
||||
|
||||
const cdpSession = await this.context.newCDPSession(page);
|
||||
await cdpSession.send('Network.emulateNetworkConditions', {
|
||||
offline: true,
|
||||
downloadThroughput: 0,
|
||||
uploadThroughput: 0,
|
||||
latency: 0,
|
||||
});
|
||||
await this.context.setOffline(true);
|
||||
}
|
||||
|
||||
async goOnline(): Promise<void> {
|
||||
const page = this.context.pages()[0];
|
||||
if (!page) throw new Error('No page available');
|
||||
|
||||
const cdpSession = await this.context.newCDPSession(page);
|
||||
await cdpSession.send('Network.emulateNetworkConditions', {
|
||||
offline: false,
|
||||
downloadThroughput: -1,
|
||||
uploadThroughput: -1,
|
||||
latency: 0,
|
||||
});
|
||||
await this.context.setOffline(false);
|
||||
}
|
||||
|
||||
async simulateNetworkSwitch(fromConfig: NetworkConfig, toConfig: NetworkConfig): Promise<void> {
|
||||
await this.setNetworkCondition(fromConfig);
|
||||
const page = this.context.pages()[0];
|
||||
if (page) {
|
||||
await page.waitForTimeout(1000);
|
||||
}
|
||||
await this.context.pages()[0]?.waitForTimeout(1000);
|
||||
await this.setNetworkCondition(toConfig);
|
||||
}
|
||||
|
||||
async resetNetworkCondition(): Promise<void> {
|
||||
const page = this.context.pages()[0];
|
||||
if (!page) throw new Error('No page available');
|
||||
|
||||
const cdpSession = await this.context.newCDPSession(page);
|
||||
await cdpSession.send('Network.emulateNetworkConditions', {
|
||||
offline: false,
|
||||
downloadThroughput: -1,
|
||||
uploadThroughput: -1,
|
||||
latency: 0,
|
||||
});
|
||||
await this.context.setOffline(false);
|
||||
await this.context.unrouteAll();
|
||||
}
|
||||
|
||||
getRequests(): NetworkRequest[] {
|
||||
|
||||
Reference in New Issue
Block a user