Geb+Spock多登录尝试测试:Cookie保留与用例优化咨询
Great question—let's break this down into two key problems you're facing, and walk through practical solutions that fit your constraints:
1. Disable Auto-Cookie-Clearing Only for This Test
Since your global autoClearCookies=true is non-negotiable for other tests, you can override this setting just for this specific test spec using Spock's setup() and cleanup() hooks. These run before/after every test iteration, so you'll preserve the session between where: block runs without messing up other tests.
Here's how to implement it:
class LoginAttemptSpec extends GebSpec { // Turn off cookie clearing before each test iteration def setup() { browser.config.autoClearCookies = false } // Put the global setting back after each iteration def cleanup() { browser.config.autoClearCookies = true } // Your test logic goes here... }
Quick Heads-Up:
- If this spec has other tests that do need fresh cookies every time, move this login attempt test to its own separate spec file. That way, the override only applies where you need it.
setup()runs before every test method (and everywhere:iteration), whilecleanup()resets things afterward—perfect for keeping your global config intact.
2. Cut Down on Redundant given: Steps
The where: block re-runs your entire given-when-then flow for every iteration, which means you're re-entering the ID/phone number and navigating to the validation page 4 times—total waste of time. Let's fix that with two better approaches:
Option A: Run All Attempts in One Test (No where: Needed)
Instead of looping via where:, execute all 4 failed attempts sequentially in a single test. This way, you only run the initial setup once, and the session stays intact naturally:
def "check error messages across 4 sequential failed login attempts"() { given: "I get to the validation page with valid ID and phone" to LoginPage id = "valid-test-id" phoneNumber = "valid-test-phone" submit() at ValidationLoginPage when: "I submit wrong passwords 4 times in a row" 4.times { attempt -> password = "bad-code" submit() then: "the right error state shows up" if (attempt < 3) { // First 3 attempts errorMessage.text() == "Wrong password. Try again." passwordInput.displayed } else { // 4th attempt locks things down errorMessage.text() == "No more attempts" !passwordInput.displayed } } }
This is my top recommendation—it's concise, efficient, and you don't even need to mess with cookie settings because the whole test runs in one continuous browser session.
Option B: Use @Stepwise for Clearer Test Reporting
If you want each attempt to show up as a separate test in your reports, use Spock's @Stepwise annotation. This runs your spec's feature methods in order, keeping the session alive between them:
@Stepwise class LoginAttemptStepwiseSpec extends GebSpec { def setupSpec() { // Disable cookie clearing for the entire spec browser.config.autoClearCookies = false } def cleanupSpec() { // Restore the global config once all tests finish browser.config.autoClearCookies = true } def "navigate to validation page with valid credentials"() { given: "I'm on the login page" to LoginPage id = "valid-test-id" phoneNumber = "valid-test-phone" submit() then: "I land on the validation page" at ValidationLoginPage } def "first failed attempt shows retry prompt"() { when: "I enter a wrong password" password = "bad-code" submit() then: "retry message and input are visible" errorMessage.text() == "Wrong password. Try again." passwordInput.displayed } def "second failed attempt shows retry prompt"() { when: "I enter another wrong password" password = "bad-code" submit() then: "retry message and input are still visible" errorMessage.text() == "Wrong password. Try again." passwordInput.displayed } def "third failed attempt shows retry prompt"() { when: "I enter a wrong password a third time" password = "bad-code" submit() then: "retry message and input remain visible" errorMessage.text() == "Wrong password. Try again." passwordInput.displayed } def "fourth failed attempt blocks further input"() { when: "I enter a wrong password a fourth time" password = "bad-code" submit() then: "lockout message appears and input is hidden" errorMessage.text() == "No more attempts" !passwordInput.displayed } }
@Stepwise guarantees tests run in the order you write them, and the setupSpec()/cleanupSpec() hooks ensure your global cookie settings are only overridden for this spec.
Final Call
Stick with Option A if you want a clean, fast test that gets the job done with minimal code. If you need granular test reporting for each attempt, go with Option B. Either way, you'll avoid the cookie-clearing issue and cut down on redundant steps.
内容的提问来源于stack exchange,提问作者albciff

