将值从 Groovy 脚本传递到 SoapUI 中的 DataSync

Passing value from Groovy script to DataSync in SoapUI

我有一个循环遍历数据集 X 的脚本,并针对该数据集 X 中的每条记录在另一个数据集 Y 中循环并检索一些结果。如何将这些结果传递给 DataSink?

周围的一些建议是使用属性,但是在我的 groovy 脚本中我有一个循环,我在其中接收结果,如果我用每个结果填充 属性 我想我将只能看到 属性 中的最后一个结果,而我的 DataSink 将只能找到最后一个结果。

我的代码如下:

def goodWeather = context.expand( '${#TestCase#goodWeather}' ) as String

if (goodWeather.equals("false"))
{
  def response = context.expand( '${CityWeatherRequest#Response#declare namespace ns1=\'http://tempuri.org/\'; //ns1:GetCityWeatherResponse[1]/ns1:GetCityWeatherResult[1]/ns1:Weather[1]}' )
  def cityinfo_City = context.expand( '${GetCitiesDS#cityinfo_City}' )
  def cityinfo_Country = context.expand( '${GetCitiesDS#cityinfo_Country}' )

  //Keep count to restrict number of returns.  CountSuggestedCities is a property.
  def count = context.expand( '${#TestCase#countSuggestedCities}' ) as Integer
  assert count instanceof Integer

  //Suggest some cities if skies are clear
  if (response.contains("clear sky"))
  {  
    if (count == 0) log.info("Making suggestions")  
    count ++
    testRunner.testCase.setPropertyValue("countSuggestedCities", count.toString()); 
        log.info(cityinfo_City + " located in: " + cityinfo_Country);
  }

  //Check property maxSuggestedCities to see if maximum suggestes required as been reached.
  if (count == (context.expand( '${#TestCase#maxSuggestedCities}' ) as Integer))
  {
    testRunner.testCase.setPropertyValue("countSuggestedCities", "0");  
    testRunner.testCase.setPropertyValue("goodWeather", "true");
    testRunner.gotoStepByName("SeperatorScript");
  }
}
else
{
    testRunner.gotoStepByName("SeperatorScript");
}

我想要的是用 DataSink 将该信息保存到数据库来替换 log.info(cityinfo_City + " located in: " + cityinfo_Country);

我认为 soapui 文档没有提供有关 DataSink 与 groovy 和数据库的示例。但您始终可以使用 Groovy sql 进行插入。这是示例代码:

def driverName = "com.mysql.jdbc.Driver"
com.eviware.soapui.support.GroovyUtils.registerJdbcDriver(driverName)
def user = "root" // change this , password, and jdbc url
def password = ""
def con = groovy.sql.Sql.newInstance("jdbc:mysql://localhost:3306/test_for_so",
        user, password, driverName)
def cityinfo_City = 'London'
def cityinfo_Country = 'England'
con.execute("INSERT INTO cityinfo (cityinfo_City, cityinfo_Country) VALUES (?, ?)",
        [cityinfo_City, cityinfo_Country])
con.close()