Forum Discussion

SanthoshShetty's avatar
SanthoshShetty
Occasional Contributor
6 years ago

Trigger REST end point from an groovy script

Hello,

 

We have a requirement in our project to fetch an id from a REST end point and that would be stored in the DB. 

We have to do this in Before Run event and we are using ProjectListener.beforerun event for it. I tried but not able to proceed further.Can some one please help me in implementing this, that is calling the REST end point (it is defined in a project in a separate testsuite) from the groovy script in BeforeRun event.

 

I can maintain the REST end point in the project or it is even ok to construct the end point and hit the service in the BeforeRun event groovy script.

 

So the final flow will be, first it comes to the BeforeRun event hits the end point and fetches the id and the same will be inserted as a record in DB and then BeforeSuite, BeforeTestCase, BeforeStep and the actual teststeps executions goes.

 

Thanks alot.

2 Replies

  • aaronpliu's avatar
    aaronpliu
    Frequent Contributor

    Hi SanthoshShetty,

     

    Can you please provide more details to describe your questions? for instance, can show an endpoint here and let us know what id you would like to fetch.

    As you request, it seems need to construct sql statement to connect DB. Anyway, it should be simple to query DB via groovy.

    Not sure how you process endpoint in project level? fetch all endpoints under project? or you'd like to get endpoint under test suites for each test case / test step?

     

    Fetch endpoints for each test suite - test case - test step (Rest request)

    project.getTestSuiteList().each{testsuite ->
    	testsuite.getTestCaseList().each{testcase ->
    		testcase.getTestStepsOfType(com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep.class).each{teststep ->
    			log.info teststep.testRequest.getEndpoint()
    			}
    		}
    }

    fetch all endpoints under project

    project.getInterfaceList().each{it.getEndpoints().each{log.info it}}

    Thanks

    /Aaron

    • SanthoshShetty's avatar
      SanthoshShetty
      Occasional Contributor

      Hi Aaron,

       

      Thanks alot for your response. 

       

      End point looks like below:

      https://projectURL01:9002/webservices/website/public/releaseId

       

      I have the project structure as like below.

       

      Project -> TestSuite ->TestCase ->RestEndpoint (to fetch the id)

       

      Before the execution is triggered, I have to insert a record into the DB and for that I neeed an id and that I will get by hitting the above end point. Connecting to the DB and inserting the record and all that am able to do, but am not knowing how to hit the end point from the BeforeRun event. Thanks alot that your response has the details to fetch the rest end point in test suite-test case, can you please tell me, after reading the end point how it can be triggered. So that I will read the response and capture the id details and will set in a global property.

       

      Then after the other execution continues, that is BeforeRun, BeforeTestSuite, BeforeTestCase and the steps in the testcase.

       

      Even am ok, instead of maintaining a test case with end point to fetch id details , if I can create an end point in the BeforeRun Event and hit the service and read the id details,  

       

      Thank you.