Forum Discussion

GMSoapUI's avatar
GMSoapUI
Contributor
12 years ago

[Res]Accessing project variable from TestS..Listner.afterRun

Hello,

In a Groovy script I have created a project level variable. I am then trying to access this variable from within a TestSuiteRunListner.afterRun event listener. It works in all the other listeners I'm using but does not work in this particular one and I'm baffled as to why.

Here is an abstract of the listener code:


def vntProject = testCase.testSuite.project;
...
log.info( "MESSAGE: " + vntProject.getPropertyValue( "myProjectLevelVariable" ) );


Could somone advise what is awry with the code or how I might access the variable from within this particular listener? As I mentioned this code works within the two other listeners I have, namely:

TestRunListener.afterStep
TestSuiteRunListener.beforeTestCase

Is this a context issue perhaps?

I have tried:


def vntProject = testRunner.testCase.testSuite.project;


and


def vntProject = testSuite.project;


But nothing will let me access the variable's value.

The error I get is:


Thu Jan 30 15:35:02 GMT 2014:ERROR:groovy.lang.MissingPropertyException: No such property: testCase for class: Script20
  • Hi,

    It doesn't work. I still get:


    Thu Jan 30 17:35:58 GMT 2014:ERROR:groovy.lang.MissingPropertyException: No such property: testCase for class: com.eviware.soapui.impl.wsdl.testcase.WsdlTestSuiteRunner


    I suspect this is due to it being AFTER the run and therefore testCase has no context or relevancy since their are no test cases due to it being AFTER the test run.

    If it is not due to this then there is a bug I think.

    I really need to be able to access these variable values within this listener for logging and reporting purposes.

    Gary.
  • Hi,

    I am not able to reproduce this issue, I have attached a project, I have create an empty project with "test" project property and an event handler "TestRunListener.afterStep" and added the same script I referred to you,

    def projectProperty = testRunner.testCase.testSuite.project.getPropertyValue( "test" )
    log.info "MESSAGE: " + projectProperty


    And I have a testcase, when I run the test case I dont see that error you are getting.

    If you add this to testSuiteRunlistener it will run only when you run the TestSuite.

    Attached is the project.

    Thanks,
    Jeshtha
  • I can see your example working but where yours differs from mine is that I am using setPropertyValue to set the value at runtime in another Groovy script like this:


    def vntProject = testRunner.testCase.testSuite.project;
    ...
    vntProject.setPropertyValue( "myProjectLevelVariable", "Hello world!" );


    In my listener I am then using:


    ...
    log.info( "MESSAGE: " + vntProject.getPropertyValue( "myProjectLevelVariable" ) );
    ...


    In the other two listeners (TestRunListener.afterStep and TestSuiteRunListener.beforeTestCase) this works perfectly and writes MESSAGE: Hello world! to the log. This does not work in the TestSuiteRunListner.afterRun listener. It doesn't matter whether I use long-hand or short-hand notation - I get the same error everytime.


    ERROR:groovy.lang.MissingPropertyException: No such property: testCase for class


    If I comment the def vntProject = testRunner.testCase.testSuite.project; the error is no longer present.

    I think this must be to do with context or scope because the event listener is TestSuiteRunListner.afterRun then I think that there is no context for testRunner / testCase / testSuite. I really need to resolve this so if this could be investigated to determine if this is a defect that would be great.
  • Hi,

    In TestSuiteRunListner.afterRun event handler try this script,

    def projectProperty = testRunner.testSuite.project.getPropertyValue( "test" )
    log.info "MESSAGE: " + projectProperty


    It is the way API is designed or each event it is not a defect.

    Thanks,
    Jeshtha
  • Hi,

    Perhaps I'm not explaining myself properly. I have tried the way you suggest as well and I still get the same error.

    I still believe this is to do with scope/context.

    The code does not work:


    ERROR:groovy.lang.MissingPropertyException: No such property: testCase for class


    If I comment out:


    def projectProperty = testRunner.testSuite.project.getPropertyValue( "test" )


    The error does not appear. There is either a defect or it is due to scope/context.
  • Hi,

    I am sorry I should have explained it better but I think you did not update the script,

    Your script is,
    def vntProject = testRunner.testCase.testSuite.project

    Update this to,
    def vntProject = testRunner.testSuite.project;

    The way API is designed, the script changes on each level like for TestCase, TestSuite or project. So as you say the context changes on each level.
    Here are some groovy script tips and tricks,
    http://www.soapui.org/Scripting-Properties/tips-a-tricks.html
    http://www.soapui.org/apidocs/overview-summary.html

    Thanks,
    Jeshtha
  • Hi,

    PERFECT! Please excuse me for not having spotted the omission of testCase sooner. This now works as I expect. Thank you for your help, please mark this as resolved.

    Regards.