Forum Discussion

jbiss's avatar
jbiss
Occasional Contributor
10 years ago

How to use method in separate groovy script within test case

My directory structure is something like as follows:

 

-testSuite

--testCase

---model (groovy script)

---view (groovy script)

---login (soap request)

---logout (soap request)

 

Essentially - is it possible to call functions (methods) defined within the 'model' script from the 'view' script? And vice versa.

The functions return and require variables to be passed, they are not just runnables. They also perform operations on the xml requests, which is why I was under the impression using the script library would not be suitable.

 

Any help or direction would be greatly appreciated. Thanks.

  • For example, the following file is present at directory ../ReadyAPI-1.4.1/bin/scripts and we see how can this be accessed in two groovy script test steps in a project

     

    Vehicle.groovy

     

    class Vehicle {
       def name
       def yearMade
       def printModelDetails(log) {
          log.info "Vehicle is ${name}, made in ${yearMade}"
       }
    }

    Model.groovy -- assume this is your groovy script test step

    import Vehicle
    
    def vehicle = new Vehicle(name:'Merk', yearMade:1975)
    vehicle.printModelDetails(log)
    //saving value of vehicle name at test case level property say VEHICLE_NAME
    context.testCase.setPropertyValue('VEHICLE_NAME', vehicle.name)

    View.groovy

    import Vehicle
    //get the previously store value from test case property

    def vehicleName = context.testCase.getPropertyValue('VEHICLE_NAME') def vehicle2 = new Vehicle(name:vehicleName)
    log.info vehicle2.name

     

    Note that all the common stuff to be present in the first groovy class that is placed in scripts directory.

    For more details see documentation

     

     

    • jbiss's avatar
      jbiss
      Occasional Contributor

      Thanks Samy for your reply. I still do not understand how I would actually call method in a separate script.

       

      In one script I have:

       

      class Script1 {
              public String hello() { 
      //Edit the local soap requests
      //Send some soap requests
      //Do some other stuff return "a result from the requests" } }

      In a the second script I want to call the method in the first script. So something like:

       

      log.info (Script1.hello())

       

      • nmrao's avatar
        nmrao
        Champion Level 3
        Do you mean to say that both model and view should interact with each other by calling others functions along with arguments?