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
How is selenium different from qtp?
How to insert a comment in selenium ide?
@timeouts ||= Timeouts.new(@bridge) what does it mean?
Tell us how to perform right click using selenium webdriver?
What is a fundamental difference between xpath and css selector?
What are the main advantages of automation testing?
How to check if a button is enabled on the page?
What are the different exceptions you have faced in selenium webdriver?
How to export the tests from selenium ide to selenium rc in different languages?
List out the test types that are supported by selenium?
Tell me what is a hub in selenium grid?
What are the different types of drivers available on Webdriver ?
What are the capabilities of selenium webdriver or google webdriver or selenium 2.0?
What is an explicit wait in selenium?
How to read a javascript variable in selenium webdriver?