Forum Discussion

harmancheema's avatar
harmancheema
Occasional Contributor
6 years ago
Solved

Set variable in groovy based on parameter from Jenkins

I have a groovy script in my tests which takes the user input to set the environment and project variables. I'm trying to integrate the tests with jenkins, and want to create a parameterized build for the two parameters.

 

So, if someone chooses env as "QA" and project as "testing" in Jenkins, I want the groovy script to read those values, and run the tests accordingly. 

 

I tried finding something related to this in the community but was unable to find it. Anyone has any ideas how this can be achieved?

 

  • Hello,

    -----------------------
    In Jenkins...

    ...Fill out desired Jenkins parameters
    =========== Jenkins "This project is parameterized" ================
    Choice Parameter

     

    Name ACCOUNT
    Choices development
                  integration
                  production


    Choice Parameter

     

    Name PROJECT
    Choices prjX
                  prjY
                  prjZ


    ...Use parameters in your command line interface for launching SoapUI/ReadyAPI within Jenkins
    =========== Jenkins Build Execute shell ================
    # clean up temporary script dir
    rm -rf bin

     

    # create a temporary script dir
    mkdir bin
    cd bin

     

    # create a temporary script
    touch tmp-test-runner.sh
    chmod 755 tmp-test-runner.sh

     

    SOAPUI_TESTRUNNER=/var/lib/jenkins/SmartBear/SoapUI-5.3.0/bin/testrunner.sh
    SOAPUI_PROJECT=soapui/{your full project file name goes here. (i.e. readyapi-or-soapui-project.xml)}

     

    echo "echo Running tests against the $ACCOUNT account" > tmp-test-runner.sh
    echo "echo Running tests against the $PROJECT project" >> tmp-test-runner.sh
    echo "$SOAPUI_TESTRUNNER -a -I -j -f output -Pact=$ACCOUNT -Pprj=$PROJECT $SOAPUI_PROJECT" >> tmp-test-runner.sh
    export DEBUG=false

     

    # return to base directory
    cd ../

     

    # execute tmp script somehow or however you are doing it now
    bin/tmp-test-runner.sh

     

    -----------------------
    In SoapUI/ReadyAPI...
    SoapUI can be launched from Jenkins. Whatever '-P' properties you supply to the command line can be used during the run of your project. They override whatever you have defined in your project.
    Above the -P "act" and -P "prj" properties are set to whatever was picked in Jenkins build request.

     

    Review the command line options from Smartbear website. I have had success with Jenkins using -P command line option for project level properties. You could also use -D for system properties or -G for global properties if you like to use those instead.

     

    In any groovy script within your project, you can reference what was passed in that property using code such as:
       def accountNameEnvironment = context.expand( '${#Project#act}' ).toLowerCase();
       def projectName = context.expand( '${#Project#prj}' ).toLowerCase();

     

    Then use as needed with groovy code inside your project:
       if (accountNameEnvironment == 'development') {
           endPoint = 'https:\\something a';
       };
       if (accountNameEnvironment == 'integration') {
          endPoint = 'https:\\something b';
       };
       if (accountNameEnvironment == 'production') {
          endPoint = 'https:\\something c';
       };


       log.info 'project from Jenkins=' + projectName;

     

    You will have to code your projects with command line parameters input in mind. Jenkins will execute everything that is not disabled top down in functionality testing. You can do all of this locally on your computer to simulate what Jenkins will do by running SoapUI or ReadyAPI from command line interface from a shell script of your operating system. If it works from your command line, it is easy to port to Jenkins.

