Showing posts with label groovy. Show all posts
Showing posts with label groovy. Show all posts

Monday, February 25, 2013

Setup Groovy in Eclipse

Groovy came in my Test Automation life here in Persado in order to make my daily life more easy and also make my coding more exciting. In this post I will present the way Groovy became part of my IDE, in order to start "groovy"ing.

Firstly, I installed the eclipse Groovy plugin, this was a piece of cake since I was only a few of steps away of it. Via the Marketplace of my Spring Tool Suite (STS): Help - Eclipse Marketplace - Search for Groovy -Install

Install Groovy plugin

Groovy plugin for eclipse can be also found under the following URL: Groovy Plugin for Eclipse

Since, the first step was completed, I moved to the second one and I created a new Groovy class by following the path in Eclipse: File - New - Other - Groovy - Groovy Class.

New Groovy class
  
My java code was already under a Maven project, so I followed the standard maven approach and placed my Groovy classes under src/main/groovy and src/test/groovy and keep my Java classes under src/main/java and src/test/java structure.

Maven Structure

HINT: In case the Groovy folders /src/test/groovy and /scr/main/groovy are not displayed, select your project and by right clicking select Build Path - Configure Build Path - Add Folder then include the folders on your view as:

Display Groovy Folders

Then, as a third step, I converted my project to a Groovy one by just selecting my project then by right clicking on option Configure - Convert to Groovy Project.

Convert to Groovy

Moving forward, I thought that I need to adapt Groovy in my maven word in order to compile my Groovy classes.
In order to achieve it, in the pom.xml, I included the dependency tag for Groovy:
<dependencies>
   <dependency>
     <groupId>org.codehaus.groovy</groupId>
     <artifactId>groovy-all</artifactId>
     <version>${groovyVersion}</version>
   <dependency>
 </dependencies> 
also, i needed a Groovy plugin with the goal “generateStubs”,so included the following tags:
<plugin>
  <groupId>org.codehaus.gmaven</groupId>
    <artifactId>gmaven-plugin</artifactId>
      <configuration>
       <providerSelection>1.8</providerSelection>
      </configuration>
        <executions>
         <execution>
           <goals>
           <goal>generateStubs</goal>
           <goal>compile</goal>
           <goal>generateTestStubs</goal>
           <goal>testCompile</goal>
           </goals>
         </execution>
       </executions>
</plugin>

The goal "generateStubs" was I a surprise for me when I realized that, as its name dedicate,creates java stubs of groovy classes.To understand it, consider a java class which needs a Groovy one to compile with and since maven standard compile process starts with /main and /test java packages and then with the /main and /test Groovy packages, compilation would stop with errors without it.

Finally, on my maven project i run Maven target "clean Install" to install all the dependencies and compile my source Java and Groovy code and i was ready to start rocking with Groovy…..That's all folks!!


Read More

Monday, January 14, 2013

Groovy up your test scripts

After one and a half year of developing test scripts using java a colleague in Persado convinced me to give a shot to groovy. Playing around for couple of weeks and there you are; I was convinced that groovy would be the new exciting thing in my testing life. Taming my excitement I decided to strategically replace blocks of java code with groovy. The plan was not to replace code in page objects neither test scripts but rather code in the supporting functions in abstraction layers between the test scripts and the page objects.
The decision in replacing the particular pieces of code was driven by the way groovy handles Lists, Arrays and Maps. In addition the effortless way of creating and using objects in a less wordy way than java showed the way.
Setting up groovy in my test environment was easy and I will show how in a feature post. With my environment ready and iron maiden in my youtube I started. First stop an object creation to carry data to a JMS dispatcher there you are.

import org.joda.time.DateTime
class MyObject {
String myString;
List myList;
Date myDate;
}

kaboom no setters no getters that simple how not to love the simplicity especially if your are not a developer. Now lets use the object, fill it up with data.

def rangeEvents = (11223300000 .. 11223300500).collect { it.toString()}
ldt = new DateTime()
def myObject = new MyObject(myList:rangeEvents,myString:"Hello",myDate:ldt);

Point 1: The list kind was never declared in the object definition.
Point 2: Assignment is as easy as myList:rangeEvents
Point 3: Ranges are Lists and each element of the list can be manipulated by a single statement
list.collect { it.toString()}

Of course it is expected that there would be people asking why go with groovy while I can do the same things with java ? Yes using groovy will not add extra arrows in your quivers but it can simplify your code and save you a lot of extra lines of code. Let me give you an example.

In one of my tests I wanted to simulate the following scenario

There are 200 people owning a cat, 300 owning a dog 140 owning a lion and 230 owning a tiger. From the people owning the cat I want 15 of them to sent a letter and likewise 63 letters from the dog owners 22 from the lion owners and 33 from the tiger owners.”

The code I had developed with java spanned in several lines


int catLetters=0;
String[] catOwners = new String[15];
int dogLetters=0;
String[] dogOwners = new String[63];
int lionLetters=0;
String[] lionOwners = new String[22];
int tigerLetters=0;
String[] tigerOwners = new String[33];
for (String data : petOwners) {
   if (data.getOwner().equals(“cat”) && catLetters<15) {
      catOwners[catLetters] = data.getOwnerName()
      catLetters = catLetters + 1;
   }
   if (data.getOwner().equals(“dog”) && dogLetters<63) {
      dogOwners[dogLetters] = data.getOwnerName()
      dogLetters = dogLetters + 1; 
   }
   if (data.getOwner().equals(“lion”) && lionLetters<22) {
      lionOwners[lionLetters] = data.getOwnerName()
      lionLetters = lionLetters + 1;
   }
   if (data.getOwner().equals(“tiger”) && tigerLetters<33) {
      tigerOwners[tigetLetters] = data.getOwnerName()
      tigetLetters = tigerLetters + 1;
   }
}

Now with groovy we can create a Map with owners and expected number of letters lettersOwnersMap


def petOwnersList = []
requestedMessageResponses.each {
   for(String data : petOwners){
      if(lettersOwnersMap(it.key) && it.value>0){
         petOwnersList.add(data.ownerName)
         it.value--
      }
   }
}

Point 1: With the java implementation the code is static meaning that if we want to add a pet owner we need to add extra if statements. In the groovy implementation we only need to add an extra map entry that simple.

Update: In this post, check how to integrate Groovy in your tests while working with Eclipse.
Read More