Class InterceptNetwork
java.lang.Object
net.serenitybdd.screenplay.playwright.network.InterceptNetwork
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 TypeMethodDescriptionnet.serenitybdd.screenplay.PerformableandAbort()Abort matching requests (useful for blocking resources).net.serenitybdd.screenplay.PerformableandContinueWithHeader(String name, String value) Continue the request with an additional header.net.serenitybdd.screenplay.PerformableHandle requests with a custom route handler.net.serenitybdd.screenplay.playwright.network.InterceptAndFulfillandRespondWith(String body) Respond with a custom body.net.serenitybdd.screenplay.playwright.network.InterceptAndFulfillJsonandRespondWithJson(Object jsonObject) Respond with a JSON body (automatically serialized and content-type set).static InterceptNetworkStart intercepting requests matching the URL pattern.
-
Method Details
-
forUrl
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
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).
-