Groovy: SoapUI: SoapUiTools.groovy

package com.gemalto.mcx.groovy

import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.FileNotFoundException
import java.io.IOException
import java.util.Date

class SoapUiTools {

    def log
    def testRunner
    
    def SoapUiTools(log, testRunner){
        //pass testRunner and Log to assign properties
        this.log = log
        this.testRunner = testRunner
    }
    
    def Logging(log){
        this.log = log
        log.info("Is it working?")
                
    }
    
    def Log_TestStep_Messages(){
        def filePath = testRunner.testCase.testSuite.project.getPropertyValue("pathLogs")

//        def startDate = new Date()
//        thisDate = startDate.format("yyyy_MMdd'_'HHmmss_SSS")
        //log.info filePath + thisDate
        
        def thisDate = new Date().format("yyyy_MMdd'_'HHmmss_SSS")
        log.info testRunner.testStepResult.testStep.label
    
        fos = new FileOutputStream( filePath + thisDate + "-step-" +  testRunner.testStepResult.testStep.label + '.txt', true )
        pw = new PrintWriter( fos )
        testRunner.testStepResult.writeTo( pw )
        pw.close()
        fos.close()
    }

    /*
     * Read SoapUI Property File and push to TestSuite
     *   pending: pass project/suite/case/step for custom targeting
     */
    def PropertyFile_Read(thisList){
        // open file and create row token
                def pathList = testRunner.testCase.testSuite.project.getPropertyValue("pathList")
        FileInputStream fis = new FileInputStream("""${pathList}\\$thisList""")
        DataInputStream dis = new DataInputStream(fis)
        BufferedReader br = new BufferedReader(new InputStreamReader(dis))
        def strLine;
        while ((strLine = br.readLine()) != null)
        {
            def propValues = strLine.tokenize('=')
            log.info propValues[0] + " = " + propValues[1]
            testRunner.testCase.testSuite.setPropertyValue( propValues[0], propValues[1] )
        }
        dis.close();
    }
    
    def PropertyFile_Write(def itemType="TestSuite"){
        // open a file handle then clear the file if it exists
                def pathList = testRunner.testCase.testSuite.project.getPropertyValue("pathList")
        File file = new File("""${pathList}\\prop2.list""")
        file.delete()
        
        
        switch(itemType){
            case "Project": //PENDING============================================
                for (def thisObj in testRunner.testCase.testSuite.project.properties){
                    def thisProperty = thisObj.getKey()
                    def thisValue = testRunner.testCase.testSuite.project.getPropertyValue( thisObj.getKey() )
                    //log.info "${thisProperty}=${thisValue}"
                    file << ("${thisProperty}=${thisValue}\r\n")
                }
                break    
            case "TestSuite":
                // loop through the properties list for the given item
                for (def thisObj in testRunner.testCase.testSuite.properties){
                    def thisProperty = thisObj.getKey()
                    def thisValue = testRunner.testCase.testSuite.getPropertyValue( thisObj.getKey() )
                    //log.info "${thisProperty}=${thisValue}"
                    file << ("${thisProperty}=${thisValue}\r\n")
                }
                break
            case "TestCase": //PENDING============================================
                // loop through the properties list for the given item
                for (def thisObj in testRunner.testCase.properties){
                    def thisProperty = thisObj.getKey()
                    def thisValue = testRunner.testCase.getPropertyValue( thisObj.getKey() )
                    //log.info "${thisProperty}=${thisValue}"
                    file << ("${thisProperty}=${thisValue}\r\n")
                }
                break
            case "TestStep": //PENDING============================================
                // loop through the properties list for the given item
/*
                    for (def thisObj in testRunner.context.getCurrentStep().getLabel()
                    
                    .properties){
                    
                    def thisProperty = thisObj.getKey()
                    def thisValue = testRunner.context.getCurrentStep().getLabel()
                    
                    .getPropertyValue( thisObj.getKey() )
                    
                    //log.info "${thisProperty}=${thisValue}"
                    file << ("${thisProperty}=${thisValue}\r\n")
                    }
*/
                break
            default:
                break
        }
    }

    /*
     * Read SoapUI Test Suite Property File and push to TestSuite
         * pending: pull in project at same time? new routine to manually upate all?
     */
    def PropertyFile_Update(){
            
        // open file and create row token
                def pathList = testRunner.testCase.testSuite.project.getPropertyValue("pathList")
        FileInputStream fis = new FileInputStream("""${pathList}""")
        DataInputStream dis = new DataInputStream(fis)
        BufferedReader br = new BufferedReader(new InputStreamReader(dis))
        def strLine;
        while ((strLine = br.readLine()) != null)
        {
            def propValues = strLine.tokenize('=')
            log.info propValues[0] + " = " + propValues[1]
            testRunner.testCase.testSuite.setPropertyValue( propValues[0], propValues[1] )
        }
        dis.close();
    }    
       
    def PropertyList_Update_TxId(){
        def transId = testRunner.testCase.testSuite.getPropertyValue("M-MerchantTransactionId").toInteger()
        transId = transId + 1
        testRunner.testCase.testSuite.setPropertyValue( "M-MerchantTransactionId", transId.toString() )
    }
    
    def PropertyList_Update_DateTime(){
        def myDate = new Date().format("yyyyMMdd")
        log.info("Date: " + myDate)
        testRunner.testCase.testSuite.setPropertyValue("M-Date", myDate);
        
        def myTime = new Date().format("HHmmss")
        log.info("Time: " + myTime)
        testRunner.testCase.testSuite.setPropertyValue("M-Time", myTime);
    }
}

Tags