Cucumber Selenium数据表格登录场景异常问题求助
Let's break down what's happening here and how to fix it:
1. The Root Cause
Your Scenario Outline treats each row in Examples as a separate, isolated test case. But if your test is reusing the same browser session between these cases, the first user's login state (cookies, session storage) is still active when the second test runs. That's why navigating to the login page redirects straight to User_1's home page—your browser is still logged in as User_1, causing the assertion failure.
2. Solution 1: Isolate Browser Sessions for Each Test
Ensure every test case starts with a fresh browser session to avoid state leakage. Use Cucumber's @Before and @After hooks to reset the browser before each scenario:
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import io.cucumber.java.After; import io.cucumber.java.Before; public class TestHooks { private WebDriver driver; @Before public void setUpFreshSession() { // Initialize a brand new browser instance for each test driver = new ChromeDriver(); // Optional: If you must reuse a driver, clear all leftover state // driver.manage().deleteAllCookies(); // driver.executeScript("window.localStorage.clear();"); // driver.executeScript("window.sessionStorage.clear();"); } @After public void tearDownSession() { if (driver != null) { driver.quit(); // Close the browser entirely to wipe state } } }
3. Solution 2: Restructure Your Test for Sequential Flow
Your test isn't two independent login cases—it's a sequential flow: first user logs in and stays active, then a second user tries to log in concurrently. A Scenario Outline isn't the right fit here. Instead, write a single Scenario that explicitly handles both steps with separate sessions:
Scenario: Enforce concurrent login limit Given the user is on Login Page When the user enters "User_1" and "passw123" and submits login Then the user is redirected to Home Page (User_1's dashboard) And the browser session remains active When a new browser session is opened and navigates to Login Page And the user enters "User_2" and "passw123" and submits login Then an error message "Login limit exceeded" is displayed And the user remains on Login Page
In your step definitions, implement the "new browser session" step with a second WebDriver instance:
private WebDriver user1Driver; private WebDriver user2Driver; @When("a new browser session is opened and navigates to Login Page") public void openNewBrowserSession() { user2Driver = new ChromeDriver(); user2Driver.get("your-login-page-url"); } // Clean up both sessions after the test @After public void tearDownAllSessions() { if (user1Driver != null) user1Driver.quit(); if (user2Driver != null) user2Driver.quit(); }
4. Double-Check Session Persistence
Make sure your first user's session isn't being terminated accidentally. If you were using the Scenario Outline approach, check if your @After hook was closing the browser after the first test case—this would break the "keep logged in" requirement for your flow.
内容的提问来源于stack exchange,提问作者N.Hol

