0
0
Selenium Javatesting~20 mins

Utility classes in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Utility Class Master
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 utility method?
Consider the following Java utility method that waits for an element to be visible. What will be the output if the element is not found within the timeout?
Selenium Java
public static boolean waitForElementVisible(WebDriver driver, By locator, int timeoutSeconds) {
    try {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeoutSeconds));
        wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
        return true;
    } catch (TimeoutException e) {
        return false;
    }
}

// Called as: waitForElementVisible(driver, By.id("nonexistent"), 5);
AReturns false after 5 seconds timeout
BThrows TimeoutException immediately
CReturns true immediately
DThrows NoSuchElementException immediately
Attempts:
2 left
💡 Hint
Think about how WebDriverWait handles waiting and exceptions.
assertion
intermediate
2:00remaining
Which assertion correctly verifies a page title using a utility method?
Given a utility method getPageTitle(WebDriver driver) that returns the current page title, which assertion correctly checks that the title equals "Home Page"?
Selenium Java
public static String getPageTitle(WebDriver driver) {
    return driver.getTitle();
}
AassertEquals("Home Page", getPageTitle(driver));
BassertTrue(getPageTitle(driver) == "Home Page");
CassertFalse(getPageTitle(driver).equals("Home Page"));
DassertNotNull(getPageTitle(driver));
Attempts:
2 left
💡 Hint
Use the correct assertion method for string equality.
locator
advanced
2:00remaining
Which locator strategy is best for a utility method to find a button by its visible text?
You want to write a utility method that clicks a button by its visible text. Which locator is the best choice?
ABy.className("btn-primary")
BBy.id("submitBtn")
CBy.cssSelector("button.submit")
DBy.xpath("//button[text()='Submit']")
Attempts:
2 left
💡 Hint
The button text is unique but no id or class is guaranteed.
🔧 Debug
advanced
2:00remaining
Why does this utility method throw a NullPointerException?
Analyze the following utility method. Why does it throw NullPointerException sometimes?
Selenium Java
public static void clickElement(WebDriver driver, By locator) {
    WebElement element = driver.findElement(locator);
    if (element.isDisplayed()) {
        element.click();
    }
}
Aelement is null if not found, causing NullPointerException on isDisplayed()
BfindElement throws NoSuchElementException if element not found, not NullPointerException
Cclick() throws NullPointerException if element is not clickable
Ddriver is null causing NullPointerException
Attempts:
2 left
💡 Hint
Check what findElement returns if no element matches the locator.
framework
expert
3:00remaining
Which design best improves reusability of Selenium utility classes?
You want to design a Selenium utility class for common actions like clicking, typing, and waiting. Which design approach best improves reusability and maintainability?
ACreate utility methods inside each test class separately for better context
BCreate instance methods in a utility class that stores WebDriver as a field initialized in constructor
CCreate static methods in a final utility class that accept WebDriver and locators as parameters
DCreate utility methods that extend WebDriver class directly
Attempts:
2 left
💡 Hint
Think about how utility classes are commonly designed for easy use across tests.