woensdag 5 december 2018

Async Groovy script for polling database results for ReadyAPI (SoapUI) TestCases

Make a new "Groovy Script" Step in your SoapUI TestCase for Async calls to a database for waiting for a certain result:

import groovy.sql.Sql
import com.eviware.soapui.support.GroovyUtils
import static org.junit.Assert.assertThat
import static org.hamcrest.CoreMatchers.containsString

log.info(" init DB connection...")

sql = Sql.newInstance("jdbc:oracle:thin:<USERNAME>/<password>@<HOSTNAME:PORT/DATABASENAME>", "oracle.jdbc.OracleDriver")

def timeout = 0
log.info("setup done")

while (true)
   def itemCount = sql.firstRow("select count(*) from <TABLENAME> where <QUERY>")
   def itemCount_nr = itemCount[0].toString()
   log.info("while count = " + itemCount_nr)
   if (itemCount_nr == "1") break
   else if (timeout % 22 == 0) break

log.info("sql close")

In this above example the result will depend on the count (expect 1 row) if not then loop 2000msec with the max of 22 x 2000 msec then "break". If between this 44 sec there is a result then "break". After that always close the SQL connection.

1 opmerking:

  1. If you need properties from you SoapUI testcase in your Groovy Script, then you can use this option:

    def functIdSubstr = testRunner.testCase.testSteps['setFunctionalId'].getPropertyValue("bulkFunctionalIdSubstr")

    Then in your SQL query:
    def itemCount = sql.firstRow("select count(*) from where part1" + functIdSubstr + "part2")