5 Replies

  • TNeuschwanger's avatar
    TNeuschwanger
    Champion Level 2

    Hello,

    -----------------------
    In Jenkins...

    ...Fill out desired Jenkins parameters
    =========== Jenkins "This project is parameterized" ================
    Choice Parameter

     

    Name ACCOUNT
    Choices development
                  integration
                  production


    Choice Parameter

     

    Name PROJECT
    Choices prjX
                  prjY
                  prjZ


    ...Use parameters in your command line interface for launching SoapUI/ReadyAPI within Jenkins
    =========== Jenkins Build Execute shell ================
    # clean up temporary script dir
    rm -rf bin

     

    # create a temporary script dir
    mkdir bin
    cd bin

     

    # create a temporary script
    touch tmp-test-runner.sh
    chmod 755 tmp-test-runner.sh

     

    SOAPUI_TESTRUNNER=/var/lib/jenkins/SmartBear/SoapUI-5.3.0/bin/testrunner.sh
    SOAPUI_PROJECT=soapui/{your full project file name goes here. (i.e. readyapi-or-soapui-project.xml)}

     

    echo "echo Running tests against the $ACCOUNT account" > tmp-test-runner.sh
    echo "echo Running tests against the $PROJECT project" >> tmp-test-runner.sh
    echo "$SOAPUI_TESTRUNNER -a -I -j -f output -Pact=$ACCOUNT -Pprj=$PROJECT $SOAPUI_PROJECT" >> tmp-test-runner.sh
    export DEBUG=false

     

    # return to base directory
    cd ../

     

    # execute tmp script somehow or however you are doing it now
    bin/tmp-test-runner.sh

     

    -----------------------
    In SoapUI/ReadyAPI...
    SoapUI can be launched from Jenkins. Whatever '-P' properties you supply to the command line can be used during the run of your project. They override whatever you have defined in your project.
    Above the -P "act" and -P "prj" properties are set to whatever was picked in Jenkins build request.

     

    Review the command line options from Smartbear website. I have had success with Jenkins using -P command line option for project level properties. You could also use -D for system properties or -G for global properties if you like to use those instead.

     

    In any groovy script within your project, you can reference what was passed in that property using code such as:
       def accountNameEnvironment = context.expand( '${#Project#act}' ).toLowerCase();
       def projectName = context.expand( '${#Project#prj}' ).toLowerCase();

     

    Then use as needed with groovy code inside your project:
       if (accountNameEnvironment == 'development') {
           endPoint = 'https:\\something a';
       };
       if (accountNameEnvironment == 'integration') {
          endPoint = 'https:\\something b';
       };
       if (accountNameEnvironment == 'production') {
          endPoint = 'https:\\something c';
       };


       log.info 'project from Jenkins=' + projectName;

     

    You will have to code your projects with command line parameters input in mind. Jenkins will execute everything that is not disabled top down in functionality testing. You can do all of this locally on your computer to simulate what Jenkins will do by running SoapUI or ReadyAPI from command line interface from a shell script of your operating system. If it works from your command line, it is easy to port to Jenkins.

    • Olga_T's avatar
      Olga_T
      SmartBear Alumni (Retired)

      Hi all,

      TNeuschwanger what a great and detailed reply! Thank you for your contribution.

      harmancheema if this answers your question, could you please click the Accept as Solution button below the provided instructions? Our users will then be able to easily find these instructions if they face a similar issue.

       

      Thanks in advance

    • harmancheema's avatar
      harmancheema
      Occasional Contributor

      Thanks for the detailed explanation, I think this should work.

      • TNeuschwanger's avatar
        TNeuschwanger
        Champion Level 2

        Hello. That looks like Jenkins parameters. Now you just have to configure the Jenkins project to invoke the command line run of SoapUI/ReadyAPI with those parameters passed. Jenkins configuration section of "Build", then "Execute shell", then "Command" as of Jenkins version 2.88.


        SOAPUI_TESTRUNNER=/var/lib/jenkins/SmartBear/SoapUI-5.3.0/bin/testrunner.sh {wherever you have SoapUI/ReadyAPI installed on Jenkins server}
        SOAPUI_PROJECT=soapui/{full project file name including .xml extension} {whatever SoapUI/ReadyAPI project you will run}

        echo "echo Running tests against the $Environment environment" > tmp-test-runner.sh {just to display in Jenkins console log what was picked}
        echo "echo Running tests against the $Project project" >> tmp-test-runner.sh {just to display in Jenkins console log what was picked}
        echo "$SOAPUI_TESTRUNNER -P{whatever the actual project level properties name you want}=$Project -P{whatever the actual project level properties name you want}=$Environment $SOAPUI_PROJECT" >> tmp-test-runner.sh
        export DEBUG=false

         

        Groovy sample previously given is how to get the project level properties (which are set with -P command line option. {whatever the actual project level properties name you want} should be whatever you call it in Groovy properties expansion in groovy sample previously given.

         

        system.getenv is for operating system variable acquisition and this is not that. This is only about passing parameters from Jenkins to SoapUI/ReadyAPI and using them within a running SoapUI/ReadyAPI project.

        Just to re-iterate, if you experiment with running SoapUI from the command line on your own computer and setting command line options there you won't have to debug within Jenkins as much. Jenkins is really just a mechanism to run your SoapUI project file from the command line.

         

        Here are a couple of reference links that are the base of Jenkins interaction:


        command line
        https://www.soapui.org/test-automation/running-from-command-line/functional-tests.html


        accessing properties (get and set)
        https://www.soapui.org/scripting-properties/tips-tricks.html

         

        good luck harmancheema  :)