Answer
Handling Login Across 200 Scenarios
Problem
Gherkin
-- Repeated in 160 feature files = maintenance nightmare
Background:
Given the user opens the browser
And navigates to the login page
And enters username "admin@test.com"
And enters password "Admin@123"
And clicks Login
And waits for dashboard to load
This adds ~10-30 seconds to every scenario. For 160 scenarios = 40-80 minutes of wasted login time.
Solution 1: API Login Hook (Best — Fastest)
Java
// Hooks.java
@Before("@needs-login")
public void loginViaApi() {
// 1. Get session token via REST API (< 200ms)
Response resp = given()
.body(Map.of("email", "admin@test.com", "password", "Admin@123"))
.post(ConfigReader.getApiUrl() + "/auth/login");
String sessionToken = resp.jsonPath().getString("token");
String sessionCookie = resp.jsonPath().getString("sessionId");
// 2. Open browser to the domain (needed to set cookies)
ctx.driver.get(ConfigReader.getBaseUrl());
// 3. Inject session cookie — browser now thinks user is logged in
ctx.driver.manage().addCookie(
new Cookie("SESSION", sessionCookie,
".example.com", "/", null, false, true));
// 4. Navigate directly to the page under test
// No login page visited — user is already authenticated
}
@Before(order = 50) // runs before @needs-login hook
public void setUp() {
ctx.driver = DriverFactory.createDriver("chrome", false);
ctx.driver.manage().window().maximize();
}
Feature Files — Tag Scenarios, Not Background
Gherkin
@regression @needs-login
Feature: Product Catalog
-- No Background needed for login!
Scenario: Search for laptop
Given the user is on the product catalog page
When the user searches for "laptop"
Then the search results should appear
Scenario: Filter by category
Given the user is on the product catalog page
When the user filters by "Electronics"
Then only electronics products should show
@regression @no-login
Feature: Public Landing Page
Scenario: View hero banner
Given the user navigates to the home page
Then the hero banner should be visible
Solution 2: Shared Login State (UI Login Once per Suite)
Java
// For cases where API login injection isn''t possible
public class LoginState {
private static volatile boolean isLoggedIn = false;
private static final Object lock = new Object();
}
@Before("@needs-login")
public synchronized void ensureLoggedIn() {
if (!LoginState.isLoggedIn) {
ctx.loginPage.login("admin@test.com", "Admin@123");
LoginState.isLoggedIn = true;
} else {
// Reuse cookies from previous session
ctx.driver.manage().addCookie(savedCookie);
}
}
Warning: This breaks parallel execution. Solution 1 is safer.
Solution 3: Common Step in Background (Acceptable)
Gherkin
-- One-liner Background using a high-level step
Background:
Given the user is logged in as "admin"
-- Step definition does API login + cookie injection
@Given("the user is logged in as {string}")
public void loginAs(String role) {
cookieManager.injectSessionFor(role); // encapsulated in utility
}
Performance Comparison
| Approach | Time per scenario | 160 scenarios total |
|---|---|---|
| UI login in Background | 15-30 sec | 40-80 min |
| API login + cookie | 0.5-1 sec | 1.5-3 min |
| Shared session (serial) | 0.1 sec | 16 sec + 1 UI login |
