设为首页 收藏本站
查看: 715|回复: 0

Integrate Selenium Test Cases into Nagios

[复制链接]

尚未签到

发表于 2017-4-20 09:33:39 | 显示全部楼层 |阅读模式
  The check_selenium source code can be found on github:https://github.com/czunker/check_selenium
  With this check it is possible to put your recorded test cases from your Selenium IDE into Nagios to use them for monitoring.
  In my opinion this has the following advantages:


  • You can transfer your test cases to monitoring without making any changes.
  • You are more flexible, when monitoring more complex scenarios.
  To get this working, you need the following components:



  • Nagios (I assume you have this already running)
  • check_selenium
  • Selenium IDE
  • Firefox

check_selenium Source Code:

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>info.devopsabyss</groupId>
<artifactId>check-selenium</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<project.encoding>UTF-8</project.encoding>
<jdk.version>1.7</jdk.version>
<selenium.version>2.46.0</selenium.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.6.1</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>${project.encoding}</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<encoding>${project.encoding}</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<mainClass>info.devopsabyss.CallSeleniumTest</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<descriptors>
<descriptor>src/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>


assembly.xml
<assembly>
<id>bin</id>
<formats>
<!--format>zip</format-->
<format>dir</format>
</formats>
<baseDirectory>libexec</baseDirectory>
<dependencySets>
<dependencySet>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>src/main/scripts</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>check_selenium.sh</include>
</includes>
</fileSet>
</fileSets>
</assembly>

 CallSeleniumTest.java
package info.devopsabyss;
/*
* This is a nagios plugin to integrate Selenium Test Cases into Nagios.
* Copyright (C) 2010 Christian Zunker (devops.abyss@googlemail.com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*
*/
import org.apache.commons.cli.*;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import java.util.List;
public class CallSeleniumTest {
private static final int NAGIOS_OK = 0;
private static final int NAGIOS_WARNING = 1;
private static final int NAGIOS_CRITICAL = 2;
private static final int NAGIOS_UNKNOWN = 3;
private static final String NAGIOS_TEXT_OK = "OK";
private static final String NAGIOS_TEXT_WARNING = "WARNING";
private static final String NAGIOS_TEXT_CRITICAL = "CRITICAL";
private static final String NAGIOS_TEXT_UNKNOWN = "UNKNOWN";
private Options options = null;
private Result runJUnitTest(String className) throws ClassNotFoundException {
Class<?> seleniumTestClass = Class.forName(className);
return new JUnitCore().run(seleniumTestClass);
}
public static void main(String[] args) throws Exception {
CallSeleniumTest seTest = new CallSeleniumTest();
Option optionClass = new Option("c", "class", true, "full classname of test case (required) e.g. \"com.example.tests.GoogleSeleniumWebdriverTestCase\"");
Option optionVerbose = new Option("v", "verbose", false, "show a lot of information (useful in case of problems)");
Option optionHelp = new Option("h", "help", false, "show this help screen");
seTest.options = new Options();
seTest.options.addOption(optionClass);
seTest.options.addOption(optionVerbose);
seTest.options.addOption(optionHelp);
CommandLineParser parser = new DefaultParser();
CommandLine cmd = null;
String output = seTest.NAGIOS_TEXT_UNKNOWN + " - Upps";
int nagios = seTest.NAGIOS_UNKNOWN;
try {
cmd = parser.parse(seTest.options, args);
// has to be checked manually, otherwise you can't access the help message without specifying correct parameters
if (cmd.hasOption("h") || cmd.getOptionValue("c") == null) {
usage(seTest.options);
System.exit(nagios);
}
Result result = seTest.runJUnitTest(cmd.getOptionValue("c"));
if (result.wasSuccessful()) {
output = seTest.NAGIOS_TEXT_OK + " - " + cmd.getOptionValue("c") + " Tests passed - ExecTime=" + result.getRunTime() + "ms";
nagios = seTest.NAGIOS_OK;
} else {
printStackTraceWhenVerbose(cmd, result.getFailures());
String failureMessage = result.getFailures().toString();
output = seTest.NAGIOS_TEXT_CRITICAL + " - " + cmd.getOptionValue("c") + " Test Failures: " + withoutNewlines(failureMessage) + " - ExecTime=" + result.getRunTime() + "ms";
nagios = seTest.NAGIOS_CRITICAL;
}
}  catch (ParseException ex) {
output = seTest.NAGIOS_TEXT_UNKNOWN + " - Parameter problems: " + messageWithoutNewlines(ex);
nagios = seTest.NAGIOS_UNKNOWN;
usage(seTest.options);
} catch (ClassNotFoundException ex) {
output = seTest.NAGIOS_TEXT_UNKNOWN + " - Test case class: " + messageWithoutNewlines(ex) + " not found!";
nagios = seTest.NAGIOS_UNKNOWN;
} catch (Exception ex) {
printStackTraceWhenVerbose(cmd, ex);
output = seTest.NAGIOS_TEXT_WARNING + " - " + cmd.getOptionValue("c") + ": " + messageWithoutNewlines(ex);
nagios = seTest.NAGIOS_WARNING;
} finally {
System.out.println(output);
System.exit(nagios);
}
}
private static String messageWithoutNewlines(final Throwable ex) {
return withoutNewlines(ex.getMessage());
}
private static String withoutNewlines(final String message) {
return message.replaceAll("\r\n", " ").replaceAll("\n", " ");
}
private static void printStackTraceWhenVerbose(final CommandLine cmd, final Throwable ex) {
if (cmd.hasOption("v")) {
ex.printStackTrace();
}
}
private static void printStackTraceWhenVerbose(final CommandLine cmd, final List<Failure> failures) {
for (Failure failure : failures) {
printStackTraceWhenVerbose(cmd, failure.getException());
}
}
private static void usage(Options options) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("check_selenium", options);
System.out.println("This version of check_selenium was tested with:");
System.out.println("  - selenium 2.46.0");
System.out.println("  - selenium ide 2.5.0");
System.out.println("  - test case exported as Java / JUnit 4 / WebDriver");
System.out.println("Some example calls:");
System.out.println(" ./check_selenium.sh -c \"com.example.tests.GoogleSeleniumWebdriverTestCase\"");
System.out.println(" ./check_selenium.sh --class \"com.example.tests.GoogleSeleniumWebdriverTestCase\"");
}
}


check_selenium.sh
#!/bin/bash
export DISPLAY=:0
#xhost + // 当出现7054错误时,在图形界面运行此命令
JAVA_HOME=/opt/java/jdk1.7.0_75
DIR=$(/usr/bin/dirname ${0})
${JAVA_HOME}/bin/java -jar ${DIR}/lib/check-selenium-1.0.jar $@
 
Install Selenium IDE
Selenium IDE is a fully-featured Integrated Development Environment (IDE) that installs as a plugin in Mozilla Firefox and enables developers to test their web applications through Selenium. With the Selenium IDE, you can record user interactions with the web browser and play them back to test for errors.


Download latest released version from addons.mozilla.org or view the Release Notes and then install some plugins.

DSC0000.png

Record Test Case

Click the Selenium IDE button to open the Selenium IDE window.

DSC0001.png
 

  • Create a new test case by going to File -> New Test Case.
  • Hit the red record switch button to record our test.
  • After recording it, export it to a Java file (File -> Export Test Case As ... -> Java  / JUnit4 / WebDriver).



Compile this Java file with javac or your favorite IDE.

Deploy to Nagios


On your Nagios host, put check_selenium.sh into your libexec path, add check-selenium.jar and other dependencies in the libexec/lib path.
As the Selenium IDE puts the Java classes automatically in the package com.example.tests,
create a directory com/example/tests on your filesystem and add it to the classpath. Put the compiled Java file into this directory.

 

Define Command and Service

Add the definition to your nagios configuration and test it.

define command {
command_name  check_selenium
command_line  $USER1$/check_selenium.sh -c $ARG1$
}
 

After that, you can define your service and asign it to a host:

define service {
service_description  selenium_Google
use                  service-check-05min
host_name            your.server.here
check_command        check_selenium!com.example.tests.GoogleTestCase
}
 

FAQ

How to confirm the param "DISPLAY"?

Log into the graphical interface:



echo $DISPLAY




Unknown job type: 10

 

CHECK_NRPE: Received 0 bytes from daemon.  Check the remote server logs for error messages

Install nrpe:
./configure --enable-command-args

 

NRPE installation error: configure: error: Cannot find ssl headers

yum install openssl-devel

 

Reference

Nagios Installation Guides

Nagios Core Documentation

NRPE Documentation

Flexible Notifications for Nagios

How To Set Up a Firewall Using FirewallD on CentOS 7

(systemctl start firewalld.service

 firewall-cmd --state

 firewall-cmd --zone=public --add-port=5000/tcp)

Configure Linux to use NTLM authentication proxy (ISA Server) using CNTLM

Enabling NTLM Authentication (Single Sign-On) in Firefox

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-366667-1-1.html 上篇帖子: [转]Ganglia 和 Nagios,第 2 部分: 使用 Nagios 监视企业集群 下篇帖子: 【转】Nagios:企业级系统监控方案
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表