public void afterTestMethod(TestContext testContext) throws Exception {
if (testContext.getTestException() == null) {
return;
}
File screenshot = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
String testName = testContext.getTestClass().getSimpleName();
String methodName = testContext.getTestMethod().getName();
Files.copy(screenshot.toPath(),
Paths.get("C:\\Users\\user\\git\\ufe-360\\UFE-TESTS\\screenshots\test.png", testName + "_" + methodName + "_" + screenshot.getName()));
}
}
I have above code in my project to take screenshots after test execution. I suspect that something is missing in my code. When I'm running each test screenshots don't save in indicated path. I don't have any errors. Each test executes correctly but without screenshot.
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.OutputType;
private static void takeScreenshot() throws IOException, InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "chromedriver");
driver = new ChromeDriver();
driver.get("https://www.google.com/");
Thread.sleep(2);
TakesScreenshot scrShot =((TakesScreenshot)driver);
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
File DestFile=new File("/home/XXXX/Desktop/test.png");
FileUtils.copyFile(SrcFile, DestFile);
}
Above code will open "google.com", it will take a screenshot and store it on the desktop as i have given desktop path.