Forum Discussion

cjamieson's avatar
cjamieson
Established Member
6 years ago

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?

  • 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")
  • 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")
    • Olga_T's avatar
      Olga_T
      SmartBear Alumni (Retired)

      Hi all,

       

      Thanks for the instruction, jsheph01 !

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