2016-11-30 63 views
0

我试图从mysql数据库中的表中读取一组用户定义的属性名称和值。如何从mysql表读取动态变量名称和值,以便它们可以在SOAPUI中使用请求

我在如何检索属性值并在请求负载中引用它们的groovy语法时遇到了问题。

我想要在安装脚本中定义所有动态属性并提取值,以便它们可以在项目的请求负载中动态使用。

我的表看起来喜欢这个

dynamic_variables表

variable_id |project_id | variable_name | variable_value 
    1   |126  | Make   | Porsche 
    2   |126  | Model   | 911 

样品JSON请求获取测试套件引发,并使用属性引用

{ 
    "username" : "my_username", 
"password" : "my_password", 
    "cars" : { 
    "carSpecs" : [ 
    { 
     "make" : "${make}", 
     "model" : "${model}" 
    } 
    ] 
} 
} 

现在,这里是我的脚本

//declare arrays to handle property name and values 
def dynamicVariables = [] 
def varPropNames = [] 
def varPropValues = [] 

//read all properties from table and store results in dynamicVariable array 
def sql =  Sql.newInstance("jdbc:mysql://localhost:3306/automation_v2","root", "",  "com.mysql.jdbc.Driver") 
sql.eachRow("SELECT * FROM dynamic_variables where project_id = 126") { 
    row -> 
    variable_name = row.variable_name 
    variable_value = row.variable_value 
    dynamicVariables.add(variable_name+","+variable_value) 
    } 

sql.close() 

//set properties so that all steps in test case can access and use them 
def a =1 
dynamicVariables.each(){ 
    def (propertyName, propertyValue) = it.split(',') 
    varPropNames.add(propertyName) 
    varPropValues.add(propertyValue) 

    testRunner.testCase.testSuite.setPropertyValue(propertyName, propertyValue) 
     testRunner.testCase.testSteps["setProperties"].setPropertyValue(propertyName, propertyValue)  
    propertyName = testRunner.testCase.testSuite.getPropertyValue(propertyName) 
    log.info(propertyName) 
    a++ 
    } 

所以当propertyName打印到控制台时,我可以在每行中看到保时捷和911“make”和“model”打印出来。但是,如何使用$ {make}和$ {model}属性语法在JSON示例请求中动态引用它们?

谢谢!

+0

是不是正确的,你需要执行所有的REST请求行?可能是你可以检查这一个,看看是否有帮助 - http://stackoverflow.com/questions/40471259/groovy-script-to-read-an-xml-file-and-update-next-step-request-与文件内容/ 40480207#40480207 – Rao

回答

0

我想出了这一个。 :) :)

我不得不使用context.put来提取属性值,以便它可以动态访问我的测试请求。

这是代码,让我有行:

propertyNameArray.add(testRunner.testCase.testSuite.getPropertyValue(propertyName)) 
    context.put('${varPropNames}'+[a],propertyNameArray[a]) 

所以我更新的代码现在看起来像:

def dynamicVariables = [] 
def varPropNames = [] 
def varPropValues = [] 
def propertyNameArray = [] 


    def sql2 = Sql.newInstance("${db_env}","root", "", "com.mysql.jdbc.Driver") 
    sql2.eachRow("SELECT * FROM dynamic_variables where project_id = 126") { 
     row -> 
     variable_name_1 = row.variable_name 
     variable_value_1 = row.variable_value 
     dynamicVariables.add(variable_name_1+","+variable_value_1) 
    } 

sql2.close() 

def propertyNameArrayValue=[] 
def a =1 
    dynamicVariables.each(){ 
     def (propertyName, propertyValue) = it.split(',') 
     varPropNames.add(propertyName) 
     varPropValues.add(propertyValue) 


     testRunner.testCase.testSuite.setPropertyValue(propertyName, propertyValue) 
      testRunner.testCase.testSteps["setProperties"].setPropertyValue(propertyName, propertyValue)  
     propertyNameArray.add(testRunner.testCase.testSuite.getPropertyValue(propertyName)) 
     context.put('${varPropNames}'+[a],propertyNameArray[a]) 

     //log.info(propertyName) 
     a++ 
    } 

sql.close() 
相关问题