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

时间:2017-08-20 11:41:14

标签: groovy 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");
}

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

1 个答案:

答案 0 :(得分:2)

我不认为soapui doc提供了有关groovy和数据库的DataSink示例。但您始终可以使用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()