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