Bird
Raised Fist0
Selenium Javatesting~20 mins

Cookie management in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Cookie Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Selenium Java code snippet?
Consider the following Selenium Java code that adds a cookie and then retrieves it. What will be printed?
Selenium Java
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class CookieTest {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.get("https://example.com");
        Cookie cookie = new Cookie("session_id", "abc123");
        driver.manage().addCookie(cookie);
        Cookie retrieved = driver.manage().getCookieNamed("session_id");
        System.out.println(retrieved.getValue());
        driver.quit();
    }
}
Asession_id
Bnull
CNoSuchElementException
Dabc123
Attempts:
2 left
💡 Hint
Think about what value is stored in the cookie and what getValue() returns.
❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies that a cookie named 'user' exists?
You want to assert that a cookie named 'user' is present in the browser using Selenium Java and JUnit. Which assertion is correct?
AassertEquals("user", driver.manage().getCookieNamed("user"));
BassertTrue(driver.manage().getCookieNamed("user").isPresent());
CassertNotNull(driver.manage().getCookieNamed("user"));
DassertFalse(driver.manage().getCookies().contains("user"));
Attempts:
2 left
💡 Hint
Check what getCookieNamed returns if the cookie does not exist.
🔧 Debug
advanced
2:00remaining
Why does this code throw a NullPointerException?
Examine the code below. It throws a NullPointerException at runtime. What is the cause?
Selenium Java
Cookie cookie = driver.manage().getCookieNamed("auth_token");
System.out.println(cookie.getValue());
AThe cookie named 'auth_token' does not exist, so getCookieNamed returns null, causing getValue() to throw NullPointerException.
BgetValue() method is deprecated and throws NullPointerException.
CThe driver.manage() method is not initialized properly.
DCookies cannot be retrieved before page load.
Attempts:
2 left
💡 Hint
What happens if getCookieNamed does not find the cookie?
🧠 Conceptual
advanced
2:00remaining
What is the main reason to delete cookies during automated testing?
Why do testers often delete cookies before or after running automated Selenium tests?
ATo speed up the browser loading time by removing cookies.
BTo ensure tests run in a clean state without leftover session data affecting results.
CTo permanently block websites from tracking the user.
DTo increase the browser's cache size for faster performance.
Attempts:
2 left
💡 Hint
Think about test isolation and repeatability.
❓ framework
expert
2:00remaining
Which Selenium Java code snippet correctly deletes all cookies and verifies none remain?
Select the code snippet that deletes all cookies from the browser and then asserts that no cookies remain.
A
driver.manage().deleteAllCookies();
assertTrue(driver.manage().getCookies().isEmpty());
B
driver.manage().deleteAllCookies();
assertNotNull(driver.manage().getCookies());
C
driver.manage().deleteAllCookies();
assertFalse(driver.manage().getCookies().isEmpty());
D
driver.manage().deleteAllCookies();
assertEquals(null, driver.manage().getCookies());
Attempts:
2 left
💡 Hint
Check what getCookies() returns after deleting all cookies.

Practice

(1/5)
1. What is the primary purpose of managing cookies in Selenium tests?
easy
A. To change the browser's window size
B. To store and manipulate small pieces of data like user sessions
C. To locate elements on a web page
D. To execute JavaScript code on the page

Solution

  1. Step 1: Understand what cookies do

    Cookies store small data such as user sessions or preferences to remember users.
  2. Step 2: Relate to Selenium's cookie management

    Selenium allows adding, retrieving, and deleting cookies to simulate user sessions during tests.
  3. Final Answer:

    To store and manipulate small pieces of data like user sessions -> Option B
  4. Quick Check:

    Cookies = user session data [OK]
Hint: Cookies store user info for session tests [OK]
Common Mistakes:
  • Confusing cookies with browser window controls
  • Thinking cookies locate page elements
  • Assuming cookies run JavaScript
2. Which of the following is the correct way to add a cookie in Selenium Java?
easy
A. driver.addCookie(new Cookie("name", "value"));
B. driver.addCookie("name", "value");
C. driver.manage().cookieAdd("name", "value");
D. driver.manage().addCookie(new Cookie("name", "value"));

