Browser Testing
Drive a real browser against your running ColdBox application with BaseTestCase, the @browser annotation, TestBox browser support and bx-playwright. BoxLang only.
Integration tests with BaseTestCase simulate a request inside a virtual ColdBox application: they are fast and precise, but no HTML is ever parsed, no JavaScript runs and no cookie travels between requests. Browser tests close that gap. Add the @browser annotation to a coldbox.system.testing.BaseTestCase spec and it drives a real Chromium, Firefox or WebKit browser, through bx-playwright, against your running application, so you can test what your users actually see: forms, redirects, sessions, JavaScript widgets and full login flows.
@appMapping( "/root" )
@browser
@baseURL( "http://127.0.0.1:8080" )
class extends="coldbox.system.testing.BaseTestCase" {
function beforeAll() {
super.beforeAll()
// Log in once through the login page, reused by every browse() call with { session : "admin" }
this.playwright().session( "admin", ( page ) => {
visitRoute( page, "login" ).fill( "Email", "admin@example.com" ).fill( "Password", "secret" ).click( "Sign in" )
} )
}
function run() {
describe( "Users", () => {
it( "shows a user profile to a logged in admin", () => {
browse( ( page ) => {
visitRoute( page, "users.show", { id : 5 } )
assertRouteIs( page, "users.show" )
expect( page ).toSee( "User 5" )
}, { session : "admin" } )
} )
} )
}
}How It Works
There is no special base class for browser tests. A browser spec is a regular BaseTestCase spec with a browser annotation, so it still loads your ColdBox application virtually, exactly like an integration test. It uses that virtual application to know your routes, module entry points and settings. The browser itself talks to a real web server that serves your application.
TestBox provides the browser support, turned on by the
@browserannotation (see Turning On Browser Support):browse(), one shared browser per bundle, isolated pages, browser matchers such astoSee()andtoHaveTitle(), and screenshots and traces attached to failed specs.bx-playwright provides the browser and its fluent API:
page.visit(),fill(),click(), locators and retrying assertions.ColdBox adds what only the framework knows: named routes (
routeURL(),visitRoute(),assertRouteIs()), built intoBaseTestCase. For pages behind a login, bx-playwright saved sessions log in once through your real login page and reuse the session.
Turning On Browser Support
TestBox attaches browser support to any spec whose class, or a class it extends, has one of these annotations:
@browser
Turns browser support on with the bx-playwright default profile
@browserProfile( "ci" )
Turns it on with bx-playwright profiles for the bundle browser, a list such as ci,mobile
@baseURL( "http://127.0.0.1:8080" )
Turns it on with the URL relative visits resolve against
The annotations are inherited: put them on a base class of your own and every spec that extends it can browse. @browser alone is enough when the base URL comes from elsewhere (see Setup), but it reads well to keep it next to @baseURL or @browserProfile.
When a spec has one of them, the TestBox runner (BoxLang only) mixes into the spec's this and variables scopes, so you call them unqualified:
browse( callback, [options] ),browserAvailable(),ensureBrowserInstalled(),getBrowserSupport()andcloseBrowser()this.playwright(), the bx-playwright manager of the bundle (an unqualifiedplaywright()is still the bx-playwright BIF)The TestBox browser matchers, such as
toSee()andtoHaveTitle()
Methods the spec declares itself are kept. The runner closes the bundle browser after the bundle, even when afterAll() throws.
browse() works in any annotated spec, including BaseModelTest or BaseInterceptorTest specs. The ColdBox route helpers routeURL(), visitRoute() and assertRouteIs() live in BaseTestCase and build URLs from the loaded application, so they need a spec that loads ColdBox: a BaseTestCase with loadColdBox left at its default of true.
Integration Tests or Browser Tests?
Use both. They answer different questions.
BaseTestCase integration tests
BaseTestCase + @browser browser tests
Runs
Inside a virtual application, no web server
A real browser against your running server
Speed
Milliseconds per spec
Hundreds of milliseconds to seconds per spec
Sees
The request context: rc, prc, rendered content, handler results
The rendered page: text, visibility, URLs, form values
JavaScript, CSS, cookies
No
Yes
Best for
Handler logic, APIs, relocations, security rules
User journeys, forms, front-end behavior, smoke tests
A good suite has many integration tests and a smaller set of browser tests that cover the journeys that matter most.
The Guide
Install bx-playwright and a browser, the version requirements, and how to run your application for the browser
A complete spec, browse(), the class annotations and the browser matchers
routeURL(), visitRoute() and assertRouteIs(), including module routes
Logged-in tests with bx-playwright saved sessions
Screenshots and traces of failed specs, the trace viewer and the debug profile
A complete GitHub Actions workflow
Common errors and how to fix them
See Also
Last updated
Was this helpful?