Search code examples
testinggroovysoapui

Retrive list of environments for a project using groovy script in Soap-UI


Within SoapUI, I would like to present the user with a dialog box to select an Environment from the Project when executing the test runner.

I have added an event listener for ProjectRunListener.beforeRun. Currently, when that event fires, I have a custom library that executes the following given the projectRunner which is provided:

public static void SetEndpoints(ProjectRunner projectRunner, Logger log = null){
    if (projectRunner == null) {
        _Log(log, "Parameter 'projectRunner' cannot be null.");
        throw new Exception();
    }

    if (_RunOnce(projectRunner, "SetEndpoints")){
        _Log(log, "Begin - Util.SetEndpoints (Project)");

        def project = (Project)projectRunner.getProject();
        _SetEndpointsForTestSuites(project.getTestSuiteList(), log);

        _Log(log, "End - Util.SetEndpoints (Project - ${project.name})");
    }
}

public static void SetEndpoints(TestSuiteRunner testSuiteRunner, Logger log = null){
    if (testSuiteRunner == null) {
        _Log(log, "Parameter 'testSuiteRunner' cannot be null.");
        throw new Exception();
    }

    if (_RunOnce(testSuiteRunner, "SetEndpoints")){
        _Log(log, "Begin - Util.SetEndpoints (TestSuite)");

        def testSuite = (TestSuite)testSuiteRunner.getTestSuite();
        _SetEndpointsForTestSuites([testSuite], log);

        _Log(log, "End - Util.SetEndpoints (TestSuite - ${testSuite.name})");
    }
}

public static void SetEndpoints(TestCaseRunner testCaseRunner, Logger log = null){
    if (testCaseRunner == null) {
        _Log(log, "Parameter 'testCaseRunner' cannot be null.");
        throw new Exception();
    }

    if (_RunOnce(testCaseRunner, "SetEndpoints")){
        _Log(log, "Begin - Util.SetEndpoints (TestCase)");

        def testCase = (TestCase)testCaseRunner.getTestCase();
        _SetEndpointsForTestCases([testCase], log);

        _Log(log, "End - Util.SetEndpoints (TestCase - ${testCase.name})");
    }
}

private static void _SetEndpointsForProject(Project project, Logger log = null){

    _SetEndpointsForTestCases(project.getTestSuiteList(), log);
}

private static void _SetEndpointsForTestSuites(List<TestSuite> testSuites, Logger log = null){
    def testCases = [] as List<TestCase>;
    testSuites.each{
        testCases.addAll(it.getTestCaseList() as List<TestCase>);
    };

    _SetEndpointsForTestCases(testCases, log);
}

private static void _SetEndpointsForTestCases(List<TestCase> testCases, Logger log = null){
    // Get all test request steps
    //def testSteps = testCase.getTestStepList();
    def testSteps = [] as List<HttpRequestTestStep>;
    testCases.each {
        testSteps.addAll(it.getTestStepsOfType(RestTestRequestStep.class) as List<HttpRequestTestStep>);
    }

    // Loop across test request steps
    def stepMap = [:]
    testSteps.each {
        Interface iface = it.getInterface();
        if (stepMap[iface.name] == null){

            List<String> endpointList = Arrays.asList(iface.getEndpoints()) as List<String>;
            if ((endpointList != null) && (endpointList.size() > 0)) {
                def selection = JOptionPane.showInputDialog(null, "Select an endpoint:",
                    iface.name, JOptionPane.QUESTION_MESSAGE, null,
                    endpointList as String[], endpointList[0]).toString();

                if (selection == null){
                    stepMap.put(iface.name, "IGNORE");
                    _Log(log, "User cancelled endpoint selection for interface ${iface.name}");
                }
                else {
                    stepMap.put(iface.name, selection);
                    _Log(log, "User selected endpoint '${selection}' for interface ${iface.name}");
                }
            }
        }

        // Set endpoint
        if (stepMap[iface.name] != "IGNORE"){
            it.getHttpRequest().setEndpoint(stepMap[iface.name]);
        }
    }
}

private static boolean _RunOnce(TestRunner runner, String methodName){
    def context = runner.getRunContext();
    if (context.getProperty(methodName) != null)
        return false;

    context.setProperty(methodName, methodName);
    return true;
}

private static void _Log(Logger log, Object message){
    if (log != null){
        log.info message;
    }
}

The script, when given a TestRunner from a Project, TestSuite, or TestCase, it will find all the interfaces for each test request step to be executed and query the user to select which endpoint to use for that test case. It will only query the user once for each interface and it will not query the user if it has already been executed (it can determine that by placing a flag within the run context which it checks for).

Within the event: ProjectRunListener.beforeRun I call this:

Util.SetEndpoints(projectRunner, log);

Within the event: TestSuiteRunListener.beforeRun, TestRunListener.beforeRun I call this:

Util.SetEndpoints(testRunner, log);

So regardless of which level the user kicks off the test runner, he/she will still be presented with the dialog to select endpoints for each interface used.

I believe this can be done in a better way using Environment selections instead. I see within the API that I am able to set the environment at the project level, which would probably set all the interface endpoints to the appropriate value for the given active environment. However, I do not see a way that we can get a list of all the available environments. The Project object within the API has .setActiveEnvironment(), but does not mention anything about getting the list of environments.

Given a Project object, does anyone know of a way to get a list of all the configured Environments?


Solution

  • As per the API documentation, it would appear that the following should work for you:

    • getEnvironmentList() found in List<com.eviware.soapui.model.environment.Environment>
    • getEnvironmentNames()
    • getEnvironmentAt(int index) and getEnvironmentByName(String envName) found in com.eviware.soapui.model.environment.Environment

    Not sure what API guide you are looking at, but I think these are all valid for what you're working on. Note that this is a PRO feature, so maybe that's why you can't find it (if you're looking at the free version).