Reputation: 33
am using a static method to take screen shot and using reporter.log function attaching the screen shot to the index.html report of testNg. Here is the code for taking screen shot.
public class GenericHelper extends CNLogin {
public static String takeScreenShot(String methodName){
try {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// C:\Users\499290\AppData\Local\Temp\screenshot7520341205731631960.png
String FilePath = "C:\\Users\\499290\\Downloads\\CNProject1\\CNProject\\test-output\\";
new File(FilePath);
FileUtils.copyFile(scrFile, new File( FilePath +methodName +".jpg") );
System.out.println("***Placed screen shot in "+scrFile+" ***");
}
catch(IOException e) {
e.printStackTrace();
}
return methodName+".jpg";
}
}
Am attching the screen shot by using the below code in the index.html report
String TakescreenShot = GenericHelper.takeScreenShot("AddNewPr");
Reporter.log("<a href=\"" + TakescreenShot + "\"><p align=\"left\">Add New PR screenshot at " + new Date()+ "</p>");
am not able to take screen shot when a test case is failed neither the screen shot is getting attached to the report.
Here is my test case if it got passed my screen shot method will take the screen shot and attach the screen shot in the report but when its failed not sure how to take the screen shot.
public void MultipleServiceDelete() throws InterruptedException {
driver.findElement(By.id("page:frm:pageB:repeatUpper:0:repeat:0:chkIsDelete")).click();
Thread.sleep(5000);
driver.findElement(By.id("page:frm:pageB:btnDeleteMultipleServices")).click();
String DeleteService = ScreenShot.takeScreenShot("MultipleServiceDelete");
Reporter.log("<a href=\"" + DeleteService + "\"><p align=\"left\"> Delete Service screenshot at " + new Date()+ "</p>");
}
Upvotes: 2
Views: 3011
Reputation: 7466
You will want to add a TestNG listener that takes a screenshot when the test fails. Here is some code for a listener taken from my Selenium Maven Template:
package com.lazerycode.selenium.listeners;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.Augmenter;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import static com.lazerycode.selenium.DriverFactory.getDriver;
public class ScreenshotListener extends TestListenerAdapter {
private boolean createFile(File screenshot) {
boolean fileCreated = false;
if (screenshot.exists()) {
fileCreated = true;
} else {
File parentDirectory = new File(screenshot.getParent());
if (parentDirectory.exists() || parentDirectory.mkdirs()) {
try {
fileCreated = screenshot.createNewFile();
} catch (IOException errorCreatingScreenshot) {
errorCreatingScreenshot.printStackTrace();
}
}
}
return fileCreated;
}
private void writeScreenshotToFile(WebDriver driver, File screenshot) {
try {
FileOutputStream screenshotStream = new FileOutputStream(screenshot);
screenshotStream.write(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES));
screenshotStream.close();
} catch (IOException unableToWriteScreenshot) {
System.err.println("Unable to write " + screenshot.getAbsolutePath());
unableToWriteScreenshot.printStackTrace();
}
}
@Override
public void onTestFailure(ITestResult failingTest) {
try {
WebDriver driver = getDriver();
String screenshotDirectory = System.getProperty("screenshotDirectory");
String screenshotAbsolutePath = screenshotDirectory + File.separator + System.currentTimeMillis() + "_" + failingTest.getName() + ".png";
File screenshot = new File(screenshotAbsolutePath);
if (createFile(screenshot)) {
try {
writeScreenshotToFile(driver, screenshot);
} catch (ClassCastException weNeedToAugmentOurDriverObject) {
writeScreenshotToFile(new Augmenter().augment(driver), screenshot);
}
System.out.println("Written screenshot to " + screenshotAbsolutePath);
} else {
System.err.println("Unable to create " + screenshotAbsolutePath);
}
} catch (Exception ex) {
System.err.println("Unable to capture screenshot...");
ex.printStackTrace();
}
}
}
The bit you will probably be most interested in is the method called onTestFailure. This is the part that will be triggered when a test fails. I have a driver factory that provides my access to my driver object, the call to getDriver is getting my driver object from the factory. If you have just got a statically defined driver object you can probably ignore the line:
WebDriver driver = getDriver();
The other methods are just convenience methods to create a file and write the screenshot to it. You'll obviously need to tweak this a bit to allow it to take the location that the screenshot has been written and pass it into your reported log.
I would suggest giving the listener access to your Reporter object and changing:
System.out.println("Written screenshot to " + screenshotAbsolutePath);
to:
Reporter.log("<a href=\"" + screenshotAbsolutePath + "\"><p align=\"left\">Add New PR screenshot at " + new Date()+ "</p>");
In the code above, the directory that the screenshots are saved into is set using a system property called "screenshotDirectory". You will either need to set his system property, or change the following line to a hard coded location where you would like to save your screenshots. To do that this line:
String screenshotDirectory = System.getProperty("screenshotDirectory");
Will need to change to something like:
String screenshotDirectory = "/tmp/screenshots";
or if you use windows, something like:
String screenshotDirectory = "C:\\tmp\\screenshots";
Upvotes: 4