← Back to libraryQuestion 297 of 468
🏗️Framework DesignIntermediate

Failure Handling and Screenshots

📌 Definition:

A robust framework captures diagnostic evidence on failure automatically — a screenshot, page source, and logs — via listeners/hooks, and handles exceptions so failures are clear and the suite continues cleanly.

📖 Detailed Explanation:

Rather than each test capturing its own screenshot, a listener (TestNG ITestListener.onTestFailure / JUnit TestWatcher.testFailed / Cucumber @After) grabs a screenshot (TakesScreenshot), attaches it to the report, and logs the error context — automatically for every failing test. The framework should also ensure teardown always runs (quit the driver in @After/finally) even when a test throws, and translate low-level exceptions into meaningful messages. Retry (sparingly) can be centralized too. Automatic failure evidence dramatically cuts debugging time and is a hallmark of a mature framework.

🔑 Key Points:
  • Auto-capture screenshot + page source + logs on failure (via listener/hook)
  • Attach evidence to the report for every failing test
  • Guarantee teardown (quit driver) even on exceptions
  • Centralize, don't duplicate, failure handling per test
🌍 Real-World Example:

onTestFailure(result) takes a screenshot, saves page source, logs the error, and embeds them in ExtentReports — so a nightly failure is diagnosable from the report alone without re-running locally.

🎯 Scenario-Based Interview Question:

How do you ensure every failing test automatically produces a screenshot without adding code to each test?