这是我的演示项目:
public class CapturingScreenshot{
ExtentReports extent;
ExtentTest test;
WebDriver driver;
@BeforeTest
public void init()
{
extent = new ExtentReports(System.getProperty("user.dir") + "/test-output/ExtentScreenshot.html", true);
}
@Test
public void scenario_01()
{
test = extent.startTest("captureScreenshot");
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://autoind2.hqdev.highq.com/autoind2/LoginRequiredPage.action");
String title = driver.getTitle();
Assert.assertEquals("Autoind2", title);
test.log(LogStatus.PASS, "Test Passed");
}
@AfterMethod
public void getResult(ITestResult result) throws IOException
{
if(result.getStatus() == ITestResult.FAILURE)
{
String screenShotPath = GetScreenShot.capture(driver, "screenShotName");
test.log(LogStatus.FAIL, result.getThrowable());
test.log(LogStatus.FAIL, "Snapshot below: " + test.addScreenCapture(screenShotPath));
}
extent.endTest(test);
}
@AfterTest
public void endreport()
{
driver.close();
extent.flush();
extent.close();
}
}
此文件用于捕获屏幕截图:
public class GetScreenShot {
public static String capture(WebDriver driver,String screenShotName) throws IOException
{
TakesScreenshot ts = (TakesScreenshot)driver;
File source = ts.getScreenshotAs(OutputType.FILE);
String dest = System.getProperty("user.dir") +"\\ErrorScreenshots\\"+screenShotName+".png";
File destination = new File(dest);
FileUtils.copyFile(source, destination);
return dest;
}
}
这是build.xml
<project basedir="." default="run" name="Extends Reports">
<target name="setClassPath">
<path id="TestAutomation.classpath">
<fileset dir="../lib">
<include name="*.jar" />
</fileset>
</path>
</target>
<target name="start" description="Pin the Starting time of Running file">
<tstamp>
<format property="START_TIME" pattern="MM_dd_yyyy hh_mm_ss" locale="en_GB" />
</tstamp>
<echo message="Starting: ${START_TIME}" />
</target>
<target name="clean">
<delete dir="../dist/bin" />
</target>
<target name="build" description="Starting Building">
<mkdir dir="../dist" />
<mkdir dir="../dist/bin/classes" />
<mkdir dir="../Reports" />
<mkdir dir="../Reports/Logs" />
</target>
<target name="compile" depends="setClassPath" description="Compilation process is starting">
<javac destdir="../dist/bin/classes" debug="true" includeantruntime="true" encoding="iso-8859-1">
<classpath refid="TestAutomation.classpath" />
<src>
<pathelement location="../src/screenshot_demo_fail_testcase" />
</src>
</javac>
<copy todir="../dist/" overwrite="true">
<fileset dir="../">
<include name="testng.xml" />
</fileset>
</copy>
</target>
<target name="jar" depends="clean,build,compile" description="Compile project file">
<jar jarfile="../dist/TestAutomation.jar" basedir="../dist/bin/classes" />
<antcall target="clean" />
</target>
<path id="distclasspath">
<fileset dir="../lib">
<include name="*.jar" />
</fileset>
<pathelement location="../dist/TestAutomation.jar" />
</path>
<target name="run" depends="start,clean,build,compile,jar">
<taskdef name="testng" classname="org.testng.TestNGAntTask">
<classpath>
<pathelement location="../lib/testng-6.9.9.jar" />
</classpath>
</taskdef>
<testng outputdir="../Reports/Report/${START_TIME}" classpathref="distclasspath" haltonfailure="false" useDefaultListeners="false" listeners="com.ontestautomation.extentreports.listener.ExtentReporterNG">
<xmlfileset dir="../dist/" includes="testng.xml" />
<sysproperty key="org.uncommons.reportng.title" value="My Test Report"/>
</testng>
</target>
这是我的testng.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<classes>
<class name="screenshot_demo_fail_testcase.CapturingScreenshot" />
</classes>
</test> <!-- Test -->
<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter" />
<listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
</listeners>
</suite> <!-- Suite -->
当我运行此项目时,它不会生成ExtentReport。请帮我解决这个问题。 在这里,我想使用ant build.xml运行我的项目,并且我希望在测试结束时使用ExtentReport。