Is it possible to get a reference of the caller Object from ITestResult

79 views
Skip to first unread message

Venkatesh Singh

unread,
Sep 9, 2020, 10:53:09 AM9/9/20
to testng-users
Is it possible to get a reference of the caller Object from ITestResult ?

My use case is I want to get the reference of TestClass Object and in turn use it to retrieve  the Driver Associated with that Test class.

This is what I was trying but it holds only the name of SuperClass and not the Object ref.
I also see that there is a  Cast API but it expects the object as a parameter and won't be useful in this case.

Any pointers ?

public synchronized void onTestFailure(ITestResult result) {

 result.getTestClass().getRealClass().getSuperclass();

}


what I want to achieve, take screenshot  based on the WebDriver that is used by Test Class.

public synchronized void onTestFailure(ITestResult result) {

 Superclass a = result.getTestClass().getRealClass().getSuperclass();
//dummy code 
WebDriver webDriver =a.getWebdriver()
File scrFile = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);

}

⇜Krishnan Mahadevan⇝

unread,
Sep 9, 2020, 11:40:43 AM9/9/20
to testng-users
You can do something like this 

Object instance = result.getInstance(); // Here result is of type ITestResult
Class<?> cls = instance.getClass();


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 "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/f345a86f-2514-4909-86aa-f00995b64022n%40googlegroups.com.

Venkatesh Singh

unread,
Sep 9, 2020, 2:20:37 PM9/9/20
to testng-users
Thanks Krishnan for replying, How do I cast Class<?> cls  to my TestClass obj ref. I want to call methods from my Test Class Obj .

Venkatesh Singh

unread,
Sep 9, 2020, 2:39:54 PM9/9/20
to testng-users
Ok, If I am right it works based on Reflection. 

This is what I tried and it fetched me the value

Object instance = result.getInstance();

Class<?> cls = instance.getClass();

Field[]fl =cls.getFields();
for(Field f:fl){
try {
System.out.println(f.get(result.getInstance()));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}

Thanks Krishnan, I will explore more on it .

⇜Krishnan Mahadevan⇝

unread,
Sep 9, 2020, 3:21:10 PM9/9/20
to testng-users
You dont need to be doing all that.

You can do something like this:

  1. Define a custom interface which has methods which can be invoked to retrieve the data that you want from your test class.
  2. Have your test class implement this interface defined in (1)
  3. Now you can check if the instance that you retrieved via ITestResult.getInstance() is an instance of the interface that you created from (1)
  4. If (3) is true, then you do a cast to the interface and then invoke the data that you need via the interface.
This is a lot more clearer than relying on reflection.


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/

Venkatesh Singh

unread,
Sep 10, 2020, 2:44:35 AM9/10/20
to testng-users
I was doing same what you suggested  only change I was casting to a super class rather than an Interface.
Problem is Casting was failing.


I tried this , gives incompatible error

Object instance = result.getInstance();
Class<?> cls = instance.getClass();

SampleExtend sE = (SampleExtend)cls;

Also tried , this expects actual Object to be passed in as argument rather than a class or Interface.

Object instance = result.getInstance();
Class<?> cls = instance.getClass();
cls.cast(SampleExtend);

⇜Krishnan Mahadevan⇝

unread,
Sep 10, 2020, 5:21:01 AM9/10/20
to testng-users
You should be casting the Object instance to your interface and not try to extract the class and then do the casting.


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/

Reply all
Reply to author
Forward
0 new messages