Jagger
 All Classes Namespaces Files Functions Variables Groups Pages
Custom listener examples code

Listeners main page

Custom test listener Custom test suite listner
/* begin: following section is used for docu generation - custom test listener */
public class ProviderOfTestListener extends ServicesAware implements Provider<TestListener> {
private static final Logger log = LoggerFactory.getLogger(ProviderOfTestListener.class);
private String commentString = "";
// Method will be executed single time, when listener provider is initiated
@Override
protected void init() {
// begin: following section is used for docu generation - example of metric creation
// In this example, we will create custom metric to collect internal metrics of SUT
// Approach is the following:
// - Declare custom metric
// - Run test. SUT will usually collect some internal data (metrics)
// - After test is finished, get all internal data from SUT and save to custom metric
// In this case internal data will be save to result DB, displayed in test report and Web UI,
// you can use it in decision maker when running Continues Integration
MetricDescription metricDescription = new MetricDescription("internalData");
metricDescription.plotData(false).showSummary(true).addAggregator(new AvgMetricAggregatorProvider());
getMetricService().createMetric(metricDescription);
// end: following section is used for docu generation - example of metric creation
}
// Method will provide custom test listener to Jagger
@Override
public TestListener provide() {
return new TestListener() {
// Method will be executed before starting of test
@Override
public void onStart(TestInfo testInfo) {
// We will take no actions in this example
}
// Method will be executed after finishing test
@Override
public void onStop(TestInfo testInfo) {
long someMetric = 0;
// Test is finished and now we are ready to get some internal data from SUT
// Unfortunately our example is very simple and no data is available
// So we will just ask what Google knows about Jagger and save number of found results as metric
// Send request to google
String url = "https://www.google.com/search?q=griddynamics+jagger";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try {
HttpResponse response = client.execute(request);
// Parse response
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
if (line.contains("id=\"resultStats\"")) {
// How many results google have found
Pattern pattern = Pattern.compile("id=\"resultStats\">About(.*?)results</div>");
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
someMetric = Long.parseLong(matcher.group(1).replaceAll(",","").replace(" ",""));
}
}
}
}
catch (Exception e) {}
// Save metric
getMetricService().saveValue("internalData",someMetric);
}
// Method will be executed periodically during test run
@Override
public void onRun(TestInfo status) {
// We will take no actions in this example
}
};
}
}
/* end: following section is used for docu generation - custom test listener */
/* begin: following section is used for docu generation - custom test suite listener */
public class ProviderOfTestSuiteListener extends ServicesAware implements Provider<TestSuiteListener> {
private static final Logger log = LoggerFactory.getLogger(ProviderOfTestSuiteListener.class);
// Method will be executed single time, when listener provider is initiated
@Override
protected void init() {
// In this example, we will take version of Jagger artifact from pom file
// and save this value to Session comment
// In the same way you can save version(s) of SUT artifacts into session comment
String POM_JAGGER_VERSION = "jagger.version";
String pomfile = "../../pom.xml";
MavenXpp3Reader mavenXpp3Reader = new MavenXpp3Reader();
try {
FileReader reader = new FileReader(pomfile);
Model model = mavenXpp3Reader.read(reader);
getSessionInfoService().appendToComment("jagger.version: " + model.getProperties().get(POM_JAGGER_VERSION));
}
catch(Exception e){
log.warn("Cant get pom info {}", e);
}
}
// Method will provide custom test suite listener to Jagger
@Override
public TestSuiteListener provide() {
return new TestSuiteListener() {
// Method will be executed before starting of test suite
@Override
public void onStart(TestSuiteInfo testSuiteInfo) {
// Here you can f.e.:
// - provide smoke tests to check if your SUT is responding
// - check system properties required for correct SUT functionality
// In this example we will append names of nodes participating in test to session comment
// We will also print OS name and CPU model
String comment = "";
for (Map.Entry<NodeId,GeneralNodeInfo> entry : testSuiteInfo.getGeneralNodeInfo().entrySet()) {
comment += "\nNode: " + entry.getKey() + " (OS: " + entry.getValue().getOsName() + ", " + entry.getValue().getCpuModel() + ")";
}
getSessionInfoService().appendToComment(comment);
}
// Method will be executed after finishing test suite
@Override
public void onStop(TestSuiteInfo testSuiteInfo) {
// We will take no actions in this example
}
};
}
}
/* end: following section is used for docu generation - custom test suite listener */