java.lang.Object
net.serenitybdd.screenplay.playwright.network.InterceptNetwork

public class InterceptNetwork extends Object
Intercept network requests and modify their behavior.

Playwright's route API allows you to intercept, modify, or mock network requests. This is useful for testing error scenarios, mocking APIs, or blocking resources.

  • Method Summary

    Modifier and Type
    Method
    Description
    net.serenitybdd.screenplay.Performable
    Abort matching requests (useful for blocking resources).
    net.serenitybdd.screenplay.Performable
    Continue the request with an additional header.
    net.serenitybdd.screenplay.Performable
    andHandle(Consumer<com.microsoft.playwright.Route> handler)
    Handle requests with a custom route handler.
    net.serenitybdd.screenplay.playwright.network.InterceptAndFulfill
    Respond with a custom body.
    net.serenitybdd.screenplay.playwright.network.InterceptAndFulfillJson
    Respond with a JSON body (automatically serialized and content-type set).
    forUrl(String urlPattern)
    Start intercepting requests matching the URL pattern.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • forUrl

      public static InterceptNetwork forUrl(String urlPattern)
      Start intercepting requests matching the URL pattern. Uses Playwright's glob pattern matching.
    • andAbort

      public net.serenitybdd.screenplay.Performable andAbort()
      Abort matching requests (useful for blocking resources).
    • andRespondWith

      public net.serenitybdd.screenplay.playwright.network.InterceptAndFulfill andRespondWith(String body)
      Respond with a custom body.
    • andContinueWithHeader

      public net.serenitybdd.screenplay.Performable andContinueWithHeader(String name, String value)
      Continue the request with an additional header.
    • andHandle

      public net.serenitybdd.screenplay.Performable andHandle(Consumer<com.microsoft.playwright.Route> handler)
      Handle requests with a custom route handler. Provides full control over the request/response.

      Example:

           InterceptNetwork.forUrl("**\/api/**").andHandle(route -> {
               if (route.request().method().equals("POST")) {
                   route.fulfill(new Route.FulfillOptions().setBody("{}"));
               } else {
                   route.resume();
               }
           });
       
    • andRespondWithJson

      public net.serenitybdd.screenplay.playwright.network.InterceptAndFulfillJson andRespondWithJson(Object jsonObject)
      Respond with a JSON body (automatically serialized and content-type set).