TestContext is a plain Java class that acts as the shared state container for a single Cucumber scenario. It holds the WebDriver instance and is passed automatically between Hooks, step definition classes, and page objects via PicoContainer’s constructor injection.
Source
Purpose
Cucumber runs each scenario in isolation, but multiple step definition classes (and theHooks class) need to share the same browser session. TestContext solves this by providing a single object that PicoContainer creates once per scenario and injects wherever it is requested.
Fields
The active Selenium
WebDriver session for the current scenario. Set by Hooks.setUp() before any step runs, and nulled out implicitly when PicoContainer disposes the context after Hooks.tearDown() closes the browser.How PicoContainer injection works
PicoContainer automatically managesTestContext because it is listed in the glue packages (steps and hooks). Any class in those packages that declares a constructor accepting TestContext will receive the same instance for the duration of a scenario.
PicoContainer requires that injectable classes have a no-arg constructor or a single constructor that PicoContainer can satisfy.
TestContext has the implicit no-arg constructor generated by the Java compiler, which is exactly what PicoContainer needs to instantiate it first.Usage pattern in hooks
Hooks receives TestContext via its constructor and assigns the WebDriver to context.driver in @Before:
Extending TestContext
If you need to share additional state across step classes — for example, a test data object or a scenario name for screenshots — add public fields toTestContext: