Forum Discussion

sonya_m's avatar
sonya_m
SmartBear Alumni (Retired)
5 years ago

API Masterminds - How do you reuse your ReadyAPI Groovy scripts?

How are you doing, API testers?Smiley Happy

 

If you do a lot of Groovy scripting in ReadyAPI, you might have already come to a need to use the same scripts in different tests and projects.

Instead of copying over the same code snippets again and again, you, of course, thought about a way to have them all in one place from where you could easily access them from other scripts.

The ways to achieve this are:

  • Create custom Groovy classes, embed your reusable Groovy scripts into their methods, save them as separate files with the .groovy extension and voila - your Script Library is ready. You just need to point ReadyAPI where to find it (Preferences > ReadyAPI > Script library). If you change anything in those external classes, ReadyAPI will detect this and reload the classes automatically.

  • Convert your Groovy code to Java code and embed it into methods of Java classes. Once the classes are compiled and packed into a .jar file, you can point to it from ReadyAPI (Preferences > ReadyAPI > Custom Java libraries) and start calling your Java methods right from your Groovy scripts. Making changes in Java classes is more time-consuming than in Groovy classes as you need to re-compile them, but you may find it more convenient to share them with your co-workers when it’s just a single .jar file.

Questions:

  • Do you use script libraries or custom Java libraries to reuse parts of your ReadyAPI scripts?

  • And if so, what tasks are your reusable scripts for?

4 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    Another one of the best feature of the product.

     

    The second point is new to me. However, if I am not wrong, it is not required to convert  Groovy into Java code. Groovy classes can be compiled and jar be created and used directory. Just have to use Groovy Compiler(groovyc) instead of Java compiler (javac).

     

    Both the cases, user have to write class files and methods, but not the scripts.

    The best part in the first approach is that in case if there is any change in the class, the tool takes care of compiling it on certain interval and reload the changes automatically. In the second case, user have to take care of compiling, creating jar. Need a restart of tool? Not sure.

     

    Again, this is limited to Pro software.

     

    In the open source also, user can leverage it using "Create class, compile and create jar; place it under /bin/ext directory"  which is almost smilary to 2nd approach mentioned in the above, But changes to the classes needs a restart in Open Source.

     

    Regarding the uses, limitless and of course depending on one's needs.

    Couple of them here;

    - saving the responses to a file

    - parsing requests / responses

    - complex / custom comparision used across the tests

     

     

    Here is nice artcle written by our expert rupert_anderson 

    http://rupertanderson.com/blog/1-how-to-develop-add-and-use-a-custom-groovy-library-in-soapui/

     

    This is can be used by both open source and pro software users.

  • sonya_m  : Nice Question, which i was struggling when i started with ReadAPI as i was new i copied the same code again and again, but then on some blog i come around with below solution and with some modification i make it worked for me :)

     

    In my current scenario, i have created 1 Resuable Test Suite, where i have placed all my reusable code in different Groovy Test Step. And i am calling those Steps where it is needed to me.

     

    For the same, i am attaching 1 sample project which help other help seekers.

    • nmrao's avatar
      nmrao
      Champion Level 3
      May be you should start using script library. Reason is the same "Re-usability". In case if the same is required in multiple projects, it will end-up copying the same suite into multiple projects. This can be avoided by using above mentioned approaches.


      • HimanshuTayal's avatar
        HimanshuTayal
        Community Hero

        nmrao  : Yes you are right, i didn't think of it that i am copying same suite where ever i need it or in every project. Will remember this advice.

         

        Thanks for letting me know. :)