Groovy解析xml并且注入Project,TestSuite,TestCase级别的custom properties



import com.eviware.soapui.support.GroovyUtils
import groovy.util.XmlParser

def groovyUtils = new GroovyUtils( context )
def xmlFilePath = groovyUtils.getProjectPath()+"\\Properties.xml"

def testAndBmkEnv = context.expand('${#Project#testAndBmkEnv}').trim()
String testEnv = testAndBmkEnv.split("vs")[0].trim()
String bmkEnv = testAndBmkEnv.split("vs")[1].trim()

//loadProperties(testEnv,bmkEnv,xmlFilePath)
//loadProperties(testEnv,bmkEnv,xmlFilePath,testSuiteName,testCaseName)

//******************************All TestSuies and TestCases**********************************
// Inject custom properties in xml file to Project, all TestSuites or all TestCases.
def loadProperties(String testEnv, String bmkEnv, String xmlFilePath){
    def xmlParser = new XmlParser().parse(xmlFilePath);        
    xmlParser.Env.each{        
        if (it.attribute("id")==testEnv){
            setProjectProperties(it, "Test")
            setTestSuiteAndTestCaseProperties(it, "Test")
        }
        if (it.attribute("id")==bmkEnv){
            setProjectProperties(it, "Bmk")
            setTestSuiteAndTestCaseProperties(it, "Bmk")
        }        
     }
}

def setProjectProperties(Object obj, String env){        
    obj.Project.each{            
        it.CusProperty.each{            
            String propertyName = it.attribute("name")+env
            String propertyValue = it.attribute("value")            
            testRunner.testCase.testSuite.project.setPropertyValue(propertyName,propertyValue)            
        }
    }
}

// Set custom properties of all TestSuites and TestCases
def setTestSuiteAndTestCaseProperties(Object obj, String env){        
    obj.TestSuite.each{
        String testSuiteName = it.attribute("name")
        it.CusProperty.each{
            String propertyName = it.attribute("name")+env
            String propertyValue = it.attribute("value")
            testRunner.testCase.testSuite.project.getTestSuiteByName(testSuiteName).setPropertyValue(propertyName,propertyValue)
        }
        it.TestCase.each{
            String testCaseName = it.attribute("name")            
            it.CusProperty.each{
                String propertyName = it.attribute("name")+env
                 String propertyValue = it.attribute("value")
                 testRunner.testCase.testSuite.project.getTestSuiteByName(testSuiteName).getTestCaseByName(testCaseName).setPropertyValue(propertyName,propertyValue)
            }
        }
    }
}
//******************************All TestSuies and All TestCases**********************************

//******************************One TestSuie and One/All TestCases**********************************
// Inject custom properties in xml file to Project, one TestSuite or one TestCase.
def loadProperties(String testEnv, String bmkEnv, String xmlFilePath, String suiteName, String caseName){
    def xmlParser = new XmlParser().parse(xmlFilePath);        
    xmlParser.Env.each{        
        if (it.attribute("id")==testEnv){
            setProjectProperties(it, "Test")
            setTestSuiteAndTestCaseProperties(it, "Test", suiteName, caseName)
        }
        if (it.attribute("id")==bmkEnv){
            setProjectProperties(it, "Bmk")
            setTestSuiteAndTestCaseProperties(it, "Bmk", suiteName, caseName)
        }        
     }
}

// Set custom properties of one TestSuite and one TestCase
def setTestSuiteAndTestCaseProperties(Object obj, String env, String suiteName, String caseName){        
    obj.TestSuite.each{
        String testSuiteName = it.attribute("name")
        if (testSuiteName==suiteName){            
            it.CusProperty.each{
                String propertyName = it.attribute("name")+env
                String propertyValue = it.attribute("value")
                testRunner.testCase.testSuite.project.getTestSuiteByName(testSuiteName).setPropertyValue(propertyName,propertyValue)
            }
            if(caseName!=""){
                it.TestCase.each{
                    String testCaseName = it.attribute("name")
                    if (testCaseName==caseName){            
                        it.CusProperty.each{
                            String propertyName = it.attribute("name")+env
                             String propertyValue = it.attribute("value")
                             testRunner.testCase.testSuite.project.getTestSuiteByName(testSuiteName).getTestCaseByName(testCaseName).setPropertyValue(propertyName,propertyValue)
                        }
                        return
                    }
                }
            }else{
                it.TestCase.each{
                    String testCaseName = it.attribute("name")
                    it.CusProperty.each{
                        String propertyName = it.attribute("name")+env
                         String propertyValue = it.attribute("value")
                         testRunner.testCase.testSuite.project.getTestSuiteByName(testSuiteName).getTestCaseByName(testCaseName).setPropertyValue(propertyName,propertyValue)
                    }
                }
            }
            return
        }
    }
}
//******************************One TestSuie and One/All TestCases**********************************
 
  

 

 
 

 

转载于:https://www.cnblogs.com/testing-life/p/5016787.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值