0

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.

0

Yes, You can.

  • But to achieve this you need to declare variable as a global
  • Good practice will be if you could initialize when you are initializing all you driver and system details
0

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;

        ...
    }
    ...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.