I am trying the following script:
import groovy.sql.Sql
println 'Some GR8 projects:'
com.eviware.soapui.support.GroovyUtils.registerJdbcDriver('net.sourceforge.jtds.jdbc.Driver')
sql = Sql.newInstance( 'jdbc:jtds:sqlserver://localhost:1433/agilejury-thehl_MC', 'user', 'password', 'net.sourceforge.jtds.jdbc.Driver' )
def rows = sql.rows( 'select * from pool_person' )
println rows.size()
sql.close()
I expected to see stuff in the script log, but I see absolutely nothing. I have run this as a groovy test step and as a setup script.
Is this script running?
Use
log.info rows.size()
I assume you already have the driver jar inside ..\soapUI-*.*.*\bin\ext or in the classpath.
Related
I am working on a project where we have multiple environments and for each environment we have a separate database connection string which I have added in environments section.
Now I want to get the environment connection string at runtime using Groovy test step to use it further to execute query. Below is my script:
def connectionString = context.testCase.testSuite.project.activeEnvironment.databaseConnectionContainer.getDatabaseConnectionByName("dbconnection").getConnectionString()
Now in response I am getting connection error as I can see in response the script is returning jdbc:oracle:thin:Username/PASS_VALUE#machine details. As per my understanding the PASS_VALUE is causing this issue.
com.eviware.soapui.support.GroovyUtils.registerJdbcDriver( "oracle.jdbc.driver.OracleDriver" )
import groovy.sql.Sql;
//get active environment connection string
def connectionString = context.testCase.testSuite.pro
log.info ("Connection String#####"+connectionString)
def driver="oracle.jdbc.driver.OracleDriver";
def con = Sql.newInstance(connectionString,driver);
So this getConnectionString() should return the password so that it can be used in further script.
I'm trying to configure Jenkins build trigger from Jira post-function Groovy script
Here is my Groovy code:
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
def WANITOPUSHField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10802);//customfield id
def WANITOPUSHValue = issue.getCustomFieldValue(WANITOPUSHField);
def SelectVersionField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10805);//customfield id
def SelectVersionValue = issue.getCustomFieldValue(SelectVersionField);
if(WANITOPUSHField != null) {
if(WANITOPUSHValue.toString() == 'Yes') {
'curl --user USERNAME:PASSWORD "http://JENKINS_URL/job/deploy-dev-test/buildWithParameters?token=MYTOCKEN&ENV=1"'.execute()
UserMessageUtil.success("Jenkins Build started ");
} else {
UserMessageUtil.success("Condition Not sucsess "+WANITOPUSHValue.toString());
}
}
Here I have used curl command to trigger Jenkins build if the Jira ticket status changed, but the curl command is not working here
It is throwing output on the alert box
java.lang.UNIXProcess#4d0c79da
I don't know what its mean whether the command is executing successfully or not, Please anyone can help me on this and suggest me if I can use some different method with Groovy to achieve this
"something".execute() returns instance of UNIXProcess java class. When toString() method is not overriden you will see something like java.lang.UNIXProcess#4d0c79da
Here some code which will help you to get shell command output:
def command = 'curl --user USERNAME:PASSWORD "http://JENKINS_URL/job/deploy-dev-test/buildWithParameters?token=MYTOCKEN&ENV=1"'
def proc = command.execute()
proc.waitFor()
println "Process exit code: ${proc.exitValue()}"
println "Std Err: ${proc.err.text}"
println "Std Out: ${proc.in.text}"
I am trying to create Soapui project using groovy script.
When running it directly from SoapUI, the Script is working correctly and new Project with WSDL is created.
The script is created in: Project -> TestSuite -> TestCase -> Groovy script
import com.eviware.soapui.impl.wsdl.*
import com.eviware.soapui.impl.WsdlInterfaceFactory
String Release = "xxx.yyy";
String projectName = "Test_$Release" + ".xml";
String projectPath = "D:/work/";
String fullProjectPath = "$projectPath$projectName";
String pathToWSDL = "D:/work/AWSECommerceService.wsdl.xml";
log.info ("Release value is $Release");
log.info ("Projet path is $projectPath");
log.info ("Project name is $projectName");
log.info ("pathToWSDL $pathToWSDL");
log.info ("fullProjectPath $fullProjectPath");
def currentProject = testRunner.testCase.testSuite.project;
WsdlProject project = currentProject
.getWorkspace()
.createProject(projectName, new File(fullProjectPath));
WsdlInterface iface = WsdlInterfaceFactory.importWsdl(project,pathToWSDL, true )[0]
context.testCase.testSuite.project.save("");
The script is working correctly when from SoapUI. But, failing when run it using testrunner.bat as given below:
testrunner.bat "D:\Ivo\Project.xml"
It is returning following error:
Error:java.lang.NullPointerException: Cannot invoke method createProject() on null object
Can you try change as given below:
From:
WsdlProject project = currentProject
.getWorkspace()
.createProject(projectName, new File(fullProjectPath));
To:
WsdlProject project = new WsdlProject();
project.setName(projectName);
//your statements goes here
//finally save project
project.saveAs(fullProjectPath);
In a soapui groovy script test step I've this.
context.setProperty("searchA", new searchA());
class searchA{
def testRunner
def searchA(testRunner){
this.testRunner=testRunner
}
def search(a,b){
def search_TestCase = testRunner.testCase.testSuite.getTestCaseByName("Search")
search_TestCase.setPropertyValue("ABC", a)
search_TestCase.setPropertyValue("DEF", b)
search_TestCase.run(new com.eviware.soapui.support.types.StringToObjectMap(), false)
}
}
and in an assertion script in a different test suite I am calling the above code like this.
scripts = messageExchange.modelItem.testStep.testCase.testSuite.project.testSuites["Test"]
scripts.testCases["Lib123"].testSteps["TestLib123"].run(context.getTestRunner(),context)
context.searchA.search("value1","value2")
but this gives me error "can not get property testCase on null object". whats wrong here?
I am not seeing null object error now. The issue was that testRunner is not available in script assertion so we need to create it like this in script assertion and then pass it in the caller method.
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner
import com.eviware.soapui.support.types.StringToObjectMap
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext
testCase = messageExchange.modelItem.testStep.testCase
tcRunner = new WsdlTestCaseRunner( testCase, new StringToObjectMap() );
context.searchA.search("value1","value2",tcRunner)
This thread helped me.
I'm writing a script in Groovy and I would like someone to be able to execute it simply by running ./myscript.groovy. However, this script requires a 3rd party library (MySQL JDBC), and I don't know of any way to provide this to the script other than via a -classpath or -cp argument, e.g.
`./monitor-vouchers.groovy -cp /path/to/mysql-lib.jar`
For reasons I won't go into here, it's not actually possible to provide the JAR location to the script using the -classpath/-cp argument. Is there some way that I can load the JAR from within the script itself? I tried using #Grab
import groovy.sql.Sql
#Grab(group='mysql', module='mysql-connector-java', version='5.1.19')
def getConnection() {
def dbUrl = 'jdbc:mysql://database1.c5vveqm7rqgx.eu-west-1.rds.amazonaws.com:3306/vouchers_prod'
def dbUser = 'pucaroot'
def dbPassword = 'password'
def driverClass = "com.mysql.jdbc.Driver"
return Sql.newInstance(dbUrl, dbUser, dbPassword, driverClass)
}
getConnection().class
But this causes the following error:
Caught: java.sql.SQLException: No suitable driver
java.sql.SQLException: No suitable driver
at monitor-vouchers.getConnection(monitor-vouchers.groovy:13)
at monitor-vouchers.run(monitor-vouchers.groovy:17)
Is there a way I can execute this script using just ./monitor-vouchers.groovy
You should be able to do:
import groovy.sql.Sql
#GrabConfig(systemClassLoader=true)
#Grab('mysql:mysql-connector-java:5.1.19')
def getConnection() {
def dbUrl = 'jdbc:mysql://database1.c5vveqm7rqgx.eu-west-1.rds.amazonaws.com:3306/vouchers_prod'
def dbUser = 'pucaroot'
def dbPassword = 'bigsecret'
def driverClass = "com.mysql.jdbc.Driver"
return Sql.newInstance(dbUrl, dbUser, dbPassword, driverClass)
}
getConnection().class
Two more options:
Put the jar in ${user.home}/.groovy/lib
If the jar is in a known location, use this code to load it into the current class loader:
this.class.classLoader.rootLoader.addURL( new URL() )