Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes, you can include the link to your screenshot in testng report. </p> <p>You need to call <code>org.testng.Reporter.log</code> method to write the hyperlink to the <a href="/questions/tagged/testng" class="post-tag" title="show questions tagged &#39;testng&#39;" rel="tag">testng</a> report either by annotating your test class or parent of all testclasses with @Listeners({yourListener.class}) or by adding the listener to your <code>testng.xml</code>.</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"&gt; &lt;suite name="default"&gt; &lt;listeners&gt; &lt;listener class-name="ScreenshotListener" /&gt; &lt;/listeners&gt; &lt;test name="Test"&gt; &lt;packages&gt; &lt;package name="someTests.*"/&gt; &lt;/packages&gt; &lt;/test&gt; &lt;/suite&gt; </code></pre> <p>You need to first create a Listener class and add it to testng. You can get details for that from testng.org. Search for listener. </p> <p>Once you create that class, you should write a method in it which overrides the <code>ontestfailure</code> method. The code inside this method will be executed whenever testng identifies a failure. </p> <p>You can call your screenshot grabbing method and use <code>Reporter.log</code> to put the hyperlink to that screenshot. Then you can find this link under the failed testcases details.</p> <pre><code>import java.io.*; import java.util.*; import java.text.*; import org.apache.commons.io.FileUtils; import org.openqa.selenium.*; import org.testng.*; public class ScreenshotListener extends TestListenerAdapter { @Override public void onTestFailure(ITestResult result) { Calendar calendar = Calendar.getInstance(); SimpleDateFormat formater = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss"); String methodName = result.getName(); if(!result.isSuccess()){ File scrFile = ((TakesScreenshot)SomeStaticWebDriver.driver).getScreenshotAs(OutputType.FILE); try { String reportDirectory = new File(System.getProperty("user.dir")).getAbsolutePath() + "/target/surefire-reports"; File destFile = new File((String) reportDirectory+"/failure_screenshots/"+methodName+"_"+formater.format(calendar.getTime())+".png"); FileUtils.copyFile(scrFile, destFile); Reporter.log("&lt;a href='"+ destFile.getAbsolutePath() + "'&gt; &lt;img src='"+ destFile.getAbsolutePath() + "' height='100' width='100'/&gt; &lt;/a&gt;"); } catch (IOException e) { e.printStackTrace(); } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload