Skip to main content

Driver factories

Kolibrium provides a DriverFactory typealias for creating WebDriver instances. Additionally, several predefined driver factories are available for common browser configurations.

DriverFactory typealias

The DriverFactory typealias represents a function that creates a new WebDriver instance.

Definition

import dev.kolibrium.selenium.DriverFactory

typealias DriverFactory = () -> WebDriver

A DriverFactory is simply a function that takes no parameters and returns a WebDriver instance.

Usage

Defining a factory does not create a driver or launch a browser. The lambda body is deferred — it only executes when the factory is invoked:

// This stores a function. No browser launches yet.
val myFactory: DriverFactory = { ChromeDriver() }

// This calls the function, which runs ChromeDriver() and opens the browser.
val driver = myFactory()

Because DriverFactory is a function type (() -> WebDriver), you can also use a constructor reference instead of a lambda:

val myFactory: DriverFactory = ::ChromeDriver

::ChromeDriver is Kotlin shorthand for { ChromeDriver() } — both produce a () -> WebDriver that calls the no-arg ChromeDriver constructor when invoked. The constructor reference form is more concise but only works when you don't need to pass arguments or configure options. For custom configurations, use the lambda form:

// Constructor reference — no customization possible
val simpleFactory: DriverFactory = ::ChromeDriver

// Lambda — full control over driver creation
val customFactory: DriverFactory = {
val options = ChromeOptions().apply {
addArguments("--start-maximized")
}
ChromeDriver(options)
}

Predefined driver factories

Kolibrium provides six predefined driver factories for common browser configurations. These factories use Kolibrium's DSL builder functions to create drivers with specific options.

Chrome factories

headlessChrome

Creates a Chrome driver and launches the browser in headless mode — the browser runs without a visible window. Useful for CI environments where no display is available.

import dev.kolibrium.selenium.headlessChrome

val driver = headlessChrome()

incognitoChrome

Creates a Chrome driver and opens the browser in incognito mode. Useful for tests that require a clean browser state without cached data.

import dev.kolibrium.selenium.incognitoChrome

val driver = incognitoChrome()

Firefox factories

headlessFirefox

Creates a Firefox driver and launches the browser in headless mode — the browser runs without a visible window. Useful for CI environments where no display is available.

import dev.kolibrium.selenium.headlessFirefox

val driver = headlessFirefox()

incognitoFirefox

Creates a Firefox driver and opens the browser in private mode. Useful for tests that require a clean browser state.

import dev.kolibrium.selenium.incognitoFirefox

val driver = incognitoFirefox()

Edge factories

headlessEdge

Creates an Edge driver and launches the browser in headless mode — the browser runs without a visible window. Useful for CI environments where no display is available.

import dev.kolibrium.selenium.headlessEdge

val driver = headlessEdge()

inPrivateEdge

Creates an Edge driver and opens the browser in InPrivate mode. Useful for tests that require a clean browser state.

import dev.kolibrium.selenium.inPrivateEdge

val driver = inPrivateEdge()

Creating custom driver factories

You can create your own driver factories for custom browser configurations:

Using Selenium's driver constructors

val customChromeFactory: DriverFactory = {
val options = ChromeOptions().apply {
addArguments("--start-maximized")
addArguments("--disable-extensions")
}
ChromeDriver(options)
}

val driver = customChromeFactory()

Using Kolibrium's DSL builders

Kolibrium provides DSL builder functions for creating drivers with a more fluent syntax:

val customFirefoxFactory: DriverFactory = {
firefoxDriver {
options {
arguments {
+headless
+incognito
windowSize {
width = 1800
height = 1000
}
}
}
}
}

val driver = customFirefoxFactory()

Using factories with different browsers

You can easily switch between browsers by changing the factory:

val driverFactory: DriverFactory = headlessChrome // Change to headlessFirefox, headlessEdge, etc.

val driver = driverFactory()

Notes

  • All predefined factories use Kolibrium's DSL builder functions internally
  • Headless mode is recommended for CI/CD environments to avoid display requirements
  • Incognito/private modes ensure tests start with a clean browser state
  • Custom factories can combine multiple options and arguments as needed
  • The DriverFactory typealias makes it easy to pass driver creation logic around