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 do you read test data from excels?
Explain me what could be the cause of selenium webdriver test to fail?
How you can handle colors in web driver?
List the parameters that one needs to pass in selenium.
How do you achieve synchronization in webdriver?
What is robot api?
How do you simulate scroll down action?
How to check the checkbox or radio button is selected?
Can you use Selenium to test the Rest API or Web services?
What could be the cause of selenium webdriver test to fail?
List the different types of drivers in webdriver.
Write the code to right click an element in selenium?
What is the difference between selenium and qtp?
How to retrieve css properties of an element?
What is the difference between "assert" and "verify" commands?