0
0
Selenium Javatesting~20 mins

Screenshot attachment on failure in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Screenshot Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when this Selenium test fails?
Consider this Java Selenium test snippet that takes a screenshot on failure. What will be the result if the assertion fails?
Selenium Java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.*;
import java.io.File;
import org.apache.commons.io.FileUtils;

public class ScreenshotTest {
    WebDriver driver;

    @BeforeMethod
    public void setup() {
        driver = new ChromeDriver();
        driver.get("https://example.com");
    }

    @Test
    public void testTitle() throws Exception {
        try {
            Assert.assertEquals(driver.getTitle(), "Wrong Title");
        } catch (AssertionError e) {
            File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(src, new File("failure.png"));
            throw e;
        }
    }

    @AfterMethod
    public void teardown() {
        driver.quit();
    }
}
ATest fails with AssertionError and a screenshot file named 'failure.png' is saved in the project folder.
BTest passes because the screenshot is taken and the error is caught, so no failure is thrown.
CTest fails but no screenshot is saved because FileUtils.copyFile throws IOException that is not handled.
DTest fails and throws a NullPointerException because driver is not initialized.
Attempts:
2 left
💡 Hint
Look at how the AssertionError is caught and rethrown after saving the screenshot.
assertion
intermediate
1:30remaining
Which assertion correctly verifies screenshot file creation after failure?
After a Selenium test fails and saves a screenshot named 'error.png', which assertion correctly checks that the screenshot file exists?
Selenium Java
File screenshot = new File("error.png");
AAssert.assertFalse(screenshot.isFile());
BAssert.assertEquals(screenshot.length(), 0);
CAssert.assertTrue(screenshot.exists());
DAssert.assertNull(screenshot);
Attempts:
2 left
💡 Hint
Check if the file physically exists on disk.
🔧 Debug
advanced
2:00remaining
Why does this screenshot capture code throw IOException?
This Selenium Java code snippet throws IOException during screenshot capture. What is the cause?
Selenium Java
File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("/invalid_path/failure.png"));
AFileUtils.copyFile requires a third argument for overwrite permission.
BThe driver is not cast correctly to TakesScreenshot.
CThe source file 'src' is null because screenshot failed.
DThe destination path '/invalid_path/failure.png' does not exist or is not writable.
Attempts:
2 left
💡 Hint
Check the file path permissions and existence.
framework
advanced
1:30remaining
Which TestNG listener interface is best to implement automatic screenshot on test failure?
You want to automatically capture screenshots on test failures in TestNG without modifying each test method. Which listener interface should you implement?
AISuiteListener
BITestListener
CIAnnotationTransformer
DIInvokedMethodListener
Attempts:
2 left
💡 Hint
Look for listener that reacts to test method results.
🧠 Conceptual
expert
1:00remaining
What is the main benefit of attaching screenshots to test reports on failure?
Why is attaching screenshots to automated test failure reports considered a best practice in software testing?
AIt provides visual evidence of the failure, helping faster debugging and communication.
BIt reduces the test execution time by skipping logs.
CIt automatically fixes UI bugs by capturing screenshots.
DIt prevents tests from failing by retrying the failed steps.
Attempts:
2 left
💡 Hint
Think about how screenshots help developers and testers understand failures.