How to pass data from TestNG tests to @AfterMethod annotation?

108 views
Skip to first unread message

mycheap...@gmail.com

unread,
Dec 21, 2021, 3:27:57 PM12/21/21
to Selenium Users
Hello,

Public class BaseTest{

String name = null;

int ID= 0;


@AfterMethod

Public void printNameID(String orgName, int newID){

name = orgName  ;

ID = newID;

System.out.println(name + ID);

}



}


Public class Test1 extends BaseTest{

String orgName = “xyz”;


@TestNG

Public void test1(){

int ID = 1;

}



@TestNG

Public void test2(){

int ID = 2;

}

}



After I run my tests, I would like to see following in console:


xyz1

Xyz2


I know my codes are totally wrong. I am not sure what parameter type to use to pass name and ID to @aftermethod annotation. How can i pass ID and orgName from TestNG tests to @AfterMethod annotation?


Thanks!


⇜Krishnan Mahadevan⇝

unread,
Dec 22, 2021, 2:07:05 AM12/22/21
to Selenium Users
Without going into the details of what exactly you are trying to do, I would suggest that you add your variables as attributes to the ITestResult object from within the "@Test" method (You can get hold of the current test methods result object by invoking Reporter.getCurrentTestResult()) and then from within your "@AfterMethod"  you retrieve the ITestResult object via native injection (See here https://testng.org/doc/documentation-main.html#native-dependency-injection ) you retrieve those added values by popping out those attributes.

PS: This is really a TestNG question and not related to Selenium. Please help keep this forum relevant by posting questions that are ONLY related to Selenium. 

For TestNG queries, you can always post them on the TestNG google forums https://groups.google.com/g/testng-users


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/
My Technical Scribblings @ https://rationaleemotions.com/


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/364356cb-1ef0-451c-b961-d2ef5d3cd143n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages