Monday, October 21, 2013

Jenkins check if selenium server is fine and no false postitives/errors in email before email-ext email is sent

This script is a groovy script that helps you detect if you selenium server is up and running and the state is success.

#!/usr/local/bin/groovy

import groovy.json.JsonSlurper

if ( "curl -s http://10.5.5.57:4444/wd/hub/status --max-time 15".execute().text == "" ) {
        println "Cannot connect to selenium server"
        return false
}

println "Connected to selenium server"

def restResponse = "curl -s http://10.5.5.57:4444/wd/hub/status --max-time 15".execute().text
def list = new JsonSlurper().parseText( restResponse )

if ( list.state == "success" ) {
        println "Server is running fine"
        return 0

} else {
        println "Server state found down"
        return 1
}

You can configure it as a Pre-send Script (NOT Pre Build Script Trigger) in Jenkins for your testng-selenium-java job You would need to install groovy though on your linux box to test it as a script. This will ensure that your tests are run only when the selenium server is alive.

A shell equivalent of doing so. But found this wouldn't help me in jenkins. May help you elsewhere

isSelServerUp=`curl -i -s http://10.5.5.57:4444/wd/hub/status | grep -e "\"state\":\"success\""`
echo "selenium server:\n"
echo $isSelServerUp
if [ -z "$isSelServerUp" ]; then
        echo 'Selenium server is not running at http://10.5.5.57:4444/wd/hub/status'
        exit 1
else
        echo "Selenium server found in good state"


fi

Update:
However post this I found problems with test emails being generated even if there were problems with machine access etc. So to suppress emails when no tests were run modified it tot he following-

#!/usr/local/bin/groovy

import groovy.json.JsonSlurper
import java.util.regex.Matcher
import java.util.regex.Pattern

String jobLog = build.getLog(20000);

def m = jobLog =~ /Test reports were found but none of them are new. Did tests run/;
def n = jobLog =~ /Tests run: 0, Failures: 0, Errors: 0, Skipped: 0/;

if (m.size())
{
        logger.println("Tests Did Not Run. Not Sending Email. Found Line - " + m[0])
        cancel=true;
}else if (n.size())
{
        logger.println("Tests Did Not Run 0 0 0. Not Sending Email. Found Line - " + m[0])
        cancel=true;
}else if( "curl -s http://10.5.5.57:4444/wd/hub/status --max-time 15".execute().text == "" ) {
        logger.println "Cannot connect to selenium server - Not sending email";
        cancel=true;
}else{
        logger.println "Connected to selenium server and confirmed that tests ran - Cleared to send email";
}


This worked great. Further inspired from http://massol.myxwiki.org/xwiki/bin/view/Blog/JenkinsDontSendEmailForFalsePositives, checked all conditions via a list-

#!/usr/local/bin/groovy

import groovy.json.JsonSlurper
import java.util.regex.Matcher
import java.util.regex.Pattern

String jobLog = build.getLog(20000);

def messages = [
  [".*A fatal error has been detected by the Java Runtime Environment.*", "JVM Crash", "A JVM crash happened!"],
  [".*Error: cannot open display: :1.0.*", "VNC not running", "VNC connection issue!"],
  [".*java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment.*", "VNC issue", "VNC connection issue!"],
  [".*hudson.plugins.git.GitException: Could not fetch from any repository.*", "Git issue", "Git fetching issue!"],
  [".*Error communicating with the remote browser. It may have died..*", "Browser issue", "Connection to Browser has died!"],
  [".*Error communicating with the remote browser. It may have died..*", "Browser issue", "Connection to Browser has died!"],
  [".*UnreachableBrowser Error.*", "Browser issue", "Browser has died!"],
  [".*Fatal Error: Unable to find package java.lang in classpath or bootclasspath.*", "Compilation issue", "Compilation issue!"],
  [".*Tests run: 0, Failures: 0, Errors: 0, Skipped: 0.*", "No Tests Ran", "Tests Did Not Run!"],
  [".*Test reports were found but none of them are new. Did tests run.*", "Tests did not run", "No Tests Ran!"]
]

def shouldSendEmail = true
messages.each { message ->
  if (jobLog =~ message.get(0)) {
    logger.println message.get(0)
    logger.println message.get(1)
    logger.println message.get(2)
    shouldSendEmail = false
  }
}

if( "curl -s http://10.5.5.57:4444/wd/hub/status --max-time 15".execute().text == "" ) {
    logger.println "Cannot connect to selenium server";
    shouldSendEmail = false
}else{
        logger.println "Connected to selenium server";
}

if(!shouldSendEmail){
        logger.println "Connected to selenium server and No log errors - Not sending email";
        cancel=true;
}

Buzz me out for any questions!
Joviano Dias ~joviano_dias@hotmail.com


No comments:

Post a Comment