Search code examples
hookscreenshotcaptureselenium4

Selenium 4 : How to capture screenshot


I need an After method to capture a screenshot compatible with Selenium 4 Java


Solution

  • @After
    public void tearDown(Scenario scenario) {
        if (scenario.isFailed()) {
            final byte[] screenshot = ((TakesScreenshot) Driver.get()).getScreenshotAs(OutputType.BYTES);
            scenario.attach(screenshot, "image/png", "screenshot");
        }
    
    
        Driver.closeDriver();
    
    }