Dataprovider and Listeners => How to distinguish each instance into the logs?

867 views
Skip to first unread message

johannga...@gmail.com

unread,
Jan 30, 2014, 7:08:28 AM1/30/14
to testng...@googlegroups.com
Hi all,

I try to make automated tests for a website (as a beginner..!), with the following configuration : 
- Eclipse SDK 4.2.1 
- Ant 1.9.2 
- selenium grid webdriver (using selenium-server-standalone-2.35.0.jar)

I am using an xls file as dataprovider in order to instantiate my tests (let's say that the method to be instanciated is called "Check")...

I have created a CustomListener class which extends TestListenerAdapter in order to log the results of my tests.

My issue is that on my tests results output html page, it is impossible to distinguish the different instances this Check method.

The output results look like this :

Check : Passed

Check : Passed

Check : Failed

Check : Passed

....

Is there any way to distinguish each Check instance? By defining a proper instance name? By catching each instance parameters? Other way?

Thanks in advance for your kind help!

Johann

Krishnan Mahadevan

unread,
Jan 30, 2014, 8:32:21 PM1/30/14
to testng...@googlegroups.com
Take a look at this post of mine and check if that helps : http://rationaleemotions.wordpress.com/2013/07/31/pretty-printing-with-testng/
--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/groups/opt_out.


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

johannga...@gmail.com

unread,
Feb 4, 2014, 11:26:48 AM2/4/14
to testng...@googlegroups.com
Hi Krishnan,

First, thank you for answering, it is the second time you help me ;-)

Actually, my question is about the logs (Reporter Output section of the report).

The objective is to display for each instance of my test (each line of a xls file) the name of the method + a couple of parameters that i would choose :
example :
Check(myenvironment1, mydata1) : Success
Check(myenvironment2, mydate2) : Failed

Now I get the following datas :
Check : Success
Check : Failed

This is particulary blocking because i would like to stock the logs of my tests in a file which get the same name as each instance.

This is a part of my test :
@DataProvider(name = "DP1")
    public Object[][] createData1() throws Exception{
        Object[][] retObjArr=getTableArray("C://Automation//Dataset//Weba//Weba.xls",
                       environment, "SEO");
        return(retObjArr);
    }

       // Beginning of the test
            @Test (dataProvider = "DP1")
            public void Check(String env, String device, String url,
                    String cookie, String clientId, String chaine) throws Exception {

                meth = Thread.currentThread().getStackTrace()[1].getMethodName();
                filename = environment + device + meth + timeStamp + ".txt";
                f = new File(filename);
             getDriver().get(url);
             Thread.sleep(3000);
             System.out.println(env + " : " + device + " => Check of Client Id and Cookie domain");          
           
             data[i]="Looking for : "+ (char)34+"cm_mode"+ ((char)34+":" +(char)34 + env +(char)34);
             assertTrue(getDriver().getPageSource().contains((char)34+"cm_mode"+ ((char)34+":" +(char)34 + env +(char)34)));

......


Here is a part of my listener class which extends TestListernerAdapter (case of sucessfull test) :
@Override
    public void onTestSuccess(ITestResult tr) {
        Reporter.log("<b><FONT COLOR='white'>"+tr.getName()+ "</font></b> : <FONT COLOR='green'>SUCCESS </font> | </font><a href=\"file:///"+ logfile + File.separator+ tr.getName() + ".txt" + "\" target='_blank'>Logs</a><FONT COLOR='white'> | </font>\n");
         
   /*    Reporter.log(tr.getParameters().toString());
        Reporter.log(tr.getInstanceName().toString());
        Reporter.log(tr.getTestName());
        Reporter.log(tr.getInstanceName());
        Reporter.log(tr.getAttributeNames().toString());*/
   }

As you can see, I tried different kind of methods but nothing works (getParameters, GetAttributeNames, etc...)

Tell me if you need more info.. and thanks again for the help!!


Le vendredi 31 janvier 2014 02:32:21 UTC+1, Krishnan a écrit :
Take a look at this post of mine and check if that helps : http://rationaleemotions.wordpress.com/2013/07/31/pretty-printing-with-testng/

On Thursday, January 30, 2014, <johannga...@gmail.com> wrote:
Hi all,

I try to make automated tests for a website (as a beginner..!), with the following configuration : 
- Eclipse SDK 4.2.1 
- Ant 1.9.2 
- selenium grid webdriver (using selenium-server-standalone-2.35.0.jar)

I am using an xls file as dataprovider in order to instantiate my tests (let's say that the method to be instanciated is called "Check")...

I have created a CustomListener class which extends TestListenerAdapter in order to log the results of my tests.

My issue is that on my tests results output html page, it is impossible to distinguish the different instances this Check method.

The output results look like this :

Check : Passed

Check : Passed

Check : Failed

Check : Passed

....

Is there any way to distinguish each Check instance? By defining a proper instance name? By catching each instance parameters? Other way?

Thanks in advance for your kind help!

Johann

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/groups/opt_out.

Krishnan Mahadevan

unread,
Feb 5, 2014, 2:43:15 AM2/5/14
to testng...@googlegroups.com
Johann,
I think it should work. It would be good if you could please assemble a simple reproducible test code that can show the problem and the expected vs actual output so that i can better understand what you are trying to accomplish.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.

johannga...@gmail.com

unread,
Feb 6, 2014, 2:57:33 AM2/6/14
to testng...@googlegroups.com
Hi Krishnan,

I hope those informations will help you to understand my issue...

This is a sample of test :

@DataProvider(name = "DP1")
    public Object[][] createData1() throws Exception{
        Object[][] retObjArr=getTableArray("C://
Automation//Dataset//Weba//Weba.xls",
                       "Sheet1", "Sentence");
        return(retObjArr);

    }


            @Test (dataProvider = "DP1")
            public void Check(String name, String place) throws Exception {
             getDriver().get("http://www.mysentence.com");
             Thread.sleep(3000);
                                    try {
             assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("Hello my name is " + name + " and I live in " + place));
             } catch (Error e) {
            verificationErrors.append(e.toString());
          }
}

And this is a sample of listener class (success case only) :

@Override
    public void onTestSuccess(ITestResult tr) {
        Reporter.log(tr.getName()+" : Success");

         
   /*    Reporter.log(tr.getParameters().toString());
        Reporter.log(tr.getInstanceName().toString());
        Reporter.log(tr.getTestName());
        Reporter.log(tr.getInstanceName());
        Reporter.log(tr.getAttributeNames().toString());
*/
   }


About my Reporter Output :
This is the expected result :
Check(johann, paris) : Success
Check(krishnan, new york) : Success

This is a the actual result :
Check : Success
Check : Success

Tell me if you need more info or screenshot!

Thks ;-)

