JMeter Beanshell 采样器缓存图像

JMeter Beanshell sampler caches image

我正在使用 JMeter (2.13 r1665067) 在登录和注册时使用 Google Kaptcha 测试站点,直到它们可以在测试环境中被禁用。我已经录制了一个会话并设置了一个保存响应到文件采样器以提取 kaptcha 图像。然后我让 Beanshell 采样器显示它,这样我就可以根据需要输入代码 (thanks to this post)。

我现在 运行 遇到的问题是重复显示从服务器检索到的第一张图像。我已经尝试将在 Beanshell 中创建的任何对象设置为 null post-use,并检查 "Reset bsh.Interpreter before each call".

我可以通过使用 $__(Random) 函数在将响应保存到文件采样器中创建时将唯一 ID 附加到每个图像来解决问题,但会导致创建大量文件.我可以验证保存的图像文件在文件系统上是否发生了变化。我也可以重新启动 JMeter,或从文件系统中清除文件以使其正确显示,但仅限于第一次。通过“保存响应”向文件采样器添加时间戳不够独特,但无论如何都会创建其他文件。

我想知道为什么 JMeter 似乎在缓存图像,如果有一种方法可以每次写入和读取一个文件,通过附加一个唯一的 ID 来避免大量的图像.我想这与我的配置有关。

Beanshell 采样器代码:

filenameOrURL = new URL("file://${FILE2}");
image = Toolkit.getDefaultToolkit().getImage(filenameOrURL);
icon = new javax.swing.ImageIcon(image);
pane = new JOptionPane("Enter Captcha", 0, 0, null);
String captcha = (String)pane.showInputDialog(null, "Captcha", "Captcha", 0, icon, null,null);
filenameOrURL = image = pane = icon = null;
log.info(captcha);
vars.putObject("captcha",captcha);

保存对文件采样器参数的响应:

Filename prefix: /response/response_
Variable name: FILE

线程组:

如果我的声誉先于我,我会 post 一张图片。 :脸红:

  Recording Controller
      login.html (GET)
        Save Responses to a file
      BeanShell Sampler
      login.html (POST)
      logout.html (GET)

您的问题不在于 JMeter,而在于 Toolkit.getImage() 函数。来自 its documentation:

Returns an image which gets pixel data from the specified file, whose format can be either GIF, JPEG or PNG. The underlying toolkit attempts to resolve multiple requests with the same filename to the same returned Image.

Since the mechanism required to facilitate this sharing of Image objects may continue to hold onto images that are no longer in use for an indefinite period of time, developers are encouraged to implement their own caching of images by using the createImage variant wherever available. If the image data contained in the specified file changes, the Image object returned from this method may still contain stale information which was loaded from the file after a prior call. Previously loaded image data can be manually discarded by calling the flush method on the returned Image.