Bird
Raised Fist0
Selenium Javatesting~20 mins

Utility classes in Selenium Java - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What is the main purpose of a utility class in Selenium Java testing?
easy
A. To define test case steps in detail
B. To store test data in variables
C. To create WebDriver instances for each test
D. To provide reusable static methods for common tasks

Solution

  1. Step 1: Understand utility class role

    Utility classes contain static methods to reuse code easily across tests.
  2. Step 2: Compare options with utility class purpose

    Only To provide reusable static methods for common tasks describes reusable static methods, which is the utility class purpose.
  3. Final Answer:

    To provide reusable static methods for common tasks -> Option D
  4. Quick Check:

    Utility class = reusable static methods [OK]
Hint: Utility classes hold static helpers for reuse [OK]
Common Mistakes:
  • Confusing utility class with test data storage
  • Thinking utility class creates WebDriver instances
  • Mixing utility class with test step definitions
2. Which of the following is the correct way to declare a utility method in Java?
easy
A. public static void openBrowser() {}
B. public void openBrowser() {}
C. void openBrowser() {}
D. static openBrowser() {}

Solution

  1. Step 1: Recall utility method syntax

    Utility methods are static so they can be called without creating an object.
  2. Step 2: Check each option's syntax

    public static void openBrowser() {} correctly declares a public static method with return type void.
  3. Final Answer:

    public static void openBrowser() {} -> Option A
  4. Quick Check:

    Utility method = public static method [OK]
Hint: Utility methods must be static for easy access [OK]
Common Mistakes:
  • Omitting static keyword
  • Missing return type
  • Incorrect method declaration syntax
3. Given this utility method:
public static String getPageTitle(WebDriver driver) {
    return driver.getTitle();
}

What will be the output if the current page title is "Home Page"?
medium
A. Compilation error
B. null
C. "Home Page"
D. driver.getTitle()

Solution

  1. Step 1: Understand method behavior

    The method returns the page title by calling driver.getTitle().
  2. Step 2: Check given page title

    The page title is "Home Page", so the method returns this string.
  3. Final Answer:

    "Home Page" -> Option C
  4. Quick Check:

    Method returns page title string [OK]
Hint: Method returns driver.getTitle() string [OK]
Common Mistakes:
  • Confusing method return with method name
  • Expecting null instead of actual title
  • Assuming syntax error without checking code
4. Identify the error in this utility class method:
public static void clickElement(WebElement element) {
    element.click;
}
medium
A. Missing parentheses after click method
B. WebElement cannot be passed as parameter
C. Method should return boolean
D. Static keyword is not allowed here

Solution

  1. Step 1: Check method syntax

    The method calls element.click but misses parentheses, which is invalid syntax.
  2. Step 2: Validate other options

    Passing WebElement is valid, return type void is fine, static allowed in utility methods.
  3. Final Answer:

    Missing parentheses after click method -> Option A
  4. Quick Check:

    Method call needs () after method name [OK]
Hint: Method calls need parentheses () [OK]
Common Mistakes:
  • Forgetting parentheses on method calls
  • Thinking WebElement can't be parameter
  • Assuming static keyword causes error
5. You want to create a utility class method that waits for an element to be visible before clicking it. Which approach is best?
hard
A. Add wait logic inside each test instead of utility method
B. Create a static method that uses WebDriverWait and then clicks the element
C. Create a non-static method that clicks element immediately without wait
D. Use Thread.sleep() inside utility method before clicking

Solution

  1. Step 1: Understand utility method purpose

    Utility methods should provide reusable, clean code for common tasks like waiting and clicking.
  2. Step 2: Evaluate options for best practice

    Create a static method that uses WebDriverWait and then clicks the element uses static method with WebDriverWait, which is efficient and reusable. Others are less reliable or not reusable.
  3. Final Answer:

    Create a static method that uses WebDriverWait and then clicks the element -> Option B
  4. Quick Check:

    Use static method with explicit wait [OK]
Hint: Use static method with WebDriverWait for reliable clicks [OK]
Common Mistakes:
  • Using Thread.sleep() causing slow tests
  • Not using static method for utility
  • Putting wait logic inside tests, not utility
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