Forum Discussion

Stereo5677's avatar
Stereo5677
Occasional Contributor
2 years ago

Is there a way to store a global or project property inside a virtualized service response script?

I have a virtualized service that is running a script for it's response. Is there a way that I can store properties in that script so that they would be accessible inside groovy scripts inside the testing portion of the project instead of the virtualization portion? The issue I was running into was that testRunner is not accessible there. Normally that is what I would use to save properties.

  • groovyguy's avatar
    groovyguy
    2 years ago

    Not a problem, I really appreciate the chance to help. I love learning, because who knows? I might need to do the same thing down the road.

    I have a sample groovy script that might work.

     

    // Assuming you know how to get the data from the mock Request to save.
    def propertyValue = "This is a test"
    
    // Gets the project we're working in
    def project = context.mockService.project
    
    def testSuite = project.getTestSuiteByName("Test Suite Name")
    
    def testCase = testSuite.getTestCaseByname("Test Case Name")
    
    def testStep = testCasee.getTestStepByName("Test Step Name")
    
    testStep.setPropertyValue("PropertyName", propertyValue)
  • groovyguy's avatar
    groovyguy
    Community Hero

    Depending on how you store that data in the script, if you read it from / wrote it to a properties test step, maybe that might work?

  • Stereo5677's avatar
    Stereo5677
    Occasional Contributor

    I can't write it to the test step because as far as I know you don't have access to the testRunner object inside of scripts that are written inside of a virtual API.

    • groovyguy's avatar
      groovyguy
      Community Hero

      Just to clarify, do you need the virtual service script to write data to a test step, or read from it? Not that it much matters, but I can try to see what I can figure out.

  • Stereo5677's avatar
    Stereo5677
    Occasional Contributor

    I need to write the data from the virtual service to the test step.

    Thanks for the help.

    • groovyguy's avatar
      groovyguy
      Community Hero

      Not a problem, I really appreciate the chance to help. I love learning, because who knows? I might need to do the same thing down the road.

      I have a sample groovy script that might work.

       

      // Assuming you know how to get the data from the mock Request to save.
      def propertyValue = "This is a test"
      
      // Gets the project we're working in
      def project = context.mockService.project
      
      def testSuite = project.getTestSuiteByName("Test Suite Name")
      
      def testCase = testSuite.getTestCaseByname("Test Case Name")
      
      def testStep = testCasee.getTestStepByName("Test Step Name")
      
      testStep.setPropertyValue("PropertyName", propertyValue)
  • Stereo5677's avatar
    Stereo5677
    Occasional Contributor

    Thank you. That worked perfectly. Would you happen to have any idea why that script would fail when it is running on the cloud?

  • groovyguy's avatar
    groovyguy
    Community Hero

    Not off of the top of my head, no. Are you running it headless in the cloud??

     

  • Stereo5677's avatar
    Stereo5677
    Occasional Contributor

    I am running it the software locally and trying to hit the deployed virtualized service. It will run headless in the future in a Jenkins pipeline, but we were running into errors before that.

    • groovyguy's avatar
      groovyguy
      Community Hero

      Gotcha, I wasn't certain what you meant in regards to "running in the cloud." That said, I still do not have a good idea of why it would error. What are you seeing as an error message when ran that way??