Selenium Automated Validation Frameworks 3 — Questions and Answers
Question 1: Which Hamcrest matcher verifies that a string contains a specific substring?
- containsString() (Correct answer)
- hasString()
- includesString()
- matchesString()
Correct answer: containsString()
Hamcrest's containsString() matcher checks that the actual string contains the specified substring anywhere within it.
Question 2: In a Selenium Page Object, what is the recommended return type for an action method that navigates to a new page?
- The new Page Object class (Correct answer)
- void
- boolean
- String
Correct answer: The new Page Object class
Returning the next Page Object from navigation methods enables fluent chaining and clearly models page transitions in the test flow.
Question 3: What is the role of a TestNG listener implementing ITestListener?
- To hook into test lifecycle events like onTestFailure and onTestSuccess (Correct answer)
- To provide test data for parameterized tests
- To define the execution order of test methods
- To configure browser driver settings
Correct answer: To hook into test lifecycle events like onTestFailure and onTestSuccess
ITestListener lets you respond to test events such as pass, fail, or skip to perform custom actions like logging or screenshots.
Question 4: Which JUnit 5 extension replaces JUnit 4's @Rule for managing external resources?
- @ExtendWith
- @RegisterExtension (Correct answer)
- @ClassRule
- @RunWith
Correct answer: @RegisterExtension
@RegisterExtension allows programmatic registration of JUnit 5 extensions to manage lifecycle-scoped resources like WebDriver instances.
Question 5: In a data-driven Selenium framework, what is the primary advantage of reading test data from an external Excel file?
- Test data can be updated without modifying the test code (Correct answer)
- Test execution speed increases significantly
- WebDriver initialization is simplified
- Assertions are automatically generated
Correct answer: Test data can be updated without modifying the test code
Externalizing test data separates data from logic, allowing testers to add or modify scenarios without touching automation code.
Question 6: What does the SoftAssert.assertAll() method do in TestNG?
- Throws an AssertionError if any soft assertion failed during the test (Correct answer)
- Clears all recorded assertion failures
- Marks the test as skipped
- Retries all failed assertions
Correct answer: Throws an AssertionError if any soft assertion failed during the test
assertAll() must be called at the end of a test to throw a consolidated exception listing all soft assertion failures.
Question 7: In a keyword-driven framework, what does a 'keyword' represent?
- A reusable action function mapped to a word in a test data table (Correct answer)
- A Selenium locator strategy
- A TestNG annotation
- A browser configuration flag
Correct answer: A reusable action function mapped to a word in a test data table
Keywords are named functions (like clickButton or enterText) that non-technical testers invoke by name in a structured data table.
Which Hamcrest matcher verifies that a string contains a specific substring?