2012-06-20 13 views
6

Sto cercando un modo per allegare uno screenshot alla sezione Risultati del report TestNG per i metodi falliti.Allegare schermate a TestNG Risultati metodi non riusciti

Finora sono stato in grado di attache miei screenshot di Reporter di uscita mediante l'attuazione di questo:

Reporter.log("<br> <img src=.\\screenshots\\" + fileName + " /> <br>");

ma ancora alle prese con l'aggiunta di loro di testare sezione Risultati di metodi falliti.

sono stato in grado di implementare ascoltatore e intercetta onTestFailure azioni che è stato originariamente suggerito qui: How can I include a failure screenshot to the testNG report

Ecco un esempio di che:

@Override 
public void onTestFailure(ITestResult result) { 
    Reporter.setCurrentTestResult(result); 
    Reporter.log("<br> <img src=.\\screenshots\\Untitled.png /> <br>"); 
    Reporter.setCurrentTestResult(null); 
} 

Ma Reporter.log funzione spinge ancora il mio informazioni in il registro di output di Reporter ma non nel Results-> Failed methods-> Failed log dei metodi.

Aggiornamento (14/03/14): Ho allegato lo screenshot per chiarire la mia domanda. Il problema non è catturare lo screenshot e collegarlo a Report. Quella parte funziona bene. Il problema è che lo screenshot è collegato alla parte Test Output del report ma voglio vederlo in Results -> Failed Methods.

enter image description here

risposta

0

ho anche implementato lo stesso estendentesi TestNG TestListenerAdapter. Catturando lo screenshot, collegalo al rapporto Testng con l'immagine di altezza = 100 e larghezza = 100 con onTestFailure. Si prega di vedere di seguito se questo aiuta a risolvere il problema

File scrFile = ((TakesScreenshot) WebdriverManager.globalDriverInstance).getScreenshotAs(OutputType.FILE); 
     //Needs Commons IO library 
     try { 
     FileUtils.copyFile(scrFile, new File(file.getAbsolutePath()+ "/selenium-reports/html/" + result.getName() + ".jpg")); 
     Reporter.log("<a href='"+ file.getAbsolutePath()+"/selenium-reports/html/" + result.getName() + ".jpg'> <img src='"+ file.getAbsolutePath()+"/selenium-reports/html/"+ result.getName() + ".jpg' height='100' width='100'/> </a>"); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    Reporter.setCurrentTestResult(null); 
+0

Hi Satish, grazie per la risposta ma credo stava facendo una domanda diversa. Ho modificato il mio commento originale per chiarire il mio problema. – Vlad

0

Oltre sopra non dimenticare di aggiungere seguenti righe al xml suite di TestNG

<listeners> 
    <listener class-name="com.tests.DotTestListener" /> 
</listeners> 

O

passando come listner parametro se lo si sta eseguendo dalla riga di comando

java -classpath testng.jar;%CLASSPATH% org.testng.TestNG -listener com.tests.DotTestListener test\testng.xml 

Riferimento: http://testng.org/doc/documentation-main.html#logging-listeners

+0

Grazie per la risposta Kamal. Ho modificato la mia domanda con maggiori dettagli e screenshot per illustrare il mio problema. – Vlad

0

Ho avuto lo stesso problema ma è stato risolto. Con l'implementazione di SuitePanel si sarà in grado di aggiungere screenshot come si desidera https://github.com/cbeust/testng/blob/master/src/main/java/org/testng/reporters/jq/SuitePanel.java

io quasi non cambiano il codice originale tranne

... 
// Description? 
String description = tr.getMethod().getDescription(); 
if (! Strings.isNullOrEmpty(description)) { 
    xsb.push("em"); 
    xsb.addString("(" + description + ")"); 
    xsb.pop("em"); 
} 
// Add screen shot here 
xsb.push(“img”,”src”,imagePath); 
xsb.pop(“img”); 

xsb.pop(D); 
xsb.pop(D); 
0

Se si desidera visualizzare la schermata sul metodo fallito allora si dovrà catturare l'eccezione e modificare il contenuto del messaggio e aggiungere img html a quello e poi apparirà. Fatemi sapere se avete bisogno di un esempio

0

Ho avuto lo stesso problema ma è stato risolto ora. Ho creato un metodo per catturare lo screenshot nella classe base. questo metodo restituisce il percorso completo dello screenshot.

public String getScreenshot (String screenshotName, WebDriver driver) throws IOException{ 
    DateFormat dateformate = new SimpleDateFormat("dd-mm-yy-hh-mm-ss"); 
    Date date = new Date(); 
    String currentdate = dateformate.format(date); 
    String imageName =screenshotName+currentdate; 
    TakesScreenshot ts=(TakesScreenshot)driver; 
    File source=ts.getScreenshotAs(OutputType.FILE); 
    String location =System.getProperty("user.dir")+"\\testOutput\\screenshot\\"+imageName+".png"; 
    File screenshotLocation =new File (location); 
    FileUtils.copyFile(source, screenshotLocation); 
    return location; 

} 

Aggiungere utilizzare questo percorso per aggiungere screenshot nella relazione TestNG così come registro per i rapporti con l'aggiornamento TestNG TestNgListener-

public void onTestFailure(ITestResult arg0) { 
    Object currentClass = arg0.getInstance(); 
    WebDriver driver = ((BrowserSetup) currentClass).getDriver(); 
    String name = arg0.getName(); 
    System.out.println(name); 
    try { 
     String screenshotPath =getScreenshot(name, driver); 
     System.out.println("Screenshot taken"); 
     String path = "<img src=\"file://" + screenshotPath + "\" alt=\"\"/>"; 
     System.out.println(screenshotPath+" and path - "+path); 
     Reporter.log("Capcher screenshot path is "+path); 
    } catch (Exception e) { 
     System.out.println("Exception while takescreenshot "+e.getMessage()); 
    } 
    printTestResults(arg0); 

} 

enter image description here

Problemi correlati