cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy script to fail test run and skip to next line in datasource

SOLVED
Member

Groovy script to fail test run and skip to next line in datasource

Hi,

I have a current issue where I am trying to write a groovy script where if criteria is met, then to proceed with the rest of the test steps and if criteria is not met then to skip to thenext row of the datasource.

 

Test case stucture:

Datasource

Test step 1

Groovy - if loop to find if a criteria is met. If it is not met then i want it to proceed to the next line in the datasource and no proceed with the rest of the test steps. If the criteria is met then i want it to proceed with th rest of the test steps.

Test step 2

Test step 3

Datasource Loop

 

There are two groovy scripts I have tried:

1. 

if(testRunner.testCase.getTestStepByName("step 1").getPropertyValue("property1")=="0" ){
    testRunner.fail("There was no property found")
    testRunner.runTestStepByName("DataSource Loop") 
}

The problem with this is that 'fail' will fail the whole test case and does not proceed with any further execution.

 

2. 

if(testRunner.testCase.getTestStepByName("step 1").getPropertyValue("property1")=="0" ){
    throw new Exception("There was no property found")
    testRunner.runTestStepByName("DataSource Loop") 
}

This proceeds with Step 2 and Step 3 and does not skip to next row of the datasource.

 

Does anyone have a solution to this issue?

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Contributor

Re: Groovy script to fail test run and skip to next line in datasource

Would you be able to use a groovy assertion inside of the request and then pass/fail the step if the criteria is met? If you can do that you could have a seperate groovy script to check if the request failed then skip to the data source loop step.

 

def tStep = testRunner.testCase.getTestStepByName("Test step 1")
def result = tStep.getAssertionList()
// if you have more than one assertion result.size() will get the assertion count
if (result[0].status.toString() == "FAIL"){
	testRunner.gotoStepByName("DataSource Loop")
	}

If you are not able to run the groovy script as a request assertion you could disable all the test steps that should run only if the request passed. You can then use a groovy script to run the disabled test steps.

def property1 = testRunner.testCase.getTestStepByName("step 1").getPropertyValue("property1")
//if the assertion failed the script ends and the test steps will not be run
assert property1 == 0
testRunner.runTestStepByName("Test step 2")
testRunner.runTestStepByName("Test step 3")
2 REPLIES 2
Highlighted
Contributor

Re: Groovy script to fail test run and skip to next line in datasource

Would you be able to use a groovy assertion inside of the request and then pass/fail the step if the criteria is met? If you can do that you could have a seperate groovy script to check if the request failed then skip to the data source loop step.

 

def tStep = testRunner.testCase.getTestStepByName("Test step 1")
def result = tStep.getAssertionList()
// if you have more than one assertion result.size() will get the assertion count
if (result[0].status.toString() == "FAIL"){
	testRunner.gotoStepByName("DataSource Loop")
	}

If you are not able to run the groovy script as a request assertion you could disable all the test steps that should run only if the request passed. You can then use a groovy script to run the disabled test steps.

def property1 = testRunner.testCase.getTestStepByName("step 1").getPropertyValue("property1")
//if the assertion failed the script ends and the test steps will not be run
assert property1 == 0
testRunner.runTestStepByName("Test step 2")
testRunner.runTestStepByName("Test step 3")
Moderator

Re: Groovy script to fail test run and skip to next line in datasource

Hi all,

 

Thanks for the instruction, @jsheph01 !

@cjamieson did the above suggestions help? We are looking forward to hearing from you.


Olga Terentieva
SmartBear Assistant Community Manager

New Here?
Join us and watch the welcome video:
Watch the New Interview
APITestingMistake#2
Top Kudoed Authors