Johann

Krishnan Mahadevan

unread,
Feb 7, 2014, 9:11:01 AM2/7/14
to testng...@googlegroups.com
Johann,

Is this what you are expecting ?

package org.rationale.emotions.testng;

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners(MyDummyClass.class)
public class MyDummyClass implements ITestListener {
    @Test(dataProvider = "dp")
    public void f(Integer n, String s) {
    }

    @DataProvider
    public Object[][] dp() {
        return new Object[][] { new Object[] { 1, "a" }, new Object[] { 2, "b" }, };
    }

    @Override
    public void onTestStart(ITestResult result) {

    }

    @Override
    public void onTestSuccess(ITestResult result) {
        StringBuffer sb = new StringBuffer();
        sb.append("Method :").append(result.getMethod().getMethodName());
        sb.append("[");
        for (Object eachObject : result.getParameters()) {
            sb.append(eachObject).append(",");
        }
        sb.append("]");
        Reporter.log(sb.toString(), true);
    }

    @Override
    public void onTestFailure(ITestResult result) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTestSkipped(ITestResult result) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStart(ITestContext context) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFinish(ITestContext context) {
        // TODO Auto-generated method stub

    }
}



Output : 

...
... TestNG 6.8.6 by C?dric Beust (ced...@beust.com)
...

[TestNG] Running:
  /private/var/folders/z0/c631_ql16cbbjcq975xc2fch38xdjj/T/testng-eclipse--359850885/testng-customsuite.xml

Method :f[1,a,]
Method :f[2,b,]
PASSED: f(1, "a")
PASSED: f(2, "b")

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@17fe1feb: 6 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@7ffc6e42: 25 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@fcfa52: 7 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@a210b5b: 2 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@21b64e6a: 3 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms







Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


johannga...@gmail.com

unread,
Feb 11, 2014, 6:24:58 AM2/11/14
to testng...@googlegroups.com
Hello Krishnan,

It functions! Thanks a lot!

Now I have a new question...^^
Is that possible to display only a part of the parameters used by the method?
For example :
In place of -> Method[parameter1, parameter2, parameter3, parameter4, parameter5]

Is that possible to get the following result -> Method[parameter1, parameter2]

Thanks in advance !

Krishnan Mahadevan

unread,
Feb 11, 2014, 8:45:15 AM2/11/14
to testng...@googlegroups.com
Yes, you can. Just tweak your onTestSuccess() according to your needs and it should do the trick for you.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


Reply all
Reply to author
Forward
0 new messages