I have a situation where 'xyz' is member variable of my testClass and I want to print some information in my report using that variable only.
So I want to access 'xyz' in my 'onTestFailure' listner method. Please suggest if anyone has attempted it.
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It only takes a minute to sign up.
Sign up to join this communityYes, You can.
In your test class, declare the variable public
.
In your listener, the onTestFailure
method receives a result
parameter. That result
parameter knows the instance of the test class that threw the failure. Call the result
's getInstance
method to get the instance of the test class.
To get access to the variable, first cast the instance to the right type (the type that declares the variable or method you want to access). Then fetch the variable from the instance.
Something like this:
public MyTestListener implements ITestListener {
...
public void onTestFailure(ITestResult result) {
Object instance = result.getInstance();
MyXYZVariableType xyz = MyTestClass.class.cast(instance).xyz;
...
}
...
}