Solution

  1. Step 1: Recall Selenium cookie API

    Cookies are added via driver.manage().addCookie() method with a Cookie object.
  2. Step 2: Check syntax correctness

    driver.manage().addCookie(new Cookie("name", "value")); uses correct method and creates a new Cookie object properly.
  3. Final Answer:

    driver.manage().addCookie(new Cookie("name", "value")); -> Option D
  4. Quick Check:

    Use driver.manage().addCookie(new Cookie()) [OK]
Hint: Use driver.manage().addCookie(new Cookie()) [OK]
Common Mistakes:
  • Calling addCookie directly on driver
  • Using incorrect method names like cookieAdd
  • Passing strings instead of Cookie objects
3. What will be the output of the following code snippet?
Cookie cookie = new Cookie("session", "abc123");
driver.manage().addCookie(cookie);
Cookie retrieved = driver.manage().getCookieNamed("session");
System.out.println(retrieved.getValue());
medium
A. abc123
B. session
C. null
D. Throws NoSuchElementException

Solution

  1. Step 1: Add cookie named "session" with value "abc123"

    The code adds a cookie with name "session" and value "abc123" to the browser.
  2. Step 2: Retrieve cookie by name and print its value

    driver.manage().getCookieNamed("session") returns the cookie object, and getValue() prints "abc123".
  3. Final Answer:

    abc123 -> Option A
  4. Quick Check:

    getCookieNamed("session").getValue() = abc123 [OK]
Hint: getCookieNamed returns cookie object; getValue prints value [OK]
Common Mistakes:
  • Expecting cookie name instead of value
  • Assuming null if cookie not found (but it is added)
  • Thinking exception is thrown when cookie exists
4. Identify the error in this Selenium Java code for deleting a cookie:
driver.manage().deleteCookie("session");
medium
A. deleteCookie requires cookie name as a String, so code is correct
B. deleteCookie method does not exist in Selenium
C. deleteCookie method requires a Cookie object, not a String
D. deleteCookie should be called on driver, not driver.manage()

Solution

  1. Step 1: Check deleteCookie method signature

    deleteCookie expects a Cookie object, not a String name.
  2. Step 2: Correct usage

    To delete by name, use deleteCookieNamed(String name) instead.
  3. Final Answer:

    deleteCookie method requires a Cookie object, not a String -> Option C
  4. Quick Check:

    deleteCookie(Cookie) vs deleteCookieNamed(String) [OK]
Hint: Use deleteCookieNamed for String name, deleteCookie for Cookie object [OK]
Common Mistakes:
  • Passing String to deleteCookie instead of Cookie object
  • Confusing deleteCookie and deleteCookieNamed methods
  • Calling deleteCookie on wrong object
5. You want to test that after deleting a cookie named "user", it no longer exists. Which code snippet correctly verifies this?
hard
A. driver.manage().deleteCookieNamed("user"); assert driver.manage().getCookieNamed("user") == null;
B. driver.manage().deleteCookie(new Cookie("user", "value")); assert driver.manage().getCookieNamed("user").getValue().equals("");
C. driver.manage().deleteCookieNamed("user"); assert driver.manage().getCookieNamed("user").getValue() == null;
D. driver.manage().deleteCookie(new Cookie("user", "value")); assert driver.manage().getCookieNamed("user") != null;

Solution

  1. Step 1: Delete cookie by name "user"

    Use deleteCookieNamed("user") to remove the cookie by its name.
  2. Step 2: Verify cookie no longer exists

    getCookieNamed("user") returns null if cookie is deleted; assert null confirms deletion.
  3. Final Answer:

    driver.manage().deleteCookieNamed("user"); assert driver.manage().getCookieNamed("user") == null; -> Option A
  4. Quick Check:

    Deleted cookie returns null [OK]
Hint: Deleted cookie returns null on getCookieNamed [OK]
Common Mistakes:
  • Checking getValue() on null cookie causing NullPointerException
  • Using deleteCookie with Cookie object without correct value
  • Asserting cookie still exists after deletion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard