在Katalon Studio中连接数据库并从数据库获取一些信息

时间:2019-01-11 06:59:38

标签: sql-server groovy automated-tests katalon-studio

我正尝试连接到我的数据库,并从那里获取Katalon Studio中的一些信息,但是由于某些原因,它不起作用。

我为连接创建了db connection关键字,然后在脚本中使用它。

package db_connection

import com.kms.katalon.core.util.KeywordUtil

import groovy.sql.Sql

public class SQLHandler_Con {
    String dbConnString = "jdbc:sqlserver://testapdb\\TEST;databaseName=2007"
    String dbUsername = "testuser"
    String dbPassword = "Password!"
    String dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"

    Sql sql = Sql.newInstance(dbConnString,dbUsername,dbPassword,dbDriver)

    public List getSelectResults(String query) {
        KeywordUtil.logInfo("Executing the query: " + query)
        List results = sql.rows(query)
        if(results.size() == 0) {
            KeywordUtil.markFailedAndStop("No rows returned from SQL Server")
        }
        return results
    }

    public void closeConn() {
        sql.close()
    }
}

这是我的SQLHandler,其他脚本如下

public class database_Selector {

    @Keyword
    def getResource() {
        SQLHandler_Con sql = new SQLHandler_Con()
        List<GroovyRowResult> res = sql.getSelectResults('SELECT TOP 100 PERSONAL_ID, PHONE2, SMS_MOBILE_PHONE FROM CLIENTS WHERE PERSONAL_ID IS NOT NULL AND PHONE2 IS NOT NULL AND SMS_MOBILE_PHONE IS NOT NULL ')
        int countFromDb = res.get(0).get('clients')
        println("No. of rows in Database is(are):" + res.get(0))
        return countFromDb
    }
}

当我尝试运行此

def database_count = CustomKeywords.'db_connection.databaseSelector.getResource'()
println(database_count)

它给我一个错误:database_count = db_connection.database_Selector.getResource()失败。

编辑:我已经编辑了代码,现在可以使用了,但是如果您有主意,我该如何从数据库中获取表,您能告诉我吗?我的脚本只为计数而写。

SELECT COUNT(*) as Clients FROM CLIENTS WHERE PERSONAL_ID IS NULL AND PHONE2 IS NOT NULL AND SMS_MOBILE_PHONE IS NULL

但是我想执行其他操作。

2 个答案:

答案 0 :(得分:3)

找到了我问题的答案,我决定在可能的情况下在此处发布。

 @Keyword
    def getResource() {
        SQLHandler_2000 sql = new SQLHandler_2000()
        List<GroovyRowResult> res = sql.getSelectResults('SELECT TOP 10 PERSONAL_ID, PHONE2 FROM CLIENTS WHERE PERSONAL_ID IS NOT NULL AND PHONE2 IS NOT NULL')

        for(Map oneRow in res) {
        String personalId = oneRow.get("PERSONAL_ID")
        KeywordUtil.logInfo("Personal ID: " + personalId)

        String phone = oneRow.get("PHONE2")

        KeywordUtil.logInfo("Phone number: " + phone )


        WS.sendRequest(findTestObject('Object Repository/GET_Customer'))
        RequestObject get_object = findTestObject('Object Repository/GET_Customer')
        get_object.setRestUrl(String.format(get_object.getRestUrl(), personalId, phone))
        ResponseObject get_response = WS.sendRequestAndVerify(get_object)
        WS.verifyResponseStatusCode(get_response, 200)
        String getContent = get_response.getResponseBodyContent()
        }
        return res
    }

答案 1 :(得分:3)

刷新数据文件-Katalon内部数据库,使用数据集,对某些数据进行断言

嗨! 想要添加到此线程以帮助新手。我还是半新手,所以我知道它的感觉。建立数据库连接后,下面的示例。怎么办?这适用于拥有许可证的Katalon用户(如果您没有许可证,也许可以使用。不确定。此测试假设您拥有全局连接,并且已制作一个数据文件以提取要声明的项。我为什么要添加这个答案?因为花了很长时间才找到关于如何实时刷新katalon内部数据文件的答案

下面,我正在执行中期测试,告诉我的内部数据库\数据库查询要刷新。我从网站订购了东西后就打电话给我。我希望从我们的网站用户界面中获取一个LogNo,并将其与sql服务器上的配套数据库条目进行比较。

其中一些导入是katalon的标准配置,其他则来自插件或jars

import com.kms.katalon.core.testcase.TestCase as TestCase 
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.testdata.DBData as DBData
import com.katalon.plugin.keyword.connection.DBType as DBType

//Fetch and Refresh Data. i.e. Katalon DataFile

DBData dbTestData1 = findTestData('internal database name')
dbTestData1.fetchedData = dbTestData1.fetchData()

//Create Method to compare UI against Database. I am also concatenating two items 
together to create unique strings to do asserts on. 
The items are LogNo (transaction number essentially and a message field in database. 
For the front end UI I am grabbing the LogNo on the payment confirmation page 
and using a hard coded string to concatenate with

//Defining UI String and Concatenate. Do not need to add .trim. 
My LogNo has a leading space

String UILOG = WebUI.getText(findTestObject('Object repository/Page_MyWebsite 
Confirmation/td_LogNo')).trim() + 'Transaction Processed Successfully'

//Defining Database String and Using Fetched Data
// example ('LogNo', 1) is the column name and the row number from my 
   dataset\datafile

String dbObjects1 = dbTestData1.getObjectValue('LogNo', 1) + 
dbTestData1.getObjectValue('Message', 1)

dbTestData1.getObjectValue('LogNo', 1)
dbTestData1.getObjectValue('Message', 1)

//This is optional. To see your strings you will do an assert on. 
Also to give a tiny bit of structure in the console

println('-----Comparison Strings-----')
println(dbObjects1)
println(UILOG)
println('------Comparison Strings End-----')

println('-----Assert Statements-----')
//Basically comparing LogNo-Transaction Completed Successfully (hardcoded)
with Database LogNo and Message field 
(Should say Transaction Completed Successfully)

assert dbObjects1 == UILOG : 'Passed'

println('-----Assert Statements End-----')