Here’s the code sample from our tutorial on how to take a screenshot in Selenium for Java

 

package screenshots;
import base.BaseTests;
import com.google.common.io.Files;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import java.io.File;
import java.io.IOException;
public class ScreenshotTests extends BaseTests {
@BeforeEach
public void launchApp(){
driver.get("https://kitchen.applitools.com");
}
@Test
public void takeScreenshot(){
var camera = (TakesScreenshot)driver;
File screenshot = camera.getScreenshotAs(OutputType.FILE);
try{
Files.move(screenshot, new File("resources/images/page.png"));
}catch(IOException e){
e.printStackTrace();
}
}
}
view raw ScreenshotTests.java delivered with ❤ by emgithub

View on GitHub
Angie Jones
Automation Architect

Ready for the next generation of testing?

Get started today Schedule a demo