Overview
Drive a real browser from your TestBox specs with bx-playwright.
TestBox can drive a real browser (Chromium, Firefox or WebKit) from your specs through the bx-playwright module. Your specs visit pages, click, fill forms and assert on what a user actually sees, with the same describe(), it() and expect() you already write.
Why Browser Tests?
Unit and integration specs call your code directly. A browser test exercises the whole stack the way a user does: routing, templates, JavaScript, CSS and the network all take part. bx-playwright brings two things that make those tests reliable:
A real browser. Pages render and run their JavaScript exactly as they do for your users, headless in CI or headed while you debug.
Web-first assertions. Every assertion retries until it passes or its timeout expires (5 seconds by default), so you never sprinkle
sleep()calls to wait for an element or a redirect.
TestBox adds the testing glue on top: one browser per bundle, fresh isolated pages per spec, expect() matchers for pages and locators, failure screenshots, traces and videos attached to the spec, retries for flaky specs, and runner options to start your web server and rerun only what failed.

And when a spec fails, you get the evidence on the failing spec in your report:

Installation
bx-playwright needs BoxLang 1.17+ and Java 21+. TestBox needs a bx-playwright version that provides playwrightEnsureBrowser(), the first stable bx-playwright release or later. Install the module into your BoxLang runtime:
That is all you need locally: the first time a bundle opens a browser, TestBox installs the browser of its profile through playwrightEnsureBrowser(). The first run needs network access and is slower while the browser downloads. On Linux, the system libraries the browser needs are not part of that download: install them once with bxplaywright install chromium --with-deps (or your profile's browser).
For CI or offline machines, install the browser up front and turn the download off with @browserAutoInstall( false ), or call ensureBrowserInstalled() yourself, for example from beforeAll():
More browsers, profiles and settings are covered in the bx-playwright getting started guide.
Your First Browser Spec
Add a browser annotation, such as @browser, @browserProfile or @baseURL, on the lines above class, and extend testbox.system.BaseSpec as usual. TestBox then turns on browser support for the bundle. Inside a spec, call browse() with a closure: it receives a fresh page, and everything the page offers (visit(), click(), fill(), finders, locators and assertions) is the bx-playwright browsing API.
Run it like any other bundle, for example with the BoxLang CLI runner:
You can assert in two styles, and mix them freely:
TestBox browser matchers:
expect( page ).toSee( "Welcome" ),expect( locator ).toBeVisible(), withnotforms.bx-playwright assertions:
page.assertSee( "Welcome" ),page.expect( "h1" ).toHaveText( "Hi" ). TheirPlaywright.AssertionFailedexceptions count as spec failures, not errors.
Turning On Browser Support
Browser support is turned on by class annotations, not by a base class. Any spec whose class, or a class it extends, has one of these annotations gets it:
@browser
Turns browser support on with the bx-playwright default profile.
@browserProfile( "ci" )
Turns it on with the given bx-playwright profiles, a list such as ci,mobile.
@baseURL( "http://localhost:8080" )
Turns it on with the URL relative visits resolve against.
@browserAutoInstall( false ) requires a pre-installed browser instead of downloading one on first use. It does not turn browser support on by itself.
When a bundle has browser support, the TestBox runner creates a testbox.system.browser.BrowserSupport for it and mixes these methods into the spec's this and variables scopes, so you call them unqualified:
browse( callback, options ): run a closure with fresh, isolated browser pages.browserAvailable(): can browser specs run here? See Skipping When No Browser Is Available.ensureBrowserInstalled(): install the profile browser, for example frombeforeAll()when auto-install is off.getBrowserSupport(): the bundleBrowserSupport.closeBrowser(): close the bundle browser.
It also adds this.playwright(), the bundle manager (described below), and registers the browser matchers for every spec of the bundle. A public method your spec declares itself, with one of these names, is kept, not overridden.
Inherited. The annotations are read from the spec class and every class it extends. Put
@browseron your project base spec and every spec that extends it can browse. A spec can opt out with@browser( false ), even when its base spec turns it on.Any base class. It works for BDD and xUnit bundles, whatever they extend:
testbox.system.BaseSpec, a ColdBoxBaseTestCaseor your own base spec.BoxLang only. The runner attaches browser support on BoxLang, where bx-playwright runs. On Lucee and Adobe the annotations are ignored, so keep browser specs out of CFML runs.
BrowserSpec
testbox.system.BrowserSpec is an optional base class: it is a testbox.system.BaseSpec that carries the @browser annotation and declares no methods of its own. The runner mixes in the browser methods and closes the browser after the bundle. Specs that extend it keep working unless they call super.browse() or super.closeBrowser(), or override closeBrowser() expecting it to run after the bundle.
How Browser Support Works
One browser per bundle. The bundle starts one bx-playwright manager, and its browser, the first time a spec uses it. The runner closes it after the bundle, even when your
afterAll()throws, so your ownbeforeAll()andafterAll()need nosupercalls. If a request aborts before the bundle ends, the browser stays open until the next test run that opens a browser, which closes it first. bx-playwright also closes anything left open when the module unloads or the JVM stops.Fresh pages per
browse(). Everybrowse()call gets new pages, each in its own browser context (its own cookies, storage and session), and closes them when the closure ends. Specs never leak state into each other.Matchers registered for you. The browser matchers are registered for every spec of the bundle.
Failure artifacts attached. When the closure throws, the screenshots, trace and videos your artifact policy kept are attached to the spec and the exception is rethrown unchanged.
Bundle Annotations
browser
Turns browser support on. Not needed when baseURL or browserProfile is set.
baseURL
The URL relative visits such as page.visit( "/login" ) resolve against.
browserProfile
The bx-playwright profiles for the bundle browser, a list such as ci,mobile. Profiles merge left to right.
browserAutoInstall
Set to false to require a pre-installed browser instead of downloading one on first use.
retries
Rerun failing specs of the bundle. See Retries.
When baseURL is not set, the bundle uses, in order:
The
--web-server-urlof the BoxLang runner, when the runner started your web server (kept inserver.testbox.webServerURL).The bx-playwright
baseURLsetting, or theBX_PLAYWRIGHT_BASEURLenvironment variable.
When browserProfile is not set, bx-playwright uses its defaultProfile setting, or the profile named in the BX_PLAYWRIGHT_PROFILE environment variable. That makes BX_PLAYWRIGHT_PROFILE=ci a handy switch for CI runs, while browserProfile pins a profile for one bundle.
browse()
callback
The closure to run. It receives one page per declared argument.
options
Context options for bx-playwright newContext(), for example { viewport : { width : 390, height : 844 } }, { timeouts : { assertion : 10000 } } or an artifacts policy.
browse() returns whatever the callback returns, so you can pull a value out of the page and assert on it afterwards:
One page per argument: multi-user flows
Declare one argument per user. Each argument gets its own page in its own browser context, so two users can be signed in at once without sharing cookies:
A callback that declares no arguments receives a single page as its first positional argument.
Context options
The options struct applies to every page of the call. For example, a phone sized viewport and a longer assertion timeout:
See the bx-playwright configuration reference for every option.
this.playwright()
this.playwright() returns the bundle's bx-playwright manager, created on first use with the browserProfile and baseURL annotations. Use it for anything beyond browse(), such as the API request client, saved sessions or a page you manage yourself. Everything it starts closes after the bundle.
Skipping When No Browser Is Available
On BoxLang, browse() and this.playwright() already skip the running spec when bx-playwright is missing. To skip whole suites up front, without even entering them, use browserAvailable() in a skip constraint:
Using the Browser From Any Spec
Browser support delegates to testbox.system.browser.BrowserSupport, which any BoxLang code can use directly, without the annotations. Close it yourself when you are done:
Thread Safety
A bx-playwright manager is not thread safe, and a bundle shares one. Do not use asyncAll on suites, or bundles, that browse. Run browser bundles sequentially.
Learn More
Browser MatchersAttachmentsRetriesRunning Browser TestsThe browser API itself is documented in the bx-playwright guides: Browsing, Assertions, Testing (artifacts, debugging, page objects) and Building on bx-playwright.
Last updated
Was this helpful?