Saturday, August 11, 2012

Getting Robot Framework Results in the Email from Jenkins

The report and log pages generated by Robot Framework are great, but it is also nice to have an executive summary of the test results. If you use the Robot Framework Plugin and Email-ext plugin for Jenkins, you can get results in the email body that look like this:

Robot Framework Results


Detailed Report
Pass Percentage: 80.0%
Test Name Status Execution Datetime
Customer
Customer.Ordering
Place Credit Card Order PASS Mon Jul 30 16:17:25 CDT 2012
Save to Wishlist PASS Mon Jul 30 16:19:42 CDT 2012
Place Paypal Order PASS Mon Jul 30 16:29:01 CDT 2012
Customer.History
View Last Order PASS Mon Jul 30 16:32:00 CDT 2012
Shipping Status FAIL Mon Jul 30 16:38:02 CDT 2012

Here is a snippet from the Groovy template that generated the above:

 <%  
 import java.text.DateFormat  
 import java.text.SimpleDateFormat  
 %>  
 <!-- Robot Framework Results -->  
 <%  
 def robotResults = false  
 def actions = build.actions // List<hudson.model.Action>  
 actions.each() { action ->  
  if( action.class.simpleName.equals("RobotBuildAction") ) { // hudson.plugins.robot.RobotBuildAction  
   robotResults = true %>  
 <p><h4>Robot Framework Results</h4></p>  
 <p><a href="${rooturl}${build.url}robot/report/report.html">Detailed Report</a></p>  
 <p>Pass Percentage: <%= action.overallPassPercentage %>%</p>  
 <table cellspacing="0" cellpadding="4" border="1" align="center">  
 <thead>  
 <tr bgcolor="#F3F3F3">  
  <td><b>Test Name</b></td>  
  <td><b>Status</b></td>  
  <td><b>Execution Datetime</b></td>  
 </tr>  
 </thead>  
 <tbody>  
 <% def suites = action.result.allSuites  
   suites.each() { suite ->   
    def currSuite = suite  
    def suiteName = currSuite.displayName  
    // ignore top 2 elements in the structure as they are placeholders  
    while (currSuite.parent != null && currSuite.parent.parent != null) {  
     currSuite = currSuite.parent  
     suiteName = currSuite.displayName + "." + suiteName  
    } %>  
 <tr><td colspan="3"><b><%= suiteName %></b></td></tr>  
 <%  DateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss.SS")
    def execDateTcPairs = []
    suite.caseResults.each() { tc ->  
      Date execDate = format.parse(tc.starttime)
      execDateTcPairs << [execDate, tc]
    }
    // primary sort execDate, secondary displayName
    execDateTcPairs = execDateTcPairs.sort{ a,b -> a[1].displayName <=> b[1].displayName }
    execDateTcPairs = execDateTcPairs.sort{ a,b -> a[0] <=> b[0] }
    execDateTcPairs.each() {
      def execDate = it[0]
      def tc = it[1]  %>
 <tr>  
  <td><%= tc.displayName %></td>  
  <td style="color: <%= tc.isPassed() ? "#66CC00" : "#FF3333" %>"><%= tc.isPassed() ? "PASS" : "FAIL" %></td>  
  <td><%= execDate %></td>  
 </tr>  
 <%  } // tests  
   } // suites %>  
 </tbody></table><%  
  } // robot results  
 }  
 if (!robotResults) { %>  
 <p>No Robot Framework test results found.</p>  
 <%  
 } %>  
The date conversion is there to convert the date from the format Robot uses to the Java default format, which is more familiar to us.

This is the first thing I have ever done in Groovy, and I must say it is a pleasant language to write in. I especially like how getters and setters are mapped to the Groovy concept of properties.

Happy roboting.

29 comments:

  1. is this the groovy script or the template?
    i tried to put your script in the default location and use:
    ${SCRIPT, robot-mail.groovy}

    no luck

    ReplyDelete
    Replies
    1. It is a template. The value I use for Default Content is:
      ${SCRIPT, script="email-ext.groovy", template="with_results.groovy", init="false"}
      A script that includes the content in this post and a little more is saved in {Jenkins installation folder}/email-templates/with_results.groovy
      Hope this helps.

      Delete
    2. thank you for your quick replay !

      now the script is loaded but it cannot find the robot results:

      No Robot Framework test results found.

      heres an example of report.html link
      http://172.21.161.162:8080/job/7-Global-Report-92/19/label=master/robot/report/report.html

      should i edit some thing in the script? (ps i am using windows for jenkins master)
      thank you for your help !

      Delete
    3. The Publish Robot Framework test results must appear before the Editable Email Notification action in the build configuration. Another thing I think that could perhaps be problematic is a matrix build. I would create a dummy job that runs one test and publishes it to troubleshoot. Good Luck.

      Delete
  2. Hi Kevin

    Thanks for your script, this help me so much.
    I need your help, when the script make the table, don't use any order to put the tests. You know how created the table by the run order?

    Many regards
    Filipe Duarte

    ReplyDelete
  3. I'm glad this was of use to you. For a while I have wanted to put some sorting in. I updated the code to sort the test cases within each suite, but the suites are still un-ordered.

    ReplyDelete
    Replies
    1. Hi Kevin,

      I am new to this groovy and robot stuff. can you please explain on how did you make the email-ext.groovy? is it available with jenkins installation by default or something that has to be written from scratch?

      Thanks,
      SWAMY

      Delete
  4. Hi Kevin

    So many thanks, I test with the new code and work fine.

    Many Regards
    Filipe Duarte

    ReplyDelete
  5. Hi Kevin,

    I am new to Robot framework and Jenkins. Will you please elaborate us how these tools are working each other and I would like to get a practical knowledge on them. Please reply as soon as possible..thanks in advance..

    ReplyDelete
    Replies
    1. I created a dummy suite with a pass and fail test. I looked at the javadocs for the Robot Framework plugin. It was just trial and error, really. I did not use an IDE.

      Delete
  6. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Hi!

      I can't put this to work in my jenkins. Can you provide the files you're using? The script and the template?

      Thanks!

      Delete
    3. See https://gist.github.com/ombre42/cb6bc804c876a7f07c45#file-gistfile1-txt

      Delete
  7. Is there possibility to show error message from robot using this script?

    ReplyDelete
  8. Thank you so much for the groovy script. I was trying to get email sent from jenkins in a way that its legible and clear. I had to spend two day trying to do it. And finally your groovy script worked like a charm. Thanks again.

    ReplyDelete
    Replies
    1. Hi,
      Could you please help me in which location this groovy script has to be placed in the jenkins

      Delete
    2. It should go in $JENKINS_ROOT/email-templates. In the instances that I have running, this is /var/lib/jenkins/email-templates.

      Also, there is an 'Email Template Testing' link within the jobs viewer where you can test the email template against any build after you've copied it to email-templates directory.

      Delete
  9. Just for convenience for any one else who would be struggling like me, I will paste the exact syntax as below :

    ${SCRIPT, template="groovy_template.groovy"}

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. I am not good at groovy scripting, but I like this code. Can someone help me modifying this script to display duration(in seconds) of each testcase execution instead of just the starttime - I meant subtracting the endtime with starttime and displaying the result in seconds

    ReplyDelete
  12. Hi Kevin
    So in Jenkins where i need paste the script and get the results in email ?
    can you please tell step by step that will be really helpful.

    Thank you

    ReplyDelete
  13. @Unknown,

    You put this code:

    ${SCRIPT, template="robot-jobs.groovy"}

    in the "Default Content" field of the "Editable Email Notification" in your job.

    The "robot-jobs.groovy" file should be placed inside your jenkins_home folder, in "email-templates" subfolder.

    ReplyDelete
    Replies
    1. Hi Zepgirus,

      Copied the ${SCRIPT, template="robot-jobs.groovy"} in "Default Content" filed in "Editable Email Notification" in my job

      And also copied the .groovy file at opt/drutt/ca/jenkins/home/plugins/emailext-template in my Jenkins server

      Now trying to run the build but it shows as Robot results publisher started...
      -Parsing output xml:
      Done!
      -Copying log files to build dir:
      Done!
      -Assigning results to build:
      Done!
      -Checking thresholds:
      Done!
      Done publishing Robot results.
      No emails were triggered.
      Finished: SUCCESS

      can you please help me

      Thanks

      Delete
    2. Do you have triggers configured under the Editable Email Notification Advanced settings in your job? If I recall, only the failure condition is enabled by default and I don't recall which 'Send To' group is bound to it. You have to add a trigger for success, unstable, etc. and also either pick a group to send those to and/or manually enter email addresses (you have to click advanced under the trigger that you are adding to manually add an address to send to)

      Delete
  14. "email-templates" subfolder, not "emailext-template". Maybe it's that?

    Can you send emails at all?

    ReplyDelete
  15. Can anyone explain me this in bit detail.I need to send the mails but getting this error when i test groovy file in templates.
    Groovy Template file [robot-jobs.groovy] was not found in $JENKINS_HOME/email-templates.
    I have created this folder then also getting same issue.

    ReplyDelete
  16. This post is so useful and informative. Keep updating with more information.....
    IELTS Certification
    IELTS Coaching

    ReplyDelete