Capture ScreenShot in Extent Report in Selenium : Extent 3

Price: INR 8,999.00
Price: INR 14,999.00
Was: INR 16,999.00

In this tutorial, you will see, How can you capture ScreenShot in Extent Report in Selenium. In the previous tutorial Generate Extent Report in Selenium, you have seen how can you configure the Extent report and Generate the Extent report in Selenium.


In this tutorial, 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.


Capture Screenshot in Extent Report Example

First, you will see, How can you capture the ScreenShot in Selenium. Below is the Java Code –


Let’s understand the above code.

First, We have created the public Java class named “” .  In this Java class, We have two functions “captureScreen()

and “generateFileName()” .  You should notice both functions are the public static type, Which means these functions can be called anywhere in this project without creating the Object of the CaptureScreenShot class.


captureScreen(), this function is responsible for capturing the screenshot and saving it. This function is accepting two parameters the first object of WebDribver and second is String for screenshot name. First, we are capturing the screenshot using “TakesScreenshot” class of Selenium than saving the file File type variable.

Then we are saving the file with the given name in the “Test-ScreenShots” directory inside the Project. and return the file path.


generateFileName(), this function is generating the screenshot name. Although you can hard code the screenshot name in captureScreen() function, in that case, The new screenshot will always replace the older screenshot because it will save the file with the same name. So we are generating the new name for each screen shot. Every time this function will be called, this will return the unique string value. This function is accepting the “ITestResult“, which is TestNG’s listener. Which will help us to find the currently running test name.


Selenium Video Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.


You can see, We are generating the file name with Test Case name and combination of Date. This string will be unique all the time because it will contain the value in millisecond up to 3 places.


The above class will capture the screenshot and provide the unique name for the each screenshot. Now let’s see how can we add these captured screenshots in Extent HTML reports.


We need to use the methods of ExtentTest class. To log the screenshot in the report we need to pass the path of the screenshot to the ExtentTest class’s method as a parameter. So, before logging the screenshot to the report we need to capture the screenshot, We have already seen, How can we capture the screenshot and generate the save the screenshot file. Below is the code –


You can see, In above Test Script. We have two tests methods “verifyTittleTest()” and “fillRegistrationformTest()“. The first test will be passed and second will be failed because We are trying to save the incomplete Registration form.


Inside the @BeforeTest we have configured the Extent Report, Which we have already seen in the last tutorial Generate Extent Report in Selenium. Inside the @BeforeMethod we are initializing the WebDriver Object and Opening the WebAppliation in Browser using Selenium.


Then we have implemented the Two Selenium Tests. You can learn the Selenium Tutorials to understand this code. Inside the @AfterMethod we are closing the Extent report. We have seen this in the last tutorial.


The main thing to note in @AfterMethod is, Here we are creating the screenshot file path using the code –

String screenShot = CaptureScreenShot.captureScreen(driver, CaptureScreenShot.generateFileName(result));

Which we have implemented in separate Java Class. You can see both captureScreen() and generateFileName() are the static functions. So that we are calling them without creating the Object of CaptureScreenShot class.


Later on, we are using the addScreenCaptureFromPath() function fo ExtentTest class to save the screenshot in the report and providing the path of ScreenShot to this function. You can notice we have called the below line of code –“Screen Shot : ” + test.addScreenCaptureFromPath(screenShot));

in both of the cases, for failure and for the pass. So This will capture the screen for Test’s Fail Step and Pass Step. You can use it as per your own requirements.


That’s all about the Capture ScreenShot in Extent Report in Selenium.


0 Comment

Leave a Reply