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

How do you launch ie/chrome browser?

587


What happens if I run this command. Driver.get(“www.softwaretestingmaterial.com”) ;

1436


How to submit a form using selenium webdriver?

602


What do you mean by xpath?

570


Is there an htmlunitdriver for .net?

631






What is junit annotations and what are different types of annotations which are useful?

591


What is the difference between close() and quit()?

577


What are the advantages of rc?

599


Write a code snippet to launch firefox browser in webdriver.

559


Can we move back and forward in browser using selenium?

573


Tell us how will you use selenium to upload a file?

544


Tell me how can we maximize browser window in selenium?

527


What is the hybrid framework?

645


Do you know a way to refresh the browser by using selenium?

545


What does a concentrator do in the selenium network?

599