Challenge - 5 Problems
Utility Class Master
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 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);
Attempts:
2 left
💡 Hint
Think about how WebDriverWait handles waiting and exceptions.
✗ Incorrect
The method uses WebDriverWait with ExpectedConditions.visibilityOfElementLocated. If the element is not found within the timeout, a TimeoutException is caught and the method returns false.
❓ assertion
intermediate2: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();
}Attempts:
2 left
💡 Hint
Use the correct assertion method for string equality.
✗ Incorrect
assertEquals compares string values correctly. Using == compares references, which is incorrect for strings in Java.
❓ locator
advanced2: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?
Attempts:
2 left
💡 Hint
The button text is unique but no id or class is guaranteed.
✗ Incorrect
Using XPath with the exact button text ensures the utility method finds the button by visible text reliably.
🔧 Debug
advanced2: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();
}
}Attempts:
2 left
💡 Hint
Check what findElement returns if no element matches the locator.
✗ Incorrect
findElement throws NoSuchElementException if element not found; it never returns null. NullPointerException is unlikely here unless driver is null.
❓ framework
expert3: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?
Attempts:
2 left
💡 Hint
Think about how utility classes are commonly designed for easy use across tests.
✗ Incorrect
Static methods in a final utility class allow easy reuse without needing to instantiate objects, improving maintainability and reducing boilerplate.