How to run testcase and testsuite using Groovy in soapui? - groovy

i have some projects in soapui.
i want to execute testsuites and testcases of these projects.
i tried with this groovy code:
//get test case from other project or from the same one
project = testRunner.getTestCase().getTestSuite().getProject().getWorkspace().getProjectByName("Project1")
testSuite = project.getTestSuiteByName("TestSuite 1 - Login");
testCase = testSuite.getTestCaseByName("TestCase 1-Login");
Thread.sleep(3000)
testSuite2 = project.getTestSuiteByName("TestSuite3 - Report");
testCase2 = testSuite.getTestCaseByName("TestCase1 - Report");
// run test case
runner = testCase.run(new com.eviware.soapui.support.types.StringToObjectMap(), false);
Thread.sleep(3000)
runner2 = testCase2.run(new com.eviware.soapui.support.types.StringToObjectMap(), false);
When i run this groovy code, i have the following error message:
java.lang.NullPointer.Exception
cannot invoke method run() on null object for the last line
runner2 = testCase2.run(new com.eviware.soapui.support.types.StringToObjectMap(), false);
If i remove the last line it's working well.
Any help please.
Thank you

I know this is super late to answer this question, but i will answer this so that it can help help seeking for help.
instead of Thread.sleep(3000), just use:
runner.waitUntilFinished()
Now it will wait until execution of step 1 get finished. Then it will start executing Step 2.

The only thing I can think of is that you're calling the wrong testcase on the step2, I tested the same with other test cases I have and it's happening the same If I put a wrong name for the test case I want to execute.

// TO RUN A TEST SUITEs ALL get APIs, getting testSuite handle from current //test_Case
// TO POST COMMENT I HAVE ADDED underscore char BELOW
// u need to import here
def my_TestSuite = testRunner.testCase.testSuite
log.info(my_TestSuite.name)
for (my_TestCase in my_TestSuite.get_TestCaseList())
{
for (testStep in myTestCase.get_TestStepList())
{
if( testStep instanceof WsdlTestRequestStep || testStep instanceof RestTestRequestStep ) {
def http_Verb = (testStep.getTestRequest().getMethod())
String apiType = http_Verb.toString()
if (apiType == "GET"){
log.info(myTestCase.name)
}
}
}
}

Related

How do I get the Assertion success or fail message through an email in JMeter?

I'm trying to get the Assertion results through an email.
There are several endpoints(requests) and associated assertions in my test plan.
Below is the Groovy code I'm using in JSR223 PreProcessor. In My SMTP Sampler, I've been using ${body} to get the results from the script.
But in email prints null when all the Assertions are passing and print exceptions when those are failing.
I need to get below through email.
Success message when there are all the assertions are passing
Fail message with the failure request name when they
import org.apache.jmeter.assertions.AssertionResult;
AssertionResult[] results = prev.getAssertionResults();
StringBuilder body = new StringBuilder();
for (AssertionResult result : results) {
body.append(result.getFailureMessage());
body.append(System.getProperty("line.separator"));
}
vars.put("body", body.toString());
JSR223 PreProcessor is being executed before each Sampler in its scope
prev stands for previous Sampler Result
Assuming above 2 points it is absolutely expected that when JSR223 PreProcessor is being executed before the first Sampler in your test the previous result doesn't exist yet, you just need to add another condition to check whether it is null or not.
import org.apache.jmeter.assertions.AssertionResult
if (prev != null) { // ensure that we have the previous sampler result
AssertionResult[] results = prev.getAssertionResults();
StringBuilder body = new StringBuilder();
for (AssertionResult result : results) {
body.append(result.getFailureMessage());
body.append(System.getProperty("line.separator"));
}
vars.put("body", body.toString());
}

How to run script assertion while running the test step?

I am new to groovy script and soap UI. i added script assertion to a test step. how ever when i run the test case, script assertions are not running. i have to manually run it to verify my response is correct.
can anyone help me on this please?
My groovy script test assertion:
import groovy.json.JsonSlurper
//grab response
def response = messageExchange.response.responseContent
//Convert to JsonSluper to access data
def list = new JsonSlurper().parseText(response)
//check delegates are in one session per timeslot
for (i = 0; i < list.size(); i++) {
// find all items for this delegate in this timeslot
def itemsForThisDelegateInThisTimeslot = list.findAll {element -> element.delegateId == list[i].delegateId && element.agendaItemId== list[i].agendaItemId}
log.info(list[i].delegateId)
// there should not be more than 1
if(itemsForThisDelegateInThisTimeslot.size() > 1) {
log.info(list[i].delegateId + "Delegate already assigned to a workshop at same time");
//Assert fail in execution
throw new Error ('Fail')
}
}
Firstly, there are no assertions in this Script Assertion. Look up Groovy assert.
If you're 'asserting' that the script is Pass or Fail, you need something like...
assert (response.contains('Something from the response'));
or
assert (someBooleanVar == true);
If it passes, the step goes Green. If it fails, it goes Red.
IMHO, it looks you're throwing an exception when the step fails. I would not use an exception in this way. An exception is there to catch and report code issues. Not a test failure.
Regarding exceptions, look up Groovy Try and Catch.
As for this running (or not) when you run a test case. I suspect it is running, but as you're not asserting anything, you can't see the result.
Ever noticed the Script Log tab at the bottom of the screen? All of your log.info statements will be in here when the test step runs. I'd suggest clearing this log (right-click in the Script Log window...), and then running the test case again and have a look in the Script Log for some of your logging messages.

