2017-09-27 58 views
0

我在我的框架中使用testng,maven,aure。目前我对失败的测试截图保存在万无一失的报告/截图为: 日期时间,类名,方法名 如:09-22-2017_01.13.23_ClassName_Methodname.png获取sscreenshot附件失败的测试引诱报告格式:datetime-classsname-testname selenium,testng,allure

下面是该代码:

@AfterMethod 
    protected void screenShotIfFail(ITestResult result) throws IOException { 
     if (!result.isSuccess()) { 
      takeScreenShot(result.getMethod()); 
     } 
    } 

private void takeScreenShot(String name) throws IOException { 
     String path = getRelativePath(name); 
     File screenShot = ((TakesScreenshot) driver) 
       .getScreenshotAs(OutputType.FILE); 
     FileUtils.copyFile(screenShot, new File(path)); 
     String filename = makeScreenShotFileName(name); 
     System.out.println("Taking Screenshot! " + filename); 
     Reporter.log("<a href=" + path + " target='_blank' >" + filename 
       + "</a>"); 
      } 
private void takeScreenShot(ITestNGMethod testMethod) throws IOException { 
     String nameScreenShot = testMethod.getTestClass().getRealClass() 
       .getSimpleName() 
       + "_" + testMethod.getMethodName(); 
     takeScreenShot(nameScreenShot); 
    } 
private String makeScreenShotFileName(String name) { 
     DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy_hh.mm.ss"); 
     Date date = new Date(); 
     return dateFormat.format(date) + "_" + name + ".png"; 
    } 
private String getRelativePath(String name) throws IOException { 
     Path path = Paths.get(".", "target", "surefire-reports", "screenShots", 
       makeScreenShotFileName(name)); 
     File directory = new File(path.toString()); 
     return directory.getCanonicalPath(); 
    } 

对于越来越重视对引诱报告,我想@Attachment这样的:

@Attachment(value = "filename", type = "image/png") 
    private byte[] takeScreenShot(String name) throws IOException { 
       return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES); 
    } 

截图被越来越重视,以引诱报告,但我怎么能得到它在相同的格式万无一失报告。

谢谢!

回答

0

可以直接通过一个自定义名称与@Attachment注解的方法:

@Attachment(value = "{name}", type = "image/png") 
private byte[] takeScreenShot(String name) throws IOException { 
    return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES); 
} 

倾城1和2倾城之间唯一的区别是在value参数。对于倾城1使用以下语法:

value = "{0}" 

哪里{0}是参考第一方法的参数(其索引)。

对于倾城2,使用以下:

value = "{name}" 

在这种情况下是{name}的方法的参数的名称。

相关问题