You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JMeter中使用WebSocket Single Read Sampler捕获所有响应标签

Solution to Capture All Tags from WebSocket Single Read Sampler Responses in JMeter

I’ve run into this exact frustration before—regular expression extractors default to grabbing only the last match, which doesn’t cut it when you need every tag from WebSocket responses. Here are two solid fixes to solve this:

Method 1: Tweak the Regular Expression Extractor

The simplest fix is adjusting the built-in extractor to capture all matches instead of just the last one:

  • Add a Regular Expression Extractor as a child of your WebSocket Single Read Sampler.
  • Set a clear Reference Name (e.g., captured_tags).
  • Enter your tag-matching regex—use non-greedy mode like <your-target-tag>(.*?)<\/your-target-tag> to avoid over-matching unrelated content.
  • Critical step: Set Match No. to -1—this tells JMeter to capture every occurrence of your pattern instead of just the last one.
  • You can then access individual tags using ${captured_tags_1}, ${captured_tags_2}, etc. To loop through all of them, pair this with a Loop Controller and a Counter component (use ${__V(captured_tags_${counter})} to reference each tag dynamically).

Method 2: Use JSR223 PostProcessor for Full Control

If you need more flexibility (like storing tags in a list, writing to a file, or custom processing), a Groovy-based JSR223 PostProcessor is your best bet:

  1. Add a JSR223 PostProcessor as a child of your WebSocket sampler.
  2. Paste this Groovy script (update the regex to match your actual tag structure):
// Fetch the full WebSocket response content
def responseText = prev.getResponseDataAsString()

// Define your tag-matching pattern (customize this to your specific tags)
def tagPattern = ~/<your-tag>(.*?)<\/your-tag>/
def matcher = tagPattern.matcher(responseText)

def allTags = []
// Collect every matching tag from the response
while (matcher.find()) {
    allTags.add(matcher.group(1)) // Group 1 captures the content inside the tag
}

// Option 1: Store all tags as a comma-separated string in a single variable
vars.put("all_tags", allTags.join(","))

// Option 2: Store each tag as an individual indexed variable (all_tag_1, all_tag_2, etc.)
allTags.eachWithIndex { tag, idx ->
    vars.put("all_tag_${idx + 1}", tag)
}

// Optional: Log captured tags to verify they're being collected correctly
log.info("Captured ${allTags.size()} tags: ${allTags}")
  • This script will capture every matching tag, and you can access them either as a single comma-separated variable or individual indexed variables based on your needs.

Quick Checks to Avoid Headaches

  • Ensure your WebSocket Single Read Sampler is configured to receive all responses—double-check timeout settings and make sure it’s not stopping after the first message.
  • Test your regex first: Use the View Results Tree listener’s regex tester to confirm it matches all your target tags correctly before applying it to the extractor or script.

内容的提问来源于stack exchange,提问作者Prakash Ranasinghe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 20:12:38