0
0
Selenium Javatesting~10 mins

Cookie management in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a website, adds a cookie, verifies the cookie is set, deletes the cookie, and confirms it is removed.

Test Code - JUnit 5 with Selenium WebDriver
Selenium Java
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

public class CookieManagementTest {
    private WebDriver driver;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
    }

    @AfterEach
    public void tearDown() {
        driver.quit();
    }

    @Test
    public void testAddAndDeleteCookie() {
        driver.get("https://example.com");

        Cookie cookie = new Cookie.Builder("testCookie", "testValue")
                .domain("example.com")
                .path("/")
                .build();

        driver.manage().addCookie(cookie);

        Cookie retrievedCookie = driver.manage().getCookieNamed("testCookie");
        assertNotNull(retrievedCookie, "Cookie should be present after adding");
        assertEquals("testValue", retrievedCookie.getValue(), "Cookie value should match");

        driver.manage().deleteCookieNamed("testCookie");

        Cookie deletedCookie = driver.manage().getCookieNamed("testCookie");
        assertNull(deletedCookie, "Cookie should be deleted and not present");
    }
}
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open but no page loaded-PASS
2Navigates to https://example.comExample.com homepage is loaded in browser-PASS
3Creates a cookie named 'testCookie' with value 'testValue' for domain example.comCookie object created in test code-PASS
4Adds the cookie to the browser sessionBrowser now has cookie 'testCookie' set-PASS
5Retrieves cookie named 'testCookie' from browserCookie 'testCookie' is found in browser cookiesAssert cookie is not null and value equals 'testValue'PASS
6Deletes cookie named 'testCookie' from browserCookie 'testCookie' removed from browser cookies-PASS
7Attempts to retrieve cookie named 'testCookie' after deletionNo cookie named 'testCookie' foundAssert cookie is null (deleted)PASS
8Test ends and browser closesBrowser window closed-PASS
Failure Scenario
Failing Condition: Cookie 'testCookie' is not found after adding or still present after deletion
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after adding the cookie?
AThe cookie exists and its value matches 'testValue'
BThe cookie is deleted immediately
CThe browser navigates to a new page
DThe cookie domain is changed
Key Result
Always verify cookies after adding or deleting them by retrieving them explicitly to confirm the browser state matches expectations.