functions implementation

I have implemented multiple groovy methods for sending different notifications, but the code breaks-off the functions concept. So i want to rewrite/combine all groovy methods in one single methods so that i can call that one method wherever i need.
Doesn't matter success or failure and i need to pass message as parameter.
static void sendSuccessApplicationNotification(p1,p2,p3,p4) {
def x = Notify(this)
x.triggerBuild("SUCCESSFUL, application ${p1}:${p2} started properly", "${p3}")
x.triggerBuild("SUCCESSFUL, application ${p1}:${p2} started properly", "${p4")
}
Finally above above should be converted to one method.Checked many articles not getting an exact example.
you can use groovy template engine in your generalized function:
import groovy.text.SimpleTemplateEngine
void triggerBuild(a,b){
println "${a} >>>> ${b}"
}
void sendNotification(code, Map parms, List nodes) {
def templates = [
'appY': 'SUCCESSFUL, application ${app}:${ver} started properly',
'appN': 'FAILED, application ${app}:${ver} failed to start properly',
'depY': 'SUCCESSFUL deployment of ${app}:${ver} to ${node}<br>Executed by ${user}',
'depN': 'FAILED deployment of ${app}:${ver} to ${node}<br>Executed by ${user}'
]
def template = templates[code]
assert template
def message = new SimpleTemplateEngine().createTemplate(template).make(parms).toString()
nodes.each{node->
triggerBuild(message, node)
}
}
sendNotification('appY',[app:'myapp', ver:'v123'],['n1','n2'])
the code above will output:
SUCCESSFUL, application myapp:v123 started properly >>>> n1
SUCCESSFUL, application myapp:v123 started properly >>>> n2

How to stop testcase execution if request fails in soapui using script assertion?

If the soap request has a failure means Status != "HTTP/1.1 200 OK" , testCase should stop and no further steps should run
There is a way to do this in groovy, but i do not want an extra test step to be added in testcase
def headers = testRunner.testCase.getTestStepByName("RequestName").httpRequest.response.responseHeaders['#status#']
if (!headers.contains("HTTP/1.1 200 OK"))
{
testRunner.fail("" + headers + "Flow failed")
testRunner.fail("No futher testSteps will be run inside the current case")
}
Please note i cannot change below settings due to some other groovy code restriction
The reason for not changing above option i have a testcase where there are 10 steps. 1 is request and other 9 steps validate various things. So if i check the "Abort on error" option and step 3 fails. then none of the steps from 4 to 10 runs. So please provide a solution considering not using "abort" option
So could you please provide a solution for script assertion without ticking this option."Abort on error"
Since testRunner.fail is not available inside script assertion and also normal assertion (assert 0==1) does not stop testcase unless we tick the above setting. i am stuck with this limitations
You have access to testRunner through the context variable available in a script assertion, so why not something like:
def httpResponseHeader = messageExchange.responseHeaders
def headers = httpResponseHeader["#status#"]
log.info("Status: " + headers)
if (!headers.contains("HTTP/1.1 200 OK")) {
context.testRunner.fail("" + headers + "Flow failed")
context.testRunner.fail("No futher testSteps will be run inside the current case")
}
Thanks #craigcaulifield, your answer has helped me a lot.
Good to know that testRunner is available even in script assertion in a tricky way
Now using script assertion we can stop a testCase if request fails.
However when we run request alone not as part of testcase an error comes
cannot invoke method fail() on null object
This error comes because
context.testRunner.fail()
testRunner is only available during test Case execution and not individual testStep execution
So to overcome here is the code which can take care of both situation
def responseHeaders=messageExchange.responseHeaders
def status=responseHeaders["#status#"]
// Checking if status is successfully fetched i.e. Response is not empty
if(status!=null)
{
if(!status.contains("HTTP/1.1 200 OK"))
{
// context.testRunner is only available when the request is run as a part of testCase and not individually
if(context.testRunner!=null)
{
// marking the case fail so further steps are not run
context.testRunner.fail()
}
assert false, "Request did not returned successful status. Expected = [HTTP/1.1 200 OK] but Actual = " + status
}
}
else
{
if(context.testRunner!=null)
{
context.testRunner.fail()
}
assert false, "Request did not returned any response or empty response"
}

Jenkins Groovy Remote Control Plugin - example

I can't connect to Jenkins and execute script via Groovy Remote Plugin.
I found only this documentation: https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Remote+Control+Plugin
My example:
class GroovyDemo {
static void main(def args) {
def transport = new HttpTransport("http://my-jenkins/plugin/groovy-remote/")
def remote = new RemoteControl(transport)
//1
def result1 = remote {
return jenkins.version.value
}
println "Jenkins version was ${result1}."
//2
def result2 = remote {
return Jenkins.getInstance().getItems().size()
}
println "Jobs count: ${result2}."
}
}
I get the result:
Jenkins version was 1.580.3.
0
Why I get zero as number of jobs despite I have many jobs on my Jenkins?
Thanks
You used different variable names for the Jenkins instance - lower case "jenkins" for the successful call and upper case "Jenkins" for the unsuccessful one.

Resources