How the Java client is built: the driver, the wire protocol, the object model, the threading model, the generated API and the test harness. Read it before you change playwright/src/main/java/com/microsoft/playwright/impl or the driver module. The upstream checkout in .upstream/ is the reference for client logic, protocol and tests. The rules for porting the client are in playwright/src/main/AGENTS.md, the rules for porting the tests in playwright/src/test/AGENTS.md.
Four things are contracts, and the tests check them; everything else is an implementation choice.
- The public API:
com.microsoft.playwright,.optionsand.assertions. Generated from the upstream API docs; the test sources compile against it. - The driver and protocol: the stock Node.js
playwright-coredriver, spoken to over the same JSON protocol as every other language port. Java never talks to a browser. - The execution model: a synchronous, single-threaded API; events are delivered only while the caller is inside a Playwright call.
- The error surface:
PlaywrightException,TimeoutError,TargetClosedError, and driver error text that the tests check withcontains.
| Path | Role |
|---|---|
driver/ |
Driver, DriverJar, DriverLogging plus the resource driver/package/ = the playwright-core package built from source by scripts/build_driver.sh (not in git) |
driver-bundle/ |
resource driver/<platform>/node for mac, mac-arm64, linux, linux-arm64, win32_x64 (same script); optional when PLAYWRIGHT_NODEJS_PATH is set |
playwright/ |
the client: generated API packages, hand-written impl, the junit package, and all tests |
.upstream/ |
gitignored checkout of microsoft/playwright at the commit in scripts/DRIVER_VERSION; the driver, the API docs and the protocol spec are all read from it |
scripts/ |
DRIVER_VERSION (an upstream commit), NODE_VERSION, build_driver.sh, generate_api.sh (runs generate_java_api.mts and generate_java_channels.js), roll_driver.sh, release helpers |
Java 8 source level. Gson for all JSON, JSpecify for nullness, opentest4j for assertion errors. Roll and release steps are in ROLLING.md and the playwright-roll / playwright-java-release skills.
scripts/DRIVER_VERSIONpins amicrosoft/playwrightcommit andscripts/NODE_VERSIONthe Node.js version;scripts/build_driver.shbuildsplaywright-corefrom.upstream/at that commit. The package and the Node.js binaries ship as jar resources.- Resolution (
Driver.newInstance):playwright.cli.dir/PLAYWRIGHT_DRIVER_DIRset → use that directory as is. Otherwise the class named byplaywright.driver.impl(defaultDriverJar) extracts the resources into aplaywright-java-*temp dir (underplaywright.driver.tmpdir), copes with nested Spring Boot jars and sets executable bits. Node.js comes fromPLAYWRIGHT_NODEJS_PATH/playwright.nodejs.pathor the bundle. - One install per JVM (
Driver.ensureDriverInstalled): the firstPlaywright.create()runsnode cli.js installunlessPLAYWRIGHT_SKIP_BROWSER_DOWNLOADorSELENIUM_REMOTE_URLis set.PlaywrightImpl.createImpl(options, true)forces a fresh driver process for tests. - Process:
<node> package/cli.js run-driver; env = JVM env +CreateOptions.env+PW_LANG_NAME=java,PW_LANG_NAME_VERSION=<java major>,PW_CLI_DISPLAY_VERSION=<jar version>. stdin/stdout are the pipe, stderr is inherited.Playwright.close()closes stdin and waits up to 30 s. CLI.mainpasses every subcommand tocli.js(codegen,install,show-trace, ...); onlyinstall-driveris handled in Java.DEBUG=pw:install|pw:channel|pw:apilogs like upstream;PLAYWRIGHT_JAVA_SRCenablesStackTraceCollectorso traces and the inspector show Java source.
The schema is upstream packages/protocol/spec/*.yml; the generated channels.d.ts and validator.ts are the fastest way to look up a command. Java uses classes generated from the same spec: scripts/generate_java_channels.js writes com.microsoft.playwright.impl.protocol (one public class per file, upstream channels.d.ts names: <Name>Channel with <Result> method(<Params>, Double timeout), <Name><Method>Params/Result, <Name>Initializer, <Name><Event>Event, structs). Connection.protocolGson() maps a ChannelOwner to {guid} and byte[] to base64. Utils.toProtocol(options, Params.class) converts an options object; the overload with an owner resolves channel references. The package is excluded from the javadoc jar.
- Framing: 4-byte little-endian length + UTF-8 JSON, both directions, over the driver's stdin/stdout.
- Call:
{id: "abcd@17", guid, method, params, metadata: {timeout?, title?, internal?, location?}}. Other objects are referenced as{guid}.timeoutnever goes inparams, only inmetadata.timeout, and the server enforces it. The validator drops unknown keys. While tracing, the stack goes separately viaLocalUtils.addStackToTracingNoReply. - Reply:
{id, result}or{id, error: {error: {message, name, stack}}, errorDetails?, log?}.nameisTimeoutError,TargetClosedErrororError, mapped toTimeoutError,TargetClosedError,DriverException. - Event:
{guid, method, params}. Structural:__create__ {type, guid, initializer}(always before any reference to the guid);__adopt__ {guid}(a Page adoptsmainFrame; a BrowserContext adoptsdebugger,requestContext,tracing; an APIRequestContext adoptstracing);__dispose__ {reason?}("gc"= evicted; later calls throwThe object has been collected to prevent unbounded heap growth.). Client-only:__waitInfo__(wait steps in the trace) andupdateSubscription {event, enabled}for opt-in events. - Bootstrap: the synthetic
Root(guid"") sendsinitialize {sdkLanguage: "java", supportsLoneSurrogates: true}; the driver creates the threeBrowserTypes,Android,Electron,LocalUtils, thenPlaywright, and replies{playwright: {guid}}. Aconnect()connection bootstraps the same way and itsPlaywrightcarriespreLaunchedBrowser. - Values (
Serialization, upstreamserializers.ts):SerializedArgument {value, handles};SerializedValuesets one ofn b s v d u bi r e a o h id/ref m se(taandfnare unsupported). Results: whole numbers becomeInteger, othersDouble; objectsLinkedHashMap; arraysArrayList;nullandundefinedbothnull. Unsupported Java types throwPlaywrightException("Unsupported type of argument: ..."). - Options: one configured Gson instance: enums lowercased (
_to-),Optional.empty()is an explicit null ("no-override"),Map<String,String>becomes[{name, value}],Patha string, handles and locators their refs. A few parameters are massaged after serialization (recordVideo,viewport/noDefaultViewport,acceptDownloads, client certificates, HAR options). Binary data is base64.
Every server object has one Java ChannelOwner with the same guid, holding connection, parent, type, guid, initializer and its children. Connection owns the transport, the guid registry, pending callbacks, the id counter, the tracing counter, the current step title, env, isRemote and LocalUtils, and instantiates objects by type through a factory map as connection.ts does (unknown type → PlaywrightException("Unknown type ..."); SocksSupport, Android*, Electron are ignored).
The tree as the driver creates it:
Root > BrowserType x3, LocalUtils, Playwright (> APIRequestContext from newRequest)
BrowserType > Browser > BrowserContext > Page, Tracing, Debugger, APIRequestContext (adopted),
Request, Response, Route, WebSocketRoute, BindingCall,
Dialog, CDPSession, Artifact (downloads, videos)
Page > Frame (main frame adopted), Worker, WebSocket
Frame > JSHandle, ElementHandle
- Initializers are read lazily and may only reference guids created earlier.
- Two kinds of impl classes: channel-backed (
PageImpl,FrameImpl,RequestImpl,TracingImpl,Stream, ...) and pure client objects without a guid (LocatorImpl,KeyboardImpl,MouseImpl,DownloadImplwrapping anArtifactImpl,ConsoleMessageImpl,SelectorsImpl,RouteHandler, the*AssertionsImpl, ...). - Same guid, same instance: tests compare
Frame,Page,RequestandBrowserContextwith==andassertEquals. - State only the client holds: listener maps and
updateSubscription;Page.frames,Frame.childFramesandloadStatesfed byframeAttached/frameDetached/navigated/loadstate; the route handler lists (RouteHandler,WebSocketRouteHandler,HarRouter); binding maps;TimeoutSettings; close reasons andisClosed/isDetached;Selectorsregistrations sent with everynewContext; tracing chunk and HAR bookkeeping;Browser.contextsandBrowserContext.pages.
Only PipeTransport has threads of its own:
API thread --serialize--> outgoing queue --> Writer thread --> driver stdin
API thread <--poll 10 ms-- incoming queue <-- Reader thread <-- driver stdout
- API thread: the caller. It serializes the message, enqueues it and pumps until its reply arrives.
- Reader thread: reads frames from stdout into a bounded
ArrayBlockingQueue(1000). Never interrupted on close, so the driver can flush and exit. A closed pipe surfaces asPlaywrightException("Failed to read message from driver, pipe closed."). - Writer thread: takes strings from a bounded queue, writes frames, flushes when the queue is empty.
close()closes stdin and interrupts only this thread.
No timer thread: timeouts are System.nanoTime deadlines polled by the loop. The loop is ChannelOwner.runUntil(code, waitable): run code, then while (!waitable.isDone()) connection.processOneMessage(), which polls for at most 10 ms and dispatches one message (a reply completes its WaitableResult; __create__/__adopt__/__dispose__ edit the tree; anything else is handleEvent).
Rules the harness relies on:
- Events are delivered only inside a Playwright call. State set by a handler is awaited with
page.waitForCondition(...);future.get()hangs. - Handlers are re-entrant: a route handler that calls
route.fulfill()runs a nested loop that may complete the outer call and deliver more events first. - Wire order is dispatch order, one message per iteration. Listener lists are copied before notification, so a handler may remove itself.
- An event wait is
WaitableRace(WaitableEvent, close helper → TargetClosedError, WaitableTimeout). The event subscribes before the callback runs, sowaitForPopup(() -> click())never misses. console,dialog,dialogClosed,request,response,requestFinished,requestFailedandfileChooserare opt-in:updateSubscriptionis sent asynchronously when the first listener is added. A dialog with no listener is dismissed client-side (accepted forbeforeunload).sendMessageAsync(route actions,updateSubscription,BindingCallreplies) does not pump;sendMessageNoReplyregisters no callback.- Driver errors are re-thrown on the API thread as a new exception of the same class, so the stack trace points at the caller.
isSafeCloseErrorfilters the "has been closed" family in close paths. - One
Playwrightper worker, not per thread: the harness creates fixtures on a fork-join thread and runs the test body on a separate timeout thread. Strictly sequential use from different threads must work: noThreadLocal, no thread affinity. connect()layers a secondConnectionover aJsonPipeof the first; the same thread pumps both.- A hung driver is caught by a
timeout + 10 sguard:TimeoutError("Timeout <n>ms exceeded while waiting for the driver to respond to <method>. The driver is not responding.").
scripts/generate_api.shrequires.upstream/at the pinned commit (orPW_SRC_DIR), runsscripts/generate_java_api.mts, which parsesdocs/src/api/*.mdwith the upstream parser, and thenscripts/generate_java_channels.jsfor the protocol types. The markdown docs are the source of truth; every Java-specific name comes fromlangs:/alias-java:annotations there.verify_api.ymlfails CI on any diff.- Generated: one interface per class in
com.microsoft.playwright(with nestedXxxOptions),assertions/*, every enum and object type inoptions/,ScreencastFrame. Hand-written:CLI,PlaywrightException,TimeoutError, thepackage-info.javafiles (@NullMarked),Annotate,BindingCallback,FunctionCallback,BrowserChannel. Generated code callsPlaywrightImpl.create,FormDataImpl,RequestOptionsImpl, the three*AssertionsImplandAssertionsTimeoutby name. - Shape rules: optional parameters produce
defaultoverloads from last to first; a union parameter produces one overload per member (String | Pattern | Predicate<String>); waits arewaitForX(Options, Runnable), there is nowaitForEvent; events areonX/offX(Consumer<T>); option fields are public and boxed,T|nullfields areOptional<T>, setters are fluent.Playwright,Browser,BrowserContextandPageareAutoCloseable. - The
@Nullableannotations are part of the compiled contract: NullAway consumers break without them.
Each client class is a line-by-line translation of one file of packages/playwright-core/src/client/: the same member order, the upstream names without the leading underscore, the same statements. A ported file starts with // Port of <file>.ts. and marks each Java-specific spot with // java: <why>. To find the port of a file: grep -rlE "^// Port of .*\bpage\.ts" playwright/src/main/java. The rules are in playwright/src/main/AGENTS.md.
What does not follow from the file names:
network.tsis one class per upstream class:RequestImpl,ResponseImpl,RouteImpl,WebSocketImpl,WebSocketRouteImpl,RawHeaders,RouteHandler,WebSocketRouteHandler.fetch.tsisAPIRequestImpl,APIRequestContextImpl,APIResponseImpl.connect.tsisConnect(staticconnectToBrowser,connectToEndpoint);JsonPipeis also the transport (JsonPipeTransportupstream).PlaywrightImplhas the Java-only driver start-up at the bottom.packages/protocol/src/serializers.tsis the first part ofSerialization(parseSerializedValue,serializeValue, ...); its Gson configuration and adapters below are Java-only.- The helpers of
packages/isomorphic/without client state are in the subpackageimpl.isomorphic(Headers,StringUtils,MimeType,Time,LocatorUtils).manualPromise.tsisLongStandingScopeandurlMatch.tsisUrlMatcher, both inimpl: the first is built onChannelOwnerandWaitable, the second has the driver convert globs (LocalUtils.globToRegex). packages/playwright/src/matchers/*.tsisAssertionsBase, the*AssertionsImplandAssertionsTimeout. They are not a line-by-line port.android.tsandelectron.tsare not ported.Utilsholds only helpers with no upstream counterpart (GsonconvertType/clone,toProtocol,writeToFile, regex flag conversion).
Deliberate divergences that the tests encode: callback-style waitForX; waitForCondition on page and context; no page.goto: prefix in error messages (tests that need it are @Fixme); locator actions that need an element resolve a handle, act and dispose it; selectors() is a per-Playwright client registry; close methods swallow safe close errors; enums go through Gson adapters.
TimeoutSettings: 30 000 ms for actions and navigations, 180 000 ms for launch and connect; a page chains to its context. Order: explicit option,setDefaultNavigationTimeout(navigation only),setDefaultTimeout, parent, constant.APIRequestContexthas its own instance. Timeout0waits forever.- Server-enforced timeouts travel in
metadata.timeoutand fail with the server's message (Timeout 30000ms exceeded.plus the call log). Client-side waits go throughWaiteras upstream (createForEvent,rejectOnEvent,rejectOnTimeout,waitForEvent,dispose); the timeout message names the event,Timeout <n>ms exceeded while waiting for event "<event>".connect()races the Playwright initialization against its timeout. Assertions default to 5 000 ms (setDefaultAssertionTimeout). - Waitables:
WaitableResult(a reply),WaitableEvent(a listener hit),WaitablePredicate(polled),WaitableTimeout,WaitableNever,WaitableRace; plus the page close helper (TargetClosedError), the frame detach helper (Navigating frame was detached!) and the frame navigation and load-state helpers. - Exceptions:
PlaywrightException(base) andTimeoutErrorare public;impl.TargetClosedErroris public with the default messageTarget page, context or browser has been closed;DriverExceptionwraps other server errors;ServerErrorWithDetailscarrieserrorDetailsforexpect; assertions throwAssertionFailedError. - A driver error message is
SerializedError.Error.toString()(Error {\n message='...'\n name='...'\n stack='...'\n}) followed by\nCall log:\n- .... Testscontains-check the upstream text insidemessage=. Quirk: log lines carry a doubled- -prefix.
assertThat(locator | page | response); .not() flips isNot. Locator and page matchers send one Frame.expect command with FrameExpectOptions {selector, expression, expressionArg, expectedText[], expectedNumber, expectedValue, useInnerText, isNot, timeout} under the title Assert "hasText", and the server polls. The expression ids and the ExpectedTextValue flags (normalizeWhiteSpace, ignoreCase, matchSubstring) are copied from upstream matchers.ts (to.have.text, to.be.visible, to.have.url, to.match.aria, ...). A failure becomes AssertionFailedError with <prefix> (Locator expected to have text, or expected not to), then \nExpected: ...\nReceived: ..., then \nCall log: when present, with expected and actual passed as ValueWrappers; the ported expect-matcher-result tests snapshot these strings. APIResponseAssertions.isOK() is client-side: Response status expected to be within [200..299] range, was <status> plus the call log from fetchLog and \nResponse text: for textual MIME types.
Each is a thin layer over a server feature, with sequencing the tests observe:
- Routing:
route()prepends aRouteHandlerto the list of the page or context and sendssetNetworkInterceptionPatterns; on arouteevent the page router runs before the context router, newest handler first, honouringtimesandfallback(); when nothing handles it,route.resume(). Globs are compiled byLocalUtils.globToRegex;unroutecomparesUrlMatchers. - HAR:
routeFromHARreplays throughLocalUtils.harOpen/harLookupor records throughTracingImpl(harStart/harExport). - WebSocket routing: the
WebSocketRouteHandlerlist plussetWebSocketInterceptionPatterns;WebSocketRouteImplmodels both ends and sendsensureOpenedwhen the handler did not connect to the server. - Bindings:
exposeBinding {name, needsHandle}; duplicates throwFunction "x" has been already registered;bindingCallevents are answered withresolve/rejectasynchronously. - Downloads, videos, uploads:
Artifactcommands (pathAfterFinished,saveAs,stream, ...); overconnect(),path()throws and content streams throughStream/WritableStream; uploads sendlocalPathslocally andcreateTempFilesplus streams remotely; payloads are size-checked. - Dialogs: may arrive before their page exists; auto-dismissed when nobody listens.
- Tracing:
tracingStart(option names rewritten),tracingStartChunk,LocalUtils.tracingStartedfor astacksId;Connection.tracingCountturns on stack shipping;tracingStopChunk {mode}thenLocalUtils.ziplocally or artifact save plus append remotely. - Selectors:
SelectorsImplkeeps engines andtestIdAttributeNameperPlaywrightand sends them with every context; locator builders only concatenateinternal:selector strings. - Clock, CDP, screencast, storage, credentials, debugger, WebMCP: thin command wrappers; the screencast acknowledges every frame with
screencastFrameAck.
com.microsoft.playwright.fixturesis a JUnit 5 port of upstreambrowserTest.ts:@PlaywrightTestruns once per browser inBROWSER, fixtures resolve by parameter type with@Fixture(scope = TEST | WORKER)overrides, andTestServeris a hand-written HTTP/1.1 and WebSocket server. The port rules are inplaywright/src/test/AGENTS.md.- Workers (
WorkerPool) each own onePlaywright; a worker serves one test at a time but not one thread (separate_threadtimeout mode). @Fixme("java: ..." | "driver: ..." | "harness: ...")is a strict expected failure: a test that starts passing fails withTest marked @Fixme passed, remove the annotation. Most marks are for the missingpage.goto:prefix and the JavaScript-styleLocator.toString().upstream_base.txtpins the ported upstream commit;upstream_not_ported.txtlists the exclusions, concurrent-call tests among them.
Internals the tests reach into:
| Symbol | Used by |
|---|---|
impl.PlaywrightImpl.createImpl(CreateOptions, boolean forceNewDriver) |
tests that need a fresh driver |
((PlaywrightImpl) pw).localUtils().globToRegex(glob, baseURL, webSocketUrl) |
interception tests |
impl.TargetClosedError and its default message |
13 files |
a Process field driverProcess on the class Playwright.create() returns |
TestUnresponsiveDriver |
a superclass ChannelOwner with a field guid |
a connect test |
impl.driver.Driver.ensureDriverInstalled, createAndInstall, driverDir(), createProcessBuilder(); impl.driver.jar.DriverJar with a no-arg constructor and installDriverTo(Path); the properties playwright.cli.dir, playwright.driver.tmpdir, playwright.nodejs.path, playwright.driver.impl; env PLAYWRIGHT_DOWNLOAD_HOST, PLAYWRIGHT_BROWSERS_PATH, PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD; the message Failed to create driver |
driver and server tests |
impl.junit.BrowserContextOptionsExtension.getContextOptions, PlaywrightExtension.getOrCreatePlaywright |
junit tests |
StackTraceCollector: exactly one src/<sha1>.java entry per trace; PLAYWRIGHT_JAVA_SRC read from CreateOptions.env before the process env |
tracing tests |
public option fields (LaunchOptions.headless/channel, CreateOptions.env, ConnectOptions.headers, BoundingBox, ViewportSize, Cookie.url) |
harness |
Exact strings: Locator@<selector>, JSHandle@<preview>, selectors.register: Selector engine name may only contain [a-zA-Z0-9_] characters, Connecting over CDP is only supported in Chromium and WebKit., Malformed endpoint. Did you use launchServer method?, Timeout 100ms exceeded while waiting for the driver to respond to waitForFunction. An exception thrown by a handler propagates out of the call that dispatched it.
Quirks that no test depends on: the doubled call-log prefix, metadata.wallTime (ignored by the server), __abort__ never sent, ta/fn values unsupported.