0
0
Selenium Pythontesting~10 mins

Cookie management in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test adds a cookie to the browser, verifies it exists, deletes it, and confirms it is removed.

Test Code - Selenium
Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time

# Setup WebDriver
service = Service()
driver = webdriver.Chrome(service=service)

try:
    # Open a simple page
    driver.get('https://www.example.com')

    # Add a cookie
    cookie = {'name': 'testcookie', 'value': 'cookievalue123'}
    driver.add_cookie(cookie)

    # Verify cookie is added
    cookies = driver.get_cookies()
    assert any(c['name'] == 'testcookie' and c['value'] == 'cookievalue123' for c in cookies), 'Cookie not found after adding'

    # Delete the cookie
    driver.delete_cookie('testcookie')

    # Verify cookie is deleted
    cookies_after_delete = driver.get_cookies()
    assert all(c['name'] != 'testcookie' for c in cookies_after_delete), 'Cookie still present after deletion'

finally:
    driver.quit()
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and WebDriver Chrome instance is createdBrowser window opens, ready to navigate-PASS
2Browser navigates to 'https://www.example.com'Example.com homepage is loaded in the browser-PASS
3Add cookie named 'testcookie' with value 'cookievalue123'Cookie is added to the browser session-PASS
4Retrieve all cookies and check if 'testcookie' with correct value existsCookies list includes the added cookieAssert cookie 'testcookie' with value 'cookievalue123' is presentPASS
5Delete cookie named 'testcookie'Cookie 'testcookie' is removed from browser session-PASS
6Retrieve all cookies and verify 'testcookie' is no longer presentCookies list does not include 'testcookie'Assert cookie 'testcookie' is absent after deletionPASS
7Close browser and end testBrowser window closes-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?
AThat the cookie is deleted immediately
BThat the page title changes
CThat the cookie named 'testcookie' with value 'cookievalue123' exists in the browser
DThat the browser navigates to a new page
Key Result
Always verify cookie presence after adding and confirm removal after deletion to ensure cookie management works correctly.