Selenium 4 NetworkInterceptor在内部是如何工作的

huangapple go评论72阅读模式
英文:

How Selenium 4 NetworkInterceptor works internally

问题

In Selenium 4,有以下代码来拦截网络请求:

NetworkInterceptor interceptor = new NetworkInterceptor(
      driver,
      Route.matching(req -> true)
        .to(() -> req -> new HttpResponse()
          .setStatus(200)
          .addHeader("Content-Type", MediaType.HTML_UTF_8.toString())
          .setContent(utf8String("Creamy, delicious cheese!"))));

我的问题是,如果我从未使用这个名为 "interceptor" 的变量,那么它是如何被使用的,或者是由谁使用的?

英文:

In Selenium 4 , there is provision to intercept network by below code:

NetworkInterceptor interceptor = new NetworkInterceptor(
      driver,
      Route.matching(req -> true)
        .to(() -> req -> new HttpResponse()
          .setStatus(200)
          .addHeader("Content-Type", MediaType.HTML_UTF_8.toString())
          .setContent(utf8String("Creamy, delicious cheese!"))));

my question is that I never use that variable interceptor, then how or who usage it?

答案1

得分: 0

如果您查看NetworkInterceptor的源代码:

public NetworkInterceptor(WebDriver driver, Filter filter) {
  Require.nonNull("WebDriver", driver);
  Require.nonNull("HTTP filter", filter);

  if (!(driver instanceof HasDevTools)) {
    throw new IllegalArgumentException("WebDriver instance must implement HasDevTools");
  }

  this.tools = ((HasDevTools) driver).getDevTools();
  tools.createSessionIfThereIsNotOne();

  tools.getDomains().network().interceptTrafficWith(filter);
}

您将看到,当您使用new创建对象时,实际上建立了一种特殊的连接,将您的代码与您的驱动程序连接起来:

this.tools = ((HasDevTools) driver).getDevTools();
tools.createSessionIfThereIsNotOne()

然后通过该连接在驱动程序端配置了“stub”:

tools.getDomains().network().interceptTrafficWith(filter);

之后,您的客户端代码不需要引用NetworkInterceptor(除非要关闭它,但由于它是AutoCloseable,您应该在try with resources块内使用它),因为所有设置已经配置为WebDriver。

此外,您可以从这些源代码中了解到拦截器并不总是起作用。它仅适用于支持DevTools的驱动程序,如Chromium、Chrome、Edge和Firefox。

英文:

If you look inside NetworkInterceptor sources:

  public NetworkInterceptor(WebDriver driver, Filter filter) {
    Require.nonNull("WebDriver", driver);
    Require.nonNull("HTTP filter", filter);

    if (!(driver instanceof HasDevTools)) {
      throw new IllegalArgumentException("WebDriver instance must implement HasDevTools");
    }

    this.tools = ((HasDevTools) driver).getDevTools();
    tools.createSessionIfThereIsNotOne();

    tools.getDomains().network().interceptTrafficWith(filter);
  }

you will see that when you create an object with new it actually establishes a special kind of connection between your code and your driver:

this.tools = ((HasDevTools) driver).getDevTools();
tools.createSessionIfThereIsNotOne()

and then configures "stub" on driver side through that connection:

tools.getDomains().network().interceptTrafficWith(filter);

After that your client code does not need a reference to NetworkInterceptor (except of closing it but since it is AutoCloseable you should use it within try with resources block) because all the setup already configured for WebDriver.

Also what you can pick from those sources is that the interceptor won't always works. It only works with drivers supporting DevTools such as: Chrominum, Chrome, Edge, Firefox

huangapple
  • 本文由 发表于 2023年5月29日 22:34:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定