การจัดการค่าแบบไดนามิก (การเชื่อมโยงค่า)
เรียนรู้การดึงและนำค่าแบบไดนามิกจากการตอบกลับของเซิร์ฟเวอร์กลับมาใช้ใหม่ด้วยตัวดึงข้อมูลนิพจน์ทั่วไป
การจัดการค่าแบบไดนามิก (การเชื่อมโยงค่า) เป็นบทเรียน Load Testing & Performance Benchmarking (JMeter & k6) ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Load Testing & Performance Benchmarking (JMeter & k6) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Load Testing & Performance Benchmarking (JMeter & k6) มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Dynamic Values Explained
When you interact with a web application, many values in the server's responses aren't static. These are called dynamic values.
They change with each user, session, or even each request. Think of them like temporary keys the server gives you.
- Session IDs
- Authentication Tokens
- CSRF Tokens
Why Correlation is Essential
If your performance test simply records and replays requests, it often fails. Why?
Because the recorded requests contain old, invalid dynamic values. The server rejects them, thinking you're not a legitimate user or session.
Correlation is the process of extracting these dynamic values from one response and reusing them in subsequent requests.
Meet the Regex Extractor
JMeter provides a powerful tool to handle dynamic values: the Regular Expression Extractor.
This Post-Processor component allows you to define a pattern (a regular expression) to search within a server's response and capture specific data.
It's like telling JMeter: "Find this pattern, and grab whatever is inside these brackets!"
How Extraction Works
Here's the basic flow for using a Regular Expression Extractor:
- Send Request: Your JMeter sampler sends a request to the server.
- Receive Response: The server sends back a response, which contains the dynamic value.
- Extract Value: The Regular Expression Extractor scans the response for your defined pattern.
- Store Value: It captures the dynamic data and stores it in a JMeter variable.
- Reuse Value: You then use this variable in subsequent requests.
Regex Basics for Extraction
A Regular Expression (Regex) is a sequence of characters that defines a search pattern. For extraction, you'll often use:
.(dot): Matches any single character.*(asterisk): Matches zero or more occurrences of the preceding character.?(question mark): Makes the preceding quantifier (like*) non-greedy, matching the smallest possible string.()(parentheses): Used to capture the part of the string you want to extract.
Try running this simple JavaScript example to see regex in action:
function extractId(text) {
// Regex to find "id=" followed by one or more digits
// and capture the digits.
const regex = /id=(\d+)/;
const match = text.match(regex);
return match ? match[1] : null;
}
console.log("Extracted ID:", extractId("user?id=123&name=test"));
console.log("Extracted ID:", extractId("product?id=456"));
console.log("Extracted ID:", extractId("no_id_here"));Adding the Extractor
In JMeter, the Regular Expression Extractor is a Post-Processor. This means it runs after a sampler (request) completes but before the results are recorded.
To add it:
- Right-click on the HTTP Request (or other sampler) that receives the dynamic value.
- Go to Add > Post-Processors > Regular Expression Extractor.
It will become a child of that specific request.
Extractor Configuration
The Regular Expression Extractor has several key fields:
- Reference Name: The name of the JMeter variable to store the extracted value (e.g.,
sessionId). - Regular Expression: The actual regex pattern to match (e.g.,
name="sessionId" value="(.+?)"). - Template: How to construct the value. Use
$1$for the first captured group,$2$for the second, etc. - Match No.: If multiple matches, which one to use?
0for random,1for first,-1for all (creates multiple variables). - Default Value: A value to assign if the regex doesn't find a match.
Using the Extracted Variable
Once the Regular Expression Extractor stores a value in a variable (e.g., sessionId), you can use it in any subsequent JMeter sampler or component.
To reference the variable, use the syntax ${ReferenceName}.
For example, if you extracted a session ID into sessionId, you might use ${sessionId} in the body or URL path of a follow-up request.
A Correlation Scenario
Consider a login sequence:
- Request 1 (Login): Sends username/password.
- Response 1: Contains a
CSRF_Tokenin a hidden field. - Regex Extractor: Extracts
CSRF_Tokenfrom Response 1 and stores it in${csrfToken}. - Request 2 (Submit Form): Uses
${csrfToken}in its request body or URL parameters.
This ensures your test script behaves like a real user, passing the dynamic token.
Test Your Regex Knowledge
Imagine a server response contains the following HTML snippet:
<input type="hidden" name="token" value="abc123xyz456" />You want to extract the value abc123xyz456 into a JMeter variable named authToken.
Which Regular Expression and Template combination should you use in the Regular Expression Extractor?
Recap: Mastering Correlation
Great job! You've learned the critical skill of correlation in JMeter.
- Dynamic values require special handling to prevent test failures.
- The Regular Expression Extractor is JMeter's primary tool for this.
- You define a regex pattern to capture specific data from server responses.
- Extracted values are stored in JMeter variables for reuse in subsequent requests.
This technique makes your performance tests robust and realistic. Next, we'll explore more JMeter functions and variables for even greater script flexibility!
คำถามที่พบบ่อย
บทเรียน “การจัดการค่าแบบไดนามิก (การเชื่อมโยงค่า)” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจัดการค่าแบบไดนามิก (การเชื่อมโยงค่า)” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Load Testing & Performance Benchmarking (JMeter & k6) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Load Testing & Performance Benchmarking (JMeter & k6) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจัดการค่าแบบไดนามิก (การเชื่อมโยงค่า)”
เรียนรู้การดึงและนำค่าแบบไดนามิกจากการตอบกลับของเซิร์ฟเวอร์กลับมาใช้ใหม่ด้วยตัวดึงข้อมูลนิพจน์ทั่วไป คุณปฏิบัติ Load Testing & Performance Benchmarking (JMeter & k6) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Load Testing & Performance Benchmarking (JMeter & k6) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Load Testing & Performance Benchmarking (JMeter & k6) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การจัดการค่าแบบไดนามิก (การเชื่อมโยงค่า)” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Load Testing & Performance Benchmarking (JMeter & k6) นี้ได้ไหม
ได้ บทเรียน Load Testing & Performance Benchmarking (JMeter & k6) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การกำหนดพารามิเตอร์ด้วยข้อมูล CSV
- การจัดการค่าแบบไดนามิก (การเชื่อมโยงค่า)
- ฟังก์ชันและตัวแปรของ JMeter
- การทดสอบฐานข้อมูลด้วย JDBC