Skip to content

Cookie assertions

Cookie assertions are available via expect(driver) (inherited by ExpectDriver). They operate on driver.get_cookies() and related methods.

Presence

to_have_cookie(name)

Asserts that a cookie with name exists.

Selenium API: driver.get_cookie(name) is not None

Parameters:

  • name (str): Cookie name.

Example:

expect(driver).to_have_cookie("session_id")

Negation:

expect(driver).not_.to_have_cookie("deleted_cookie")

to_have_no_cookies()

Asserts that no cookies are set.

Selenium API: driver.get_cookies() == []

Example:

driver.delete_all_cookies()
expect(driver).to_have_no_cookies()

Value

Asserts that driver.get_cookie(name)["value"] equals value.

Selenium API: driver.get_cookie(name)["value"]

Parameters:

  • name (str): Cookie name.
  • value (str): Expected cookie value.

Example:

expect(driver).to_have_cookie_value("session_id", "abc123")

Asserts that value is a substring of the cookie's value.

Selenium API: value in driver.get_cookie(name)["value"]

Parameters:

  • name (str): Cookie name.
  • value (str): Expected substring.

Example:

expect(driver).to_have_cookie_value_contains("session_id", "abc")

Properties

Asserts that driver.get_cookie(name)["domain"] equals domain.

Selenium API: driver.get_cookie(name)["domain"]

Parameters:

  • name (str): Cookie name.
  • domain (str): Expected domain.

Example:

expect(driver).to_have_cookie_domain("session_id", "example.com")

Asserts that driver.get_cookie(name)["path"] equals path.

Selenium API: driver.get_cookie(name)["path"]

Parameters:

  • name (str): Cookie name.
  • path (str): Expected path.

Example:

expect(driver).to_have_cookie_path("session_id", "/")

Asserts that driver.get_cookie(name)["httpOnly"] is True.

Selenium API: driver.get_cookie(name)["httpOnly"]

Parameters:

  • name (str): Cookie name.

Example:

expect(driver).to_have_cookie_http_only("session_id")

Asserts that driver.get_cookie(name)["secure"] is True.

Selenium API: driver.get_cookie(name)["secure"]

Parameters:

  • name (str): Cookie name.

Example:

expect(driver).to_have_cookie_secure("session_id")

Asserts that driver.get_cookie(name)["sameSite"] equals same_site.

Selenium API: driver.get_cookie(name)["sameSite"]

Parameters:

  • name (str): Cookie name.
  • same_site (str): Expected SameSite value ("Strict", "Lax", or "None").

Example:

expect(driver).to_have_cookie_same_site("session_id", "Strict")

Count

Asserts that len(driver.get_cookies()) equals count.

Selenium API: len(driver.get_cookies())

Parameters:

  • count (int): Expected number of cookies.

Example:

expect(driver).to_have_cookie_count(3)

Asserts that len(driver.get_cookies()) > n.

Selenium API: len(driver.get_cookies()) > n

Parameters:

  • n (int): Minimum exclusive count.

Example:

expect(driver).to_have_cookie_count_greater_than(0)

Expiry

Asserts that driver.get_cookie(name)["expiry"] equals expiry.

Selenium API: driver.get_cookie(name)["expiry"]

Parameters:

  • name (str): Cookie name.
  • expiry (int): Expected expiry timestamp (Unix epoch seconds).

Example:

expect(driver).to_have_cookie_expiry("session_id", 1735689600)

Tips and common patterns

Verifying session cookies

# After login, verify the session cookie was set
expect(driver).to_have_cookie("session_id")
expect(driver).to_have_cookie_value("session_id", "abc123")

# Verify the session cookie is secure and HttpOnly
expect(driver).to_have_cookie_secure("session_id")
expect(driver).to_have_cookie_http_only("session_id")

# Verify SameSite attribute
expect(driver).to_have_cookie_same_site("session_id", "Lax")
# Verify exactly 3 cookies are set
expect(driver).to_have_cookie_count(3)

# Verify at least 1 cookie exists
expect(driver).to_have_cookie_count_greater_than(0)

# Verify no cookies are set (e.g., after clearing)
driver.delete_all_cookies()
expect(driver).to_have_no_cookies()
# Check the cookie domain
expect(driver).to_have_cookie_domain("session_id", ".example.com")

# Check the cookie path
expect(driver).to_have_cookie_path("session_id", "/")

Negation

# Cookie does NOT exist
expect(driver).not_.to_have_cookie("deleted_cookie")

# Cookie value is NOT "old_value"
expect(driver).not_.to_have_cookie_value("session_id", "old_value")

# Cookie is NOT secure
expect(driver).not_.to_have_cookie_secure("tracking_id")