Issue with Listeners while running TestNG through Command line

515 views
Skip to first unread message

Sella ra

unread,
Nov 29, 2010, 9:35:37 AM11/29/10
to testng...@googlegroups.com
Hi,


In my script,I capture screenshots for failed testcases using onTestFailure method.

It is working perfectly fine when I run testng.xml file as TestNG Suite in Eclipse.

But when I run it using a batch file with the command

@echo off
java -classpath <all the dependant jars and scripts> org.testng.TestNG testng.xml


The test results are updated properly but the screenshot is not referenced properly.
Suppose,
Test1 and Test2 fails,Test2 contains the screenshot of Test1.


I capture screenshot of all the testcases in tearDownTest and in OnTestFailure method,delete the screenshot if the testcase is pass and rename it to result.getName() in case of testcase failure.
The screenshots of the other/previous testcase is assigned to the current testcase.
Also,result.getName is returning something like 'TestScript1 on instance null(com.aol.Test1.MainScript)'

The listener doesn't work properly.

Please help me with this.

It is very urgent.


Thanks.


Cédric Beust ♔

unread,
Nov 29, 2010, 10:40:25 AM11/29/10
to testng...@googlegroups.com
How are you specifying your listenener when you launch from the command line?

-- 
Cédric




--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.



--
Cédric


Sella ra

unread,
Nov 29, 2010, 12:13:15 PM11/29/10
to testng...@googlegroups.com
Hi,

I am specifying the listener using

<listeners>
    <listener class-name="com.aol.Test1.Screenshot"/>
    </listeners>

in the testng.xml file

Screenshot extends TestListenerAdapter and has the OnTestFailure method which has the code to rename the file to result.getname() in case of Failure.


Please help me.


Thanks.


2010/11/29 Cédric Beust ♔ <ced...@beust.com>

Cédric Beust ♔

unread,
Nov 29, 2010, 12:24:27 PM11/29/10
to testng...@googlegroups.com
Can you post the code in your listener that takes the screen shot and saves it? I suspect a bug there (probably not in TestNG).

-- 
Cédric


On Mon, Nov 29, 2010 at 6:35 AM, Sella ra <sella...@gmail.com> wrote:

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.



--
Cédric


Sella ra

unread,
Nov 29, 2010, 12:33:13 PM11/29/10
to testng...@googlegroups.com
Hi,

Please find below the code


package com.aol.Test1;


import java.io.File;


import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestListenerAdapter;


public class Screenshot extends TestListenerAdapter {
       
    @Override
       public void onTestFailure(ITestResult result) {
       
       
        // Tear down method saved the screenshot in a temporary location
        // Rename the file to specific test case file name
       
        File file = new File("");
        String tempFileName = file.getAbsolutePath() + "\\reports\\temp.jpg";
        String newFileName = file.getAbsolutePath()+ "\\reports\\"+result.getName()+ ".jpg";
       
        try
        {
            // Delete if the file exists.
            File newFile = new File(newFileName);
           
            if (newFile.exists())
                newFile.delete();
           
            file = new File(tempFileName);
           
            file.renameTo(new File(newFileName));
           
            if (file.exists())
                file.delete();
        }
        catch (Exception e)
        {
       
        }
       
        
        Reporter.setCurrentTestResult(result);
       
        Reporter.log("screenshot saved at "+ newFileName);
        Reporter.log("<a href='../reports/"+result.getName()+".jpg'> <img src='../reports/"+result.getName()+".jpg' height='100' width='100'/> </a>");
        //MainTestCoordinator.selenium.captureScreenshot(file.getAbsolutePath()+"\\reports\\"+result.getName()+".jpg");
       
        Reporter.setCurrentTestResult(null);
    }
   
 @Override
       public void onTestSuccess(ITestResult result) {
      // will be called after test will pass
          
          // Delete the temp file
           File file = new File("");
           String tempFileName = file.getAbsolutePath() + "\\reports\\temp.jpg";
           File file1 = new File(tempFileName);
           file1.delete();
          
       }
}


And the code to capture screenshot independant of success or failure is in the tearDownTest


 @AfterMethod(alwaysRun=true)
    public void tearDownTest()throws Exception {

        // Take screen shot independent of success/failure
        File file = new File("");
        MainTest.selenium.captureScreenshot(file.getAbsolutePath()+"\\reports\\temp.jpg");
      
         
    }


The same code is working perfectly fine while running scripts in eclipse using Testng.xml ---> Run as TestNG suite.

It is not working properly when running using command line.



Thanks lot for your help.


2010/11/29 Cédric Beust ♔ <ced...@beust.com>

Sella ra

unread,
Nov 29, 2010, 1:06:54 PM11/29/10
to testng...@googlegroups.com
Hi,


Is there any solution for this?
It would be of great if I can get help on this.


Thanks lot.

Cédric Beust ♔

unread,
Nov 29, 2010, 1:20:09 PM11/29/10
to testng...@googlegroups.com
Sella, your traces should be able to tell you the difference between running from the command line and from Eclipse.

-- 
Cédric

Sella ra

unread,
Nov 29, 2010, 1:24:28 PM11/29/10
to testng...@googlegroups.com
Hi,

Not able to find much from the logs or by printing some values or statements for debugging.

Thanks.

2010/11/29 Cédric Beust ♔ <ced...@beust.com>
Sella, your traces should be able to tell you the difference between running from the command line and from Eclipse.

Felipe Knorr Kuhn

unread,
Nov 29, 2010, 1:32:20 PM11/29/10
to testng...@googlegroups.com
How are we supposed to help, then? :)

You know your code better than anyone here.

Taking a wild guess, I would say the problem lies in @AfterMethod method.

What if you take the screenshot during the onTestFailure method?

Sella ra

unread,
Nov 29, 2010, 1:42:46 PM11/29/10
to testng...@googlegroups.com
Hi,

There is a problem with taking the screenshot using onTestFailure method.
In AfterMethod,I had provided the code related to the snapshot part.
There is also code to sgin out of the application in AfterMethod.
So,If I take screenshot in OnTestFailure,I would be getting only the Home page screenshots :-(


Is there any way to specify the listener in the command line itself?

I tried specifying using -listener Screenshot,but the script didn't get executed.

I am finding it difficult to find the cause of this issue since it works fine in Eclipse.


Thanks.

Felipe Knorr Kuhn

unread,
Nov 29, 2010, 1:50:03 PM11/29/10
to testng...@googlegroups.com
Try implementing IInvokedMethodListener2 and using afterInvocation() instead.

You will need to query result.isSucess() to decide whether or not you should take the screenshot, or save it in different directories.

FK
Reply all
Reply to author
Forward
0 new messages