How to capture the screenshot of failed testcase only among a set of testcases?

Answer Posted / kiran212

package TestNG;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class Screenshot {
WebDriver driver;
@BeforeClass
public void setup() {
driver = new FirefoxDriver();
driver.get("http://google.com");
}

@Test
public void tc01() {
driver.findElement(By.xpath("wrong xpath"));
}
@Test
public void tc02(){

System.out.println(driver.getTitle());

}

@AfterClass
public void browserKill() {
driver.close();
}
//screenshot method for failure test cases
@AfterMethod(alwaysRun=true)
public void takeScreenShot(ITestResult result) throws IOException{
if(result.getStatus()==2){
String testName = result.getMethod().getMethodName();
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File("D://"+testName+".jpeg"));
}
}
}

Is This Answer Correct ?    2 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain the fundamental difference between xpath and css selector.

531


What you say in regards to the flexibility of selenium test suite?

522


How can we fetch the page source in selenium?

537


How does one submit a form in selenium?

533


Explain how you can insert a break point in selenium ide?

480






What are the benefits of automation testing?

574


Explain how you can use recovery scenario with selenium?

481


What kinds of test types are supported by selenium?

446


What is the difference between verify and assert commands?

505


What are the annotations used in testng?

513


How can we clear a text written in a textbox?

527


How to find more than one web element in the list?

479


What are some commonly encountered exceptions in selenium?

530


What is Dynamic xpath in selenium ?

545


How to do file upload in selenium?

532