Forum Discussion

JustinM89's avatar
JustinM89
Contributor
8 years ago

Clear REST request on saving?

I discovered today that the settings.xml file for our SoapUI composite project is a whopping 500,000 lines and nearly 50MB. The reason for this is because of the way we structure our test cases. We use a ton of Groovy scripts in between test steps to map to/from JSON and objects, then use the following syntax to set the request of the subsequent test steps:

 

testRunner.testCase.getTestStepByName("Some Test Step").setPropertyValue("Request", someJson);

The problem is that these requests (some of which are thousands of lines of JSON) remain in the REST request test steps, and upon saving the project, are written into that settings.xml file. Is there anyway to clear out all requests upon save? We've tried writing our own listener in Java by overriding the Project class' beforeSave method, but that doesn't appear to be working. We know it's actually getting into our method, because we have it creating an empty text file, but it's not clearing out the requests. The code we wrote is below (I know that these nested loops are not the most efficient, but we just wrote something quick and dirty to see if it could work):

 

public class SoapUiProjectListener extends ProjectListenerAdapter {

@Override
public void beforeSave(Project project) {
for (TestSuite tSuite: project.getTestSuiteList()) {
for (TestCase tCase : tSuite.getTestCaseList()) {
for (TestStep tStep : tCase.getTestStepList()) {
String request = tStep.getPropertyValue("Request");
if (request!= "" && request.length() > 500) {
tStep.setPropertyValue("Request", "");
}
}
}
}
}
}

I don't think it's even getting the project correctly, as project.getName() doesn't even return anything. Does anyone have ideas on how we could accomplish this? We've considered just making a test-suite level "tempJson" property and always writing to that, but it would require a lot of restructuring.

  • OK, I've found a suitable solution using existing event handlers. I added a TestRunListener.afterRun handler with the following code:

     

    testCase = context.getTestCase()

    for (tStep in testCase.getTestStepList()) { String request = tStep.getPropertyValue("request") if (request.length() > 500) { tStep.setPropertyValue("request", "") } }

    This will execute after any test is executed, even if the test is canceled or fails.

     

    While I was at it, I also added handlers for clearing custom properties as well. This should hopefully help with managing the project in SVN -- we have a ton of properties that are generated each test run and these files show as conflicted every single time we sync. We really only need the properties at run time, so I added handlers to clear those too. The code is below for anyone interested:

     

    TestRunListener.afterRun to clear test case level properties

    testCase = context.getTestCase()
    
    testCase.properties.each {
    	testCase.properties[it.key].value = ''
    }

    TestSuiteRunListener.afterRun to clear test suite level properties

    testRunner.testSuite.properties.each {
    	testRunner.testSuite.properties[it.key].value = ''
    }
  • groovyguy's avatar
    groovyguy
    Community Hero

    Try changing

     

     

    tStep.getPropertyValue(...)

     

    to

     

     

    tStep.testRequest.getRequestContent();

     

    and 

     

     

     tStep.setPropertyValue("Request", "")

     

     

    to

     

     

     tStep.testRequest.setRequestContent("");

     

    • JustinM89's avatar
      JustinM89
      Contributor

      OK, I've found a suitable solution using existing event handlers. I added a TestRunListener.afterRun handler with the following code:

       

      testCase = context.getTestCase()

      for (tStep in testCase.getTestStepList()) { String request = tStep.getPropertyValue("request") if (request.length() > 500) { tStep.setPropertyValue("request", "") } }

      This will execute after any test is executed, even if the test is canceled or fails.

       

      While I was at it, I also added handlers for clearing custom properties as well. This should hopefully help with managing the project in SVN -- we have a ton of properties that are generated each test run and these files show as conflicted every single time we sync. We really only need the properties at run time, so I added handlers to clear those too. The code is below for anyone interested:

       

      TestRunListener.afterRun to clear test case level properties

      testCase = context.getTestCase()
      
      testCase.properties.each {
      	testCase.properties[it.key].value = ''
      }

      TestSuiteRunListener.afterRun to clear test suite level properties

      testRunner.testSuite.properties.each {
      	testRunner.testSuite.properties[it.key].value = ''
      }