</>

Technology

Cucumber

Difficulty

Advanced

Interview Question

What is the Cucumber World object and how is it different from PicoContainer?

Answer

Cucumber World Object

What Is the World Object?

In Ruby and JavaScript Cucumber, the World is a special shared object that is automatically available in all step definition files for a single scenario. It resets between scenarios.

Ruby example:

RUBY
# World is implicitly available
Given("the user is logged in") do
  @driver = Selenium::WebDriver.for :chrome   # World variable
end

When("the user clicks checkout") do
  @driver.find_element(id: "checkout").click  # same World variable
end

Java Equivalent: PicoContainer ScenarioContext

Java Cucumber does not have a built-in World object, but the same concept is achieved with PicoContainer dependency injection:

Java
// ScenarioContext = Java's equivalent of World
public class ScenarioContext {
    public WebDriver driver;
    public String lastApiResponse;
    public Map<String, Object> testData = new HashMap<>();

    // Lifecycle: created fresh for each scenario by PicoContainer
}
Java
// ALL step classes share the same ScenarioContext instance
public class LoginSteps {
    private final ScenarioContext world;  // "world" in Ruby terms
    public LoginSteps(ScenarioContext world) { this.world = world; }
}

public class CheckoutSteps {
    private final ScenarioContext world;
    public CheckoutSteps(ScenarioContext world) { this.world = world; }
}

Comparison: World vs PicoContainer

Ruby WorldJava PicoContainer
Automatic availabilityYes (implicit)No (constructor injection)
Configuration neededNoneAdd cucumber-picocontainer dep
Reset per scenarioYesYes (new instance per scenario)
Thread safetyPer-scenario isolationPer-scenario isolation
Custom data storage@variablectx.variable or ctx.testData.put()

Generic Context Store Pattern

Java
// Flexible World-like context with Map storage
public class ScenarioContext {
    public WebDriver driver;
    private Map<String, Object> scenarioData = new HashMap<>();

    public void set(String key, Object value) {
        scenarioData.put(key, value);
    }

    @SuppressWarnings("unchecked")
    public <T> T get(String key) {
        return (T) scenarioData.get(key);
    }

    public boolean has(String key) {
        return scenarioData.containsKey(key);
    }
}

// Usage
@When("an order is created")
public void createOrder() {
    String orderId = orderPage.placeOrder();
    ctx.set("orderId", orderId);   // store dynamically
}

@Then("the order confirmation should show the order ID")
public void verifyOrderId() {
    String orderId = ctx.get("orderId");   // retrieve
    assertTrue(confirmationPage.getOrderId().equals(orderId));
}

When to Use World/ScenarioContext

  • Sharing driver between step definition classes
  • Storing API tokens for use in subsequent steps
  • Passing created entity IDs from When to Then
  • Storing test run metadata (browser, env, user)
  • Accumulating SoftAssert instances

Follow AutomateQA

Related Topics