How to force Jmeter to collect chart screenshots after tests
[*]My name is Ilya Ulizko, I am engaged in load testing of RBS LE in the Digital Transformation Block of RSHB-Intech. In this article, I will share with you the experience of automating the collection of graphs in Grafana in the absence of the grafana-image-render plugin installed on the server. In order to teach Apache Jmeter to take screenshots of panels in Grafana, we need selenium And Browsermob-proxy.

[*]As a result, we get an archive with more than 40 jar files, which we put in apache-jmeter-5.4.3/lib/junit/.
For flexibility, I recommend downloading both drivers and installing both browsers.
After everything is downloaded, put the drivers in a separate folder WebDriver/bin.
echo 'export PATH=$PATH:/path/to/WebDriver/bin' >> ~/.profile
echo 'export API_GRAFANA=eyJblablablablablablablablabla9' >> ~/.profile
source ~/.profile
$.panels[*].[id,title]
and online service get a list of all panel id’s. We will further transfer this array to the PANELIDS variable.props.put("TESTSTART",'${__time()}')
Then we set up parameterized variables:
final String URL = "http://10.10.10.10:3000/d/";
final String DASHBOARD = "VQB0x5pVz/settings";
final int[] PANELIDS = new int[]{26,28,38,37,29,4,2,23,39,40,36,25,27}; final String FROM = '${__P(TESTSTART)}';
final String TO = '${__time()}';
final String SELECTOR = "panel-container";
final String API_KEY = System.getenv("API_GRAFANA");
final double SCALE_MONITOR = 1;
final SimpleDateFormat formatter = new SimpleDateFormat("'_'dd.MM.yyyy");
Pay attention to the SCALE_MONITOR variable, it is needed to level out the scaling of high resolution monitors. For example, if you have a monitor with a resolution of 4K and a scale of 150% is set in the screen settings, then the value of this variable will be 1.5, for a scale of 125% you need to set it to 1.25, and so on.BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.setTrustAllServers(true);
proxy.start();
proxy.addHeader("Authorization", "Bearer ".concat(API_KEY));
WebDriver driver;
DesiredCapabilities caps = new DesiredCapabilities();
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
System.setProperty("sun.java2d.uiScale", "1");
System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "null");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.addArguments("-private");
caps.setCapability("moz:firefoxOptions", firefoxOptions);
caps.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
caps.setCapability(CapabilityType.PROXY, seleniumProxy);
driver = new FirefoxDriver(caps);
Next, save each screenshot under its own name in the specified directory:
driver.manage().window().maximize();
for (int i = 0; i < PANELIDS.length; i++) {
driver.navigate().to(URL
.concat(DASHBOARD)
.concat("?orgId=1&viewPanel=")
.concat(String.valueOf(PANELIDS[i]))
.concat("&from=")
.concat(FROM)
.concat("&to=")
.concat(TO));
Thread.sleep(2000);
WebElement elem = new WebDriverWait(driver, 5)
.until(ExpectedConditions
.visibilityOfElementLocated(By.className(SELECTOR)));
File screenshot = ((org.openqa.selenium.TakesScreenshot) driver)
.getScreenshotAs(org.openqa.selenium.OutputType.FILE);
BufferedImage image = ImageIO.read(screenshot);
int x = (int) (elem.getLocation().getX() * SCALE_MONITOR);
int y = (int) (elem.getLocation().getY() * SCALE_MONITOR);
int width = (int) (elem.getSize().getWidth() * SCALE_MONITOR);
int height = (int) (elem.getSize().getHeight() * SCALE_MONITOR);
BufferedImage elemScreenshot = image.getSubimage(x, y, width, height);
ImageIO.write(elemScreenshot, "png", new File("/path/GrafanaScreenshots/"
.concat(elem.getAttribute("aria-label")
.substring(0,elem.getAttribute("aria-label").length()-6))
.concat(formatter.format(System.currentTimeMillis()))
.concat(".png")));
}
driver.quit();
proxy.stop();
[*]Thus, we managed to automate the process of collecting graphs from the Grafana dashboard after the HT.
[*]I hope that this material was useful to those who are engaged in load testing and work with Apache Jmeter. I will be glad to answer your questions in the comments.