2016-11-30 25 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() 
相關問題