Forum Discussion

jbiss's avatar
jbiss
Occasional Contributor
9 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 -...
  • nmrao's avatar
    nmrao
    9 years ago

    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