โ† Back to libraryQuestion 161 of 273
๐ŸงชSeleniumAdvanced

Listeners in Selenium

๐Ÿ“– Detailed Explanation:

Listeners 'listen' for events in your script (like the start of a test, a success, or a failure) and trigger specific code in response. In Selenium 4, you use `WebDriverListener` (formerly `WebDriverEventListener`). In TestNG, you use `ITestListener`. By implementing listeners, you can add global logging, screenshots on failure, report generation, and more without modifying individual test methods.

๐ŸŒ Real-World Example:

Listeners are like setting up alarms. When 'Test Starts', trigger logging. When 'Test Fails', trigger screenshot. Without manually adding code everywhere.

๐ŸŽฏ Scenario-Based Interview Question:

How can you automatically log every URL the driver navigates to without adding `System.out.println` to every test?