有什么方法可以使用 Puppeteer 或 Chrome DevTools 来延迟 Chrome 响应负载?
Is there any way to delay a Chrome response payload using Puppeteer or Chrome DevTools?
下面是我使用 PuppeteerSharp 库在 Chrome 浏览器中延迟 请求 的代码。
public static void HandleRequestIntercepted(object sender, RequestEventArgs args)
{
Task.Run(async () => {
await Task.Delay(1000);
await args.Request.ContinueAsync();
});
}
static void Main(string[] args)
{
Task.Run(async () =>
{
Browser b = await Puppeteer.LaunchAsync(new LaunchOptions()
{
ExecutablePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
DefaultViewport = null,
Headless = false
});
Page[] pages = await b.PagesAsync();
Page page = pages[0];
CDPSession cdpSession = await page.Target.CreateCDPSessionAsync();
JObject obj = await cdpSession.SendAsync("Network.enable");
await page.SetRequestInterceptionAsync(true);
// Subscribe to requests
page.Request += HandleRequestIntercepted;
}
}
但是,是否可以类似地延迟响应?具体来说,我希望 Chrome 将响应负载延迟 X 时间。
我能够使用以下代码引入响应延迟:
public static CDPSession _cdpSessionResponse;
public static void CdpSessionMessageReceived(object sender, MessageEventArgs args)
{
if (args.MessageID == "Fetch.requestPaused")
{
Task.Run(async () =>
{
await Task.Delay(3000); // Wait 3 seconds
await _cdpSessionResponse.SendAsync("Fetch.continueRequest",
new Dictionary<string, object>
{
{ "requestId", args.MessageData.ToObject<FetchRequestPausedResponse>().RequestId }
});
});
}
}
static void Main(string[] args)
{
Task.Run(async () =>
{
Browser b = await Puppeteer.LaunchAsync(new LaunchOptions()
{
ExecutablePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
DefaultViewport = null,
Headless = false
});
Page[] pages = await b.PagesAsync();
Page page = pages[0];
// Set up RESPONSE interception using PuppeteerSharp
_cdpSessionResponse = await page.Target.CreateCDPSessionAsync();
JObject response = await _cdpSessionResponse.SendAsync("Fetch.enable",
new Dictionary<string, object>
{
{
"patterns", new Dictionary<string, object>[]
{
new Dictionary<string, object>
{
{ "requestStage", "Response" }
}
}
}
});
_cdpSessionResponse.MessageReceived += CdpSessionMessageReceived;
}
}
internal class FetchRequestPausedResponse
{
public string RequestId { get; set; }
public string NetworkId { get; set; }
}
然而,这令人费解,因为这是我在 Chrome DevTools 网络选项卡中看到的内容——并非所有响应都被一条长蓝线延迟:
为了比较,如果我将行 { "requestStage", "Response" }
更改为 { "requestStage", "Request" }
,我看到所有请求都被延迟了一条长透明线(排队时间):
我不确定为什么会有这种差异 - 也许这是 Chrome DevTools 协议的内部原因?
下面是我使用 PuppeteerSharp 库在 Chrome 浏览器中延迟 请求 的代码。
public static void HandleRequestIntercepted(object sender, RequestEventArgs args)
{
Task.Run(async () => {
await Task.Delay(1000);
await args.Request.ContinueAsync();
});
}
static void Main(string[] args)
{
Task.Run(async () =>
{
Browser b = await Puppeteer.LaunchAsync(new LaunchOptions()
{
ExecutablePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
DefaultViewport = null,
Headless = false
});
Page[] pages = await b.PagesAsync();
Page page = pages[0];
CDPSession cdpSession = await page.Target.CreateCDPSessionAsync();
JObject obj = await cdpSession.SendAsync("Network.enable");
await page.SetRequestInterceptionAsync(true);
// Subscribe to requests
page.Request += HandleRequestIntercepted;
}
}
但是,是否可以类似地延迟响应?具体来说,我希望 Chrome 将响应负载延迟 X 时间。
我能够使用以下代码引入响应延迟:
public static CDPSession _cdpSessionResponse;
public static void CdpSessionMessageReceived(object sender, MessageEventArgs args)
{
if (args.MessageID == "Fetch.requestPaused")
{
Task.Run(async () =>
{
await Task.Delay(3000); // Wait 3 seconds
await _cdpSessionResponse.SendAsync("Fetch.continueRequest",
new Dictionary<string, object>
{
{ "requestId", args.MessageData.ToObject<FetchRequestPausedResponse>().RequestId }
});
});
}
}
static void Main(string[] args)
{
Task.Run(async () =>
{
Browser b = await Puppeteer.LaunchAsync(new LaunchOptions()
{
ExecutablePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
DefaultViewport = null,
Headless = false
});
Page[] pages = await b.PagesAsync();
Page page = pages[0];
// Set up RESPONSE interception using PuppeteerSharp
_cdpSessionResponse = await page.Target.CreateCDPSessionAsync();
JObject response = await _cdpSessionResponse.SendAsync("Fetch.enable",
new Dictionary<string, object>
{
{
"patterns", new Dictionary<string, object>[]
{
new Dictionary<string, object>
{
{ "requestStage", "Response" }
}
}
}
});
_cdpSessionResponse.MessageReceived += CdpSessionMessageReceived;
}
}
internal class FetchRequestPausedResponse
{
public string RequestId { get; set; }
public string NetworkId { get; set; }
}
然而,这令人费解,因为这是我在 Chrome DevTools 网络选项卡中看到的内容——并非所有响应都被一条长蓝线延迟:
为了比较,如果我将行 { "requestStage", "Response" }
更改为 { "requestStage", "Request" }
,我看到所有请求都被延迟了一条长透明线(排队时间):
我不确定为什么会有这种差异 - 也许这是 Chrome DevTools 协议的内部原因?