Challenge - 5 Problems
Cookie Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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(); } }
Attempts:
2 left
💡 Hint
Think about what value is stored in the cookie and what getValue() returns.
✗ Incorrect
The code adds a cookie named 'session_id' with value 'abc123'. Retrieving it by name and calling getValue() returns 'abc123'.
❓ assertion
intermediate2: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?
Attempts:
2 left
💡 Hint
Check what getCookieNamed returns if the cookie does not exist.
✗ Incorrect
getCookieNamed returns null if the cookie is not found. So asserting not null confirms the cookie exists.
🔧 Debug
advanced2: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());Attempts:
2 left
💡 Hint
What happens if getCookieNamed does not find the cookie?
✗ Incorrect
If the cookie is not found, getCookieNamed returns null. Calling getValue() on null causes NullPointerException.
🧠 Conceptual
advanced2: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?
Attempts:
2 left
💡 Hint
Think about test isolation and repeatability.
✗ Incorrect
Deleting cookies ensures no leftover session or user data affects test outcomes, making tests reliable and repeatable.
❓ framework
expert2: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.
Attempts:
2 left
💡 Hint
Check what getCookies() returns after deleting all cookies.
✗ Incorrect
After deleting all cookies, getCookies() returns an empty set, so isEmpty() returns true. This confirms deletion.