您的位置:首页 > 产品设计 > UI/UE

【SoapUI】Groovy连接mysql数据库方式汇总,及部分方法纠正

2016-03-14 08:16 465 查看


【SoapUI】Groovy连接mysql数据库方式汇总

步骤1:

将mysql-connector-java-5.1.7-bin.jar驱动包(自行下载),放在soapui安装目录的lib文件夹下,比如:D:\Program
Files\SmartBear\SoapUI-Pro-5.0.0\lib

步骤2:

在groovy中写如下脚本:

importgroovy.sql.Sql

try{

/*---------方法1:直接连接DB,操作如下---------------*/

//defsql = Sql.newInstance("jdbc:mysql://127.0.0.1:3306/test","root","123456","com.mysql.jdbc.Driver");

/*--------方法2:通过读取配置文件连接数据库----------*/

//方式1:通过读取testCase配置中参数连接数据库,操作如下:

//比如:在某个testCase中定义DB_Connection_URL、DB_loginnam等参数,且参数具体值分别为:
jdbc:mysql://127.0.0.1:3306/test、root等,可参照直接连接DB中输入值。

// defDBProperties = testRunner.testCase.getTestStepByName( "TestCase的参数Name值"
);

// defsql = Sql.newInstance(DBProperties.getPropertyValue( "DB_Connection_URL"),DBProperties.getPropertyValue( "DB_loginname"),DBProperties.getPropertyValue(
"DB_Password"),DBProperties.getPropertyValue( "DB_Driver_Class" ));

//方式2:通过读取TestSuite配置参数连接数据库,操作如下:

比如:在某个TestSuite中定义DB_Connection_URL、DB_loginnam等参数,且参数具体值分别为:jdbc:mysql://127.0.0.1:3306/test、root等,可参照直接连接DB中输入值。

defDBProperties = testRunner.testCase.getTestSuite();

defsql = Sql.newInstance(DBProperties.getPropertyValue("DB_Connection_URL" ),DBProperties.getPropertyValue( "DB_loginname"),DBProperties.getPropertyValue( "DB_Password"),DBProperties.getPropertyValue(
"DB_Driver_Class" ));

defGet_shop_mobile=DBProperties.getPropertyValue( "shop_mobile" )

defGet_sn_id=DBProperties.getPropertyValue( "sn_id" )

//进行sql具体操作,根据店铺ID和sn_id

def
get_token = sql.firstRow("SELECTa.device_token from 1dcq_shop_device a LEFT JOIN 1dcq_shop b ona.shop_id=b.shop_id LEFT JOIN 1dcq_user c on b.principal_id=c.user_id wherea.sn_id=${Get_sn_id} and c.mobile=${Get_shop_mobile}")
;

//获取数据库表字段值

defToken= get_token.device_token

if(Token!= null & Token !="")

{

//方法1:将Token值存入testSuite中Custom
Property的参数

//testRunner.testCase.testSuite.setPropertyValue("YD_Token",Token)

//方法2:将Token值存入testCase中Custom
Property的参数

testRunner.testCase.setPropertyValue("YD_Token",Token)

//方法3:直接返回结果集

//return Token

log.info(Token)

}else{

login.info("获取验证失败,请联系管理员!")

}

}catch(Exceptione)

{

e.printStackTrace();

}finally{

try{ sql.close();}catch(Exception e){}

}

备注:1、数据库连接配置,可直接连接,也可以将从testSuite或testCase中定义参数获取配置值,也可以读取外部配置文件,但目前不考虑读取外部文件方式;2、将数据库查询操作得出来的值,可采用3种任一方法得到值(或者其他外部存储)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: