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 to set test case priority in testng?
Which automation tools can be used for post-launch validation with continuous integration?
What will be the limits of selenium?
Which is the latest Selenium tool?
How to find whether an element is displayed on the web page?
What is the use of get options () method?
Mention what is selenium 3.0?
what will be the first salary and what will be the hike in the salary?
What are the advantages of using git hub for selenium?
How can we check if an element is enabled for interaction on a web page?
Which is the super interface of webdriver?
selenium can handle pop-up windows based on selenium is a test tool?
How to write selenium code for purchasing product from amazon
How do you set user extensions in selenium ide?
How to verify tooltip text using selenium?