Home / Modules / yoja-selenium

yoja-selenium

Browser-driven tests with a fluent TestBuilder: pick a browser, start an embedded server, run named JS exports as test steps, capture browser logs, integrate as JUnit 6 dynamic tests.

Installation

testImplementation 'com.easygoingapi:yoja-selenium:VERSION'

Requires a matching Selenium WebDriver on the test classpath (chromedriver, geckodriver, msedgedriver), either on PATH or via Selenium Manager.

Quick start

import com.easygoingapi.yoja.selenium.*;
import org.junit.jupiter.api.*;
import java.util.stream.Stream;

class TaskBoardTest {

    @TestFactory
    Stream<DynamicNode> scenarios() {
        return TestBuilder.builder()
            .browser(Browser.builder(Browser.CHROME)
                            .mode(Browser.Mode.HEADLESS)
                            .build())
            .startYojaWeb()
            .loadYwAssert()
            .testJsUnit("/jsUnitSyncTest.js",
                        List.of("titleIsTaskManager", "loginFormIsPresent"))
            .testAsyncModule("/jsUnitAsyncTest.js")
            .stream();
    }
}

Browser

Browser enum

Supported browsers. Each value maps to a Selenium WebDriver implementation.

Values
ValueDescription
CHROME · FIREFOX · EDGESupported browsers; require the matching WebDriver on PATH or via Selenium Manager
API
SignatureReturnsDescription
getWebDriverClass()Class<WebDriver>Selenium driver class for this browser
static builder(Browser)Browser.BuilderStart a Config builder
Browser.Mode
ValueDescription
HEADLESSNo visible window
HEADFULVisible window
DEBUGGERPins all timeouts to 1 hour and opens devtools
Browser.Config (Builder)
SignatureReturnsDescription
Builder.mode(Mode)Builder
Builder.timeout(Duration)BuilderDefault wait timeout for element lookups
Builder.build()Browser.Config
getBrowser()Browser
getMode()Mode
getTimeout()Duration

SeleniumService

SeleniumService class (AutoCloseable)

High-level wrapper around a WebDriver, tailored to driving Yoja-Web pages.

Factory
SignatureReturnsDescription
static newInstance(Browser.Config)SeleniumServiceLaunch the browser with the right options
webDriver()WebDriverUnderlying Selenium driver
javascriptExecutor()JavascriptExecutor
Navigation
SignatureReturnsDescription
getHttpPage(HttpUrl | String)voidNavigate the browser to the given URL
getHttpPage(Duration, HttpUrl | String)voidNavigate with an explicit wait timeout
reload()voidReload the current page
reload(ScriptOption)voidReload and re-inject helpers
await(Duration)voidPause execution for the given duration
resizeWindow(int w, int h)voidSet the browser window dimensions
Script execution
SignatureReturnsDescription
executeScript(String | Path [, args])ObjectExecute synchronous JS
executeScript(Duration, String | Path, args)ObjectWith an explicit timeout
executeAsyncScript(String | Path [, args])ObjectExecute async JS (must call the injected done callback)
repeatScript(Duration until, String, args)ObjectRe-execute until a truthy value is returned or the timeout elapses
repeatAsyncScript(Duration, String, args)ObjectAsync variant of repeatScript
DOM queries
SignatureReturnsDescription
firstTag(css)WebElementFirst matching element (waits for presence)
firstTag(Duration, css)WebElementWith an explicit wait timeout
firstTagFrom(WebElement, css)WebElementScoped to a given element
findTags(css)List<WebElement>All matching elements
findTagsFrom(WebElement, css)List<WebElement>Scoped to a given element
Storage accessors
SignatureReturnsDescription
localStorage(key)StringRaw value from window.localStorage
localStorage(key, Storage)StringStorage.date / type / value — extract a specific field from the stored JSON
sessionStorage(key [, Storage])StringSame for window.sessionStorage
Logs & helpers
SignatureReturnsDescription
logs()List<Log>All browser-side log entries collected so far
clearLogs()voidDiscard captured log entries
saveLogs()voidPersist logs to disk
printLogs()voidPrint logs to stdout
loadJavascript(String)voidInject a JS snippet into the current page
loadYwAssert()voidInject the ywAssert.js assertion helper
debugger()voidIDE breakpoint hook
close()voidQuit the WebDriver

TestBuilder

TestBuilder class

Fluent orchestrator. Builds a list of test steps, then runs them via execute() or exposes them as JUnit dynamic nodes via stream().

Factory
SignatureReturnsDescription
static builder()TestBuilder
Configuration
SignatureReturnsDescription
browser(Browser.Config)TestBuilderSelect browser and mode
host(String)TestBuilderOverride embedded server host
contentType(String ext, String mime)TestBuilderExtension → MIME mapping for static serving
webResource(String packageName [, contextPath [, url]])TestBuilderServe resources from a classpath package
webResource(WebApp, String url)TestBuilderServe resources from a WebApp
webResource(WebResource)TestBuilderServe a pre-built WebResource
webService(WebService)TestBuilderAdd a service endpoint to the embedded server
webSocket(WebSocket)TestBuilderAdd a WebSocket endpoint to the embedded server
Bootstrap steps
SignatureReturnsDescription
startJavascript([ScriptOption])TestBuilderStep: serve a minimal HTML fixture and navigate to it
startYojaWeb([ScriptOption])TestBuilderStep: serve a full Yoja-Web fixture page and navigate to it
getPage(String path)TestBuilderStep: navigate to the given path on the embedded server
Test steps
SignatureReturnsDescription
test(String name, Consumer<TestContext>)TestBuilderAdd a custom named step
testJsUnit(String path, List<String> functions, args...)TestBuilderRun named JS exports as individual test steps
testModule(String path, args...)TestBuilderRun a synchronous JS module as a test step
testAsyncModule([Duration,] String path, args...)TestBuilderRun an async JS module; optional timeout
loadModule(String path)TestBuilderStep: load a JS module without asserting on its result
repeatTestModuleUntil(Duration, String path, args...)TestBuilderStep: retry the module until it returns truthy or the timeout elapses
Utility steps
SignatureReturnsDescription
reload([ScriptOption])TestBuilderStep: reload the current page
resizeWindow(int w, int h)TestBuilderStep: resize the browser window
await(Duration)TestBuilderStep: pause execution
loadYwAssert()TestBuilderStep: inject ywAssert.js
saveLogs()TestBuilderStep: persist browser logs to disk
printLogs()TestBuilderStep: print browser logs to stdout
debugger()TestBuilderStep: IDE breakpoint hook
Run
SignatureReturnsDescription
execute([boolean keepRunning])voidRun all steps sequentially; optionally keep the browser open after completion
stream()Stream<DynamicNode>Expose steps as JUnit 6 @TestFactory dynamic nodes

ScriptOption class

Controls which helpers are injected after page navigation.

SignatureReturnsDescription
static apply()ScriptOptionFresh option with all flags off
saveLogs()ScriptOptionInstall the ywLogger.js log capture helper
loadYwAssert()ScriptOptionInstall the ywAssert.js assertion helper

Test context

TestContext class (AutoCloseable)

Per-test handle injected into every Consumer<TestContext> step. Closing it shuts down both the embedded server and the WebDriver.

SignatureReturnsDescription
browser()BrowserActive browser enum value
seleniumService()SeleniumServiceThe wrapped WebDriver instance
httpServerContext()HttpServerContextEmbedded HTTP server details
httpUrlBuilder()HttpUrlBuilderURL builder pre-configured for the embedded server
logs()List<Log>Browser-side log entries
getHttpPage(HttpUrl | String [, Duration])voidNavigate the browser; optional explicit timeout
close()voidStop the embedded server and quit the WebDriver

HttpServerContext · HttpUrlBuilder classes

Embedded Yoja HTTP server serving test fixtures. Port auto-assigned from a monotonically-increasing sequence starting at 8888.

HttpServerContext
SignatureReturnsDescription
host()StringEmbedded server hostname
port()intAuto-assigned port
httpServerState()StateServer lifecycle state
httpUrlBuilder()HttpUrlBuilderURL builder pre-wired for this server
close()voidStop the embedded server
HttpUrlBuilder
SignatureReturnsDescription
path(String | Path)HttpUrlBuilder
query(String | HttpParameter)HttpUrlBuilderQuery string
fragment(String)HttpUrlBuilderURL hash fragment
build()HttpUrlBuild the full URL pointing at the embedded server

Logs

Log · Log.Level classes

Browser-side log entry collected by ywLogger.js and surfaced via SeleniumService.logs().

Log accessors
SignatureReturnsDescription
date()InstantTime the log entry was captured
level()Level
message()String
Log.Level values
ValueDescription
LOG · TRACE · DEBUG · INFO · WRAN · ERRORMirror the browser-side console levels; WRAN is intentional (matches the JS side)

DateUtil · Debugger · SeleniumException helpers

ClassKey APIDescription
DateUtilformatTimestamp(Long | Instant)Format a timestamp as yyyy-MM-dd HH:mm:ss in the JVM timezone
Debuggerdebugger()IDE breakpoint hook — throws and immediately catches a Debugger exception
SeleniumExceptionRuntime exception for WebDriver, script and embedded-server failures

Module README: yoja-selenium/README.md.