How to capture the screenshot of failed testcase only among a set of testcases?
Answers were Sorted based on User's Feedback
Answer / suresh vemula
First we have to create a method of screen shot using java.
After that we have annotation in testng to identify the failure test case or methods using "iTestResult"
This iTestResult using only where annotation in aftermethod
@Aftermethod
Public void test(iTestResult.FAILURE==result.getStatus()){
Utility.screenshot(driver,result.getname());
}
Here utility.screenshot is java class to created for screenshot
Here getName() returns the failure test case name
Then you have to identify the failure test easily
| Is This Answer Correct ? | 3 Yes | 0 No |
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 |
In which format does source view shows your script in selenium ide?
Which attribute you should consider throughout the script in frame for “if no frame id as well as no frame name”?
What is the selenium's recording language?
How to insert a comment in selenium ide?
what is the use of x-path?
What you say in regards to the flexibility of selenium test suite?
When we use findElement () and findElements () findElement ()?
Who is the best faculty for selenium in twin cities (Hyderabad/secundrabad)
What is the difference between "assert" and "verify" commands?
what is the difference between 2.0 & 3.0
What automation tools could be used for post-release validation with continuous integration?
How would you test your element locator?