Capture FullPage Screenshot in Extent Reports – Java
Capture Screenshot in Extent Reports will discuss about capturing the screenshot of a particular failure step in the HTML report. In our previous blog we have seen how to log the messages in the report. Same way here we will capture the screenshot for a particular failure step. The screenshot will give the information about on which screen the script got failed during the execution. Then we can easily identify the issue by looking into the report.
But the issue from Selenium 3 is; it will not capture the full screenshot of the particular page rather it will capture only the visible area of the web page. But sometimes we need to capture full page of the particular web page. Then we can not use the normal screenshot functionality which is provided by the selenium. For this we will use Ashot to capture the full page screenshot of the particular web page. In one of our previous blogs we have seen how to capture the same using Ashot. In this blog, we will use Ashot functionality and make that available in the extent reports.
For this we need to use the log() method of ExtentTest class. To log the screenshot in the report we need to pass the path of the screenshot to the log method as a parameter. So, before logging the screenshot to the report we need to capture the full page screenshot of that particular failure and place it in a folder, from there we need to get the path of the screenshot file and pass it to the log method.
Below is the sample program to capture screenshot in the report:
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.ITestResult; import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import com.aventstack.extentreports.ExtentReports; import com.aventstack.extentreports.ExtentTest; import com.aventstack.extentreports.Status; import com.aventstack.extentreports.markuputils.ExtentColor; import com.aventstack.extentreports.markuputils.MarkupHelper; import com.aventstack.extentreports.reporter.ExtentHtmlReporter; public class CapturingFullPageScreenshot { ExtentHtmlReporter htmlReporter; ExtentReports extent; ExtentTest test; WebDriver driver; @BeforeTest public void config() { htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir") +"/test-output/MyOwnReport.html"); extent = new ExtentReports(); extent.attachReporter(htmlReporter); } @Test public void captureScreenshot() { test = extent.createTest("captureScreenshot"); System.setProperty("webdriver.gecko.driver", "/KRISHNA VOLUME/drivers/geckodriver"); driver = new FirefoxDriver(); driver.get("http://automationtesting.in"); String title = driver.getTitle(); Assert.assertEquals("Home - Automation Test", title); } @AfterMethod public void getResult(ITestResult result) throws Exception { if (result.getStatus() == ITestResult.FAILURE) { String screenShotPath = GetFullPageScreenShot.capture(driver, "MyFullPageScreenshot"); test.log(Status.FAIL, MarkupHelper.createLabel(result.getName()+" Test case FAILED due to below issues:", ExtentColor.RED)); test.fail(result.getThrowable()); test.fail("Snapshot below: " + test.addScreenCaptureFromPath(screenShotPath)); } else if(result.getStatus() == ITestResult.SUCCESS) { test.log(Status.PASS, MarkupHelper.createLabel(result.getName()+" Test Case PASSED", ExtentColor.GREEN)); } else { test.log(Status.SKIP, MarkupHelper.createLabel(result.getName()+" Test Case SKIPPED", ExtentColor.ORANGE)); test.skip(result.getThrowable()); } extent.flush(); } @AfterTest public void endReport() { driver.quit(); } }
In the above program we have written test.log method to log the screenshot into the extent reports. This information will logged in the report with time stamp.
In the above program we have written test.log method to log the screenshot into the extent reports. This information will logged in the report with time stamp. addScreenCaptureFromPath() is a method to place the screenshot in the extent report.
Below is the program to capture the full page screenshot and it will place the screenshot in a particular folder and will return the path of the screenshot. Will use that path to capture the full page screenshot in the HTML report for a failure step.
import org.openqa.selenium.WebDriver; import ru.yandex.qatools.ashot.AShot; import ru.yandex.qatools.ashot.Screenshot; import ru.yandex.qatools.ashot.shooting.ShootingStrategies; import javax.imageio.ImageIO; import java.io.File; public class GetFullPageScreenShot { public static String capture(WebDriver driver, String screenShotName) throws Exception { Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver); String dest = System.getProperty("user.dir") + "/ErrorScreenshots/" + screenShotName + ".png"; ImageIO.write(screenshot.getImage(),"PNG",new File(dest)); return dest; } }
Above program will return the path of the screenshot.
Below is the output of the above program:
Please watch the YouTube video of this blog for better understanding.