Java 如何使用ReportNG在selenium webdriver中拍摄失败测试的屏幕截图
如果有人知道如何使用reportNG在selenium webdriver中拍摄失败测试/方法的屏幕截图,请提供帮助 如果您提供了代码,那么它将非常有用,或者提供一些解决此问题的方法 提前谢谢。是的,这是可能的Java 如何使用ReportNG在selenium webdriver中拍摄失败测试的屏幕截图,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,如果有人知道如何使用reportNG在selenium webdriver中拍摄失败测试/方法的屏幕截图,请提供帮助 如果您提供了代码,那么它将非常有用,或者提供一些解决此问题的方法 提前谢谢。是的,这是可能的 WebDriver firfoxDriver = new FirefoxDriver(); firfoxDriver.get("https://stackoverflow.com/"); File file = ((TakesScreenshot)firfoxDriver).getScr
WebDriver firfoxDriver = new FirefoxDriver();
firfoxDriver.get("https://stackoverflow.com/");
File file = ((TakesScreenshot)firfoxDriver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(file, new File("E:\\Data\\ss.png"));
是的,这是可能的
WebDriver firfoxDriver = new FirefoxDriver();
firfoxDriver.get("https://stackoverflow.com/");
File file = ((TakesScreenshot)firfoxDriver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(file, new File("E:\\Data\\ss.png"));
或者,您可以使用:
@AfterMethod(alwaysRun=true)
public void catchExceptions(ITestResult result){
Calendar calendar = Calendar.getInstance();
SimpleDateFormat formater = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss");
String methodName = result.getName();
if(!result.isSuccess()){
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(scrFile, new File((String) PathConverter.convert("failure_screenshots/"+methodName+"_"+formater.format(calendar.getTime())+".png")));
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
或者,您可以使用:
@AfterMethod(alwaysRun=true)
public void catchExceptions(ITestResult result){
Calendar calendar = Calendar.getInstance();
SimpleDateFormat formater = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss");
String methodName = result.getName();
if(!result.isSuccess()){
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(scrFile, new File((String) PathConverter.convert("failure_screenshots/"+methodName+"_"+formater.format(calendar.getTime())+".png")));
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
好的,这取决于你的框架,在方法失败后,控件将去哪里 假设在您的例子中,它将进入catch异常块,然后在catch块中复制该代码
File file = ((TakesScreenshot)firfoxDriver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File("YOUR PATH"+Filename+.JPG));
好的,这取决于你的框架,在方法失败后,控件将去哪里 假设在您的例子中,它将进入catch异常块,然后在catch块中复制该代码
File file = ((TakesScreenshot)firfoxDriver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File("YOUR PATH"+Filename+.JPG));
您可以在
testng
中使用Itestlistener
拍摄屏幕截图testng
有一个内置功能,当它失败时,您可以通过该功能拍摄屏幕截图
只需创建一个单独的listerner类并粘贴以下代码:
public class listeners implements ITestListener{
//This "Base " is the main class where you can pass the result of test case and write the //code for screenshot
//I cam creating an object so I can access the method that I created for screenshot in this base class.
//I am also getting the result name so I can identify which test case it got failed
Base b = new Base();
public void onTestFailure(ITestResult result) {
try {
//Getting the result name by result.getName() method
b.getScreenshot( result.getName());
System.out.println("The Failed test is=="+result.getName());
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我在基类中创建了这个getScreenshot
方法,并传递了结果名
public class Base {
public WebDriver InitializeDr() throws IOException {
//Creating the method to take screenshot in the Base class
public void getScreenshot(String result) throws IOException
{
File dest=new File("D:\\Work\\KT\\Scr Shot\\"+result+"test.png");
File src=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, dest);
}
}
}
您可以在
testng
中使用Itestlistener
拍摄屏幕截图testng
有一个内置功能,当它失败时,您可以通过该功能拍摄屏幕截图
只需创建一个单独的listerner类并粘贴以下代码:
public class listeners implements ITestListener{
//This "Base " is the main class where you can pass the result of test case and write the //code for screenshot
//I cam creating an object so I can access the method that I created for screenshot in this base class.
//I am also getting the result name so I can identify which test case it got failed
Base b = new Base();
public void onTestFailure(ITestResult result) {
try {
//Getting the result name by result.getName() method
b.getScreenshot( result.getName());
System.out.println("The Failed test is=="+result.getName());
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我在基类中创建了这个getScreenshot
方法,并传递了结果名
public class Base {
public WebDriver InitializeDr() throws IOException {
//Creating the method to take screenshot in the Base class
public void getScreenshot(String result) throws IOException
{
File dest=new File("D:\\Work\\KT\\Scr Shot\\"+result+"test.png");
File src=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, dest);
}
}
}
你能提供不工作的代码和详细说明什么是“不工作的”吗?否则就没那么容易帮助了。@Frank如果你知道代码,那就分享它,帮我解决这个问题。你能提供不工作的代码和详细的描述什么是“不工作的”吗?否则就没那么容易帮助了。@Frank如果你知道代码,请分享它,并帮助我解决这个问题。屏幕截图问题已解决,但现在我想在ReportNG生成的报告中添加失败的方法屏幕截图…这意味着在HTML reportYes中,这意味着我们在testNG中生成报告,在testNG中我们可以选择在ReportNG jar文件的帮助下生成可自行配置的HTML格式报告。为此,您需要覆盖testNG的onTestFailure方法,然后需要将该类添加到testNG.xml中的侦听器中。。更多详情:-屏幕截图问题已解决,但现在我想在ReportNG生成的报告中添加失败的方法屏幕截图…这意味着在HTML reportYes中,这意味着我们在testNG中生成报告,在testNG中我们可以选择在ReportNG jar文件的帮助下生成可自行配置的HTML格式报告。为此,您需要覆盖testNG的onTestFailure方法,然后需要将该类添加到testNG.xml中的侦听器中。。有关详细信息:-