public class ProviderOfTestListener extends ServicesAware implements Provider<TestListener> {
private static final Logger log = LoggerFactory.getLogger(ProviderOfTestListener.class);
private String commentString = "";
@Override
protected void init() {
MetricDescription metricDescription = new MetricDescription("internalData");
metricDescription.plotData(false).showSummary(true).addAggregator(new AvgMetricAggregatorProvider());
getMetricService().createMetric(metricDescription);
}
@Override
public TestListener provide() {
return new TestListener() {
@Override
public void onStart(TestInfo testInfo) {
}
@Override
public void onStop(TestInfo testInfo) {
long someMetric = 0;
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);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
if (line.contains("id=\"resultStats\"")) {
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) {}
getMetricService().saveValue("internalData",someMetric);
}
@Override
public void onRun(TestInfo status) {
}
};
}
}