Saturday, December 18, 2021

Screen Shot for Failure & Pass Test Cases With Extent Report

Step1:- Create Maven Project.

Step2:- Add Dependency


<dependency>

              <groupId>com.relevantcodes</groupId>

              <artifactId>extentreports</artifactId>

              <version>2.41.2</version>

          </dependency>

          <dependency>

              <groupId>org.testng</groupId>

              <artifactId>testng</artifactId>

              <version>6.14.3</version>

              <scope>test</scope>

          </dependency>

          <dependency>

              <groupId>org.seleniumhq.selenium</groupId>

              <artifactId>selenium-java</artifactId>

              <version>3.141.59</version>

          </dependency>

     

      <dependency>

              <groupId>commons-io</groupId>

              <artifactId>commons-io</artifactId>

              <version>2.6</version>

          </dependency>

    =========================================================

Step3:- Write Below code








=========================================================================
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import javax.swing.plaf.FileChooserUI;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;

public class AmazonTest {
public WebDriver driver;
public ExtentReports extent;
public ExtentTest extentTest;

@BeforeTest
public void setExtent(){
extent=new  ExtentReports(System.getProperty("user.dir")+"/test-output/ExtentReport.html",true);
extent.addSystemInfo("Host Name","Mohit Window");
extent.addSystemInfo("UserName","Mohit Kumar");
extent.addSystemInfo("Environment","QA");

}

@AfterTest
public void endReport(){
extent.flush();
extent.close();
}

public static String getScreenShot(WebDriver driver,String screenShotName) throws IOException{
String dateName=new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
TakesScreenshot ts= (TakesScreenshot)driver;
File sources=ts.getScreenshotAs(OutputType.FILE);
String destination= System.getProperty("user.dir") + "//FailedTestsScreenshots//" + screenShotName + dateName
+ ".png";
File fileDestination=new File(destination);
FileUtils.copyFile(sources, fileDestination);
return destination;
}



@BeforeMethod
public void setUp(){
System.setProperty("webdriver.chrome.driver","F:\\drivernew123\\chromedriver.exe");
driver=new ChromeDriver();

driver.get("https://ingenioustechhub.blogspot.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}


@Test
public void verifyTitle(){
extentTest=extent.startTest("verifyTitle");
String actual=driver.getTitle();
Assert.assertEquals(actual,"asasdasd");
}

@Test
public void verifyTitleOfpage(){
extentTest=extent.startTest("verifyTitle");
String actual=driver.getTitle();
Assert.assertEquals(actual,"Ingenious TechHub");
}

@AfterMethod
public void tearDown(ITestResult result) throws IOException{
if(result.getStatus()==ITestResult.FAILURE){
extentTest.log(LogStatus.FAIL,"TEST CASE FAILED IS "+result.getName());//to add name in extent report
extentTest.log(LogStatus.FAIL,"TEST CASE FAILED IS "+result.getThrowable());//to add error/exception in extent report
String screenshotPath=AmazonTest.getScreenShot(driver,result.getName());
extentTest.log(LogStatus.FAIL, extentTest.addScreenCapture(screenshotPath));//to add screenshot in extent report
//extentTest.log(LogStatus.FAIL, extentTest.addScreencast(screenshotPath));//to add screenshot in extent report

}
else if(result.getStatus()==ITestResult.SKIP){
extentTest.log(LogStatus.SKIP,"Test Case SKIPPED IS " + result.getName());
}
else if(result.getStatus()==ITestResult.SUCCESS){
String screenshotPath=AmazonTest.getScreenShot(driver,result.getName());
extentTest.log(LogStatus.PASS, extentTest.addScreenCapture(screenshotPath));
}
extent.endTest(extentTest);//Ending test and ends the current test and prepare to create html report

driver.quit();
}

}
=========================================================================
















No comments:

Post a Comment

Q) How To Find Duplicate Characters In A String In Java?

Step1:- Creating a HashMap containing char as key and it's occurrences as value. Step2:- Converting given string to char array. Step3:- ...