A utility class is a helper class that contains reusable methods to perform common tasks like waiting for elements, taking screenshots, or reading data. It helps avoid repeating code.
Click to reveal answer
beginner
Why should utility classes have static methods?
Static methods allow calling utility functions without creating an object. This makes it easy to reuse methods anywhere in the test code.
Click to reveal answer
intermediate
Give an example of a common method in a Selenium utility class.
A method to wait for an element to be visible, like: public static void waitForVisibility(WebDriver driver, WebElement element, int timeoutSeconds) { new WebDriverWait(driver, Duration.ofSeconds(timeoutSeconds)) .until(ExpectedConditions.visibilityOf(element)); }
Click to reveal answer
intermediate
How do utility classes improve test maintenance?
By centralizing common code, utility classes make it easier to update logic in one place instead of many test files, reducing errors and saving time.
Click to reveal answer
beginner
What is a best practice when naming utility classes?
Use clear, descriptive names like 'WaitUtils' or 'ScreenshotHelper' to indicate the purpose of the class and improve code readability.
Click to reveal answer
What is the main benefit of using utility classes in Selenium tests?
ATo reuse common code and avoid duplication
BTo make tests run faster
CTo create more test cases
DTo store test data
✗ Incorrect
Utility classes help reuse common code like waits or screenshots, reducing duplication and improving maintainability.
Which keyword is commonly used for methods in utility classes to avoid creating objects?
Astatic
Babstract
Cprivate
Dfinal
✗ Incorrect
Static methods can be called without creating an instance of the class, making them ideal for utility methods.
Which of the following is NOT a typical utility class method in Selenium?
ATaking screenshots
BLaunching the browser
CWaiting for element visibility
DReading test data from files
✗ Incorrect
Launching the browser is usually handled by setup code, not utility classes focused on reusable helper methods.
What should you do if you need to change the way you wait for elements in all tests?
AIgnore the change
BUpdate each test individually
CChange the wait method in the utility class
DWrite new tests
✗ Incorrect
Changing the wait method in the utility class updates all tests that use it, saving time and avoiding errors.
Which naming style is best for a utility class that handles waits?
ATest1
BMyClass
CHelper
DWaitUtils
✗ Incorrect
Clear and descriptive names like 'WaitUtils' help understand the class purpose quickly.
Explain what a utility class is and why it is useful in Selenium Java testing.
Think about helper methods you use often in tests.
You got /4 concepts.
Describe how static methods in utility classes help simplify test code.
Consider how you call methods from utility classes.
You got /3 concepts.
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
Step 1: Understand utility class role
Utility classes contain static methods to reuse code easily across tests.
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.
Final Answer:
To provide reusable static methods for common tasks -> Option D
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
Step 1: Recall utility method syntax
Utility methods are static so they can be called without creating an object.
Step 2: Check each option's syntax
public static void openBrowser() {} correctly declares a public static method with return type void.
Final Answer:
public static void openBrowser() {} -> Option A
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
Step 1: Understand method behavior
The method returns the page title by calling driver.getTitle().
Step 2: Check given page title
The page title is "Home Page", so the method returns this string.
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
Step 1: Check method syntax
The method calls element.click but misses parentheses, which is invalid syntax.
Step 2: Validate other options
Passing WebElement is valid, return type void is fine, static allowed in utility methods.
Final Answer:
Missing parentheses after click method -> Option A
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
Step 1: Understand utility method purpose
Utility methods should provide reusable, clean code for common tasks like waiting and clicking.
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.
Final Answer:
Create a static method that uses WebDriverWait and then clicks the element -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.