0
0
Selenium Javatesting~20 mins

Cookie management in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 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.