0
0
Selenium Pythontesting~15 mins

Cookie management in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify cookie management on example.com
Preconditions (2)
Step 1: Open browser and navigate to https://example.com
Step 2: Add a cookie with name 'test_cookie' and value 'cookie_value'
Step 3: Retrieve the cookie named 'test_cookie' and verify its value is 'cookie_value'
Step 4: Delete the cookie named 'test_cookie'
Step 5: Verify that the cookie named 'test_cookie' no longer exists
✅ Expected Result: The cookie 'test_cookie' is added, verified, deleted, and confirmed absent successfully.
Automation Requirements - Selenium with Python
Assertions Needed:
Assert that the cookie 'test_cookie' exists after adding
Assert that the cookie value is 'cookie_value'
Assert that the cookie 'test_cookie' does not exist after deletion
Best Practices:
Use explicit waits if needed before cookie operations
Use Selenium's cookie management API methods
Keep test steps clear and assertions precise
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import unittest

class TestCookieManagement(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome(service=Service())
        self.driver.get('https://example.com')

    def test_cookie_management(self):
        driver = self.driver

        # Add cookie
        cookie = {'name': 'test_cookie', 'value': 'cookie_value'}
        driver.add_cookie(cookie)

        # Retrieve cookie and verify value
        retrieved_cookie = driver.get_cookie('test_cookie')
        self.assertIsNotNone(retrieved_cookie, "Cookie 'test_cookie' should exist")
        self.assertEqual(retrieved_cookie['value'], 'cookie_value', "Cookie value should be 'cookie_value'")

        # Delete cookie
        driver.delete_cookie('test_cookie')

        # Verify cookie is deleted
        deleted_cookie = driver.get_cookie('test_cookie')
        self.assertIsNone(deleted_cookie, "Cookie 'test_cookie' should be deleted")

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

This test uses Python's unittest framework with Selenium WebDriver.

setUp: Opens Chrome browser and navigates to example.com.

test_cookie_management: Adds a cookie named 'test_cookie' with value 'cookie_value'. Then it retrieves the cookie and asserts it exists and has the correct value. Next, it deletes the cookie and asserts it no longer exists.

tearDown: Closes the browser after the test.

This structure keeps tests clean and easy to understand. Assertions check each step clearly.

Common Mistakes - 3 Pitfalls
Trying to add a cookie before navigating to any page
Using hardcoded waits instead of checking page load before cookie operations
Not verifying if the cookie exists before accessing its value
Bonus Challenge

Now add data-driven testing to add and verify cookies with 3 different name-value pairs.

Show Hint