如何向WebDriver Sampler传递CSV文件数据并实现多用户独立登录的性能测试
Hey there! Let's break down exactly how to get your 30 unique users logging in simultaneously using JMeter's WebDriver Sampler and CSV Data Set Config. I’ve tackled this exact scenario multiple times, so here’s a step-by-step guide to make it work smoothly:
1. Configure CSV Data Set Config Correctly
First, let’s make sure your CSV Data Set Config is set up to feed unique credentials to each thread:
- Filename: Point to your CSV file (use a relative path like
./credentials.csvif the file is in JMeter’sbinfolder for portability). - Variable Names: Enter
username,password(match these to the columns in your CSV—first column = username, second = password). - Delimiter: Use
,(or whatever separator your CSV uses, like;if needed). - Recycle on EOF: Set to
False—we don’t want threads reusing credentials once the CSV is exhausted. - Stop thread on EOF: Set to
True—this ensures each thread stops after using its unique credential set (perfect for 30 threads and 30 CSV rows). - Sharing Mode: Keep it as
All threadsso every thread gets a unique row from the CSV.
2. Set Up Your Thread Group for Concurrent Logins
Your Thread Group controls how many browser instances launch and when:
- Number of Threads (users): Set to
30—this will spawn 30 separate browser instances. - Ramp-Up Period: Set to
0if you want all 30 users to start logging in at the exact same time. If you prefer a gradual launch (e.g., spread over 10 seconds), use10instead. - Loop Count: Set to
1—each user only needs to log in once.
3. Update Your WebDriver Sampler Script to Use CSV Variables
Modify your existing JavaScript login script to pull credentials from the CSV variables instead of hardcoding them. Here’s how to adjust it:
Original Hardcoded Script (Example)
// Old code with fixed credentials var usernameField = WDS.browser.findElement(pkg.By.id("username")); usernameField.sendKeys("user1"); var passwordField = WDS.browser.findElement(pkg.By.id("password")); passwordField.sendKeys("pass1"); var loginBtn = WDS.browser.findElement(pkg.By.id("login-button")); loginBtn.click();
Updated Script with CSV Variables
// Pull unique credentials from CSV Data Set Config var user = vars.get("username"); var pass = vars.get("password"); // Locate username field and input the value var usernameField = WDS.browser.findElement(pkg.By.id("username")); usernameField.sendKeys(user); // Locate password field and input the value var passwordField = WDS.browser.findElement(pkg.By.id("password")); passwordField.sendKeys(pass); // Click the login button (adjust the selector to match your site) var loginBtn = WDS.browser.findElement(pkg.By.id("login-button")); loginBtn.click();
Key note: The vars.get("variableName") method is JMeter’s way of accessing variables defined in components like CSV Data Set Config. Each thread will automatically pull its own unique row from the CSV.
4. Critical Tips to Avoid Common Issues
- Browser Driver Setup: Ensure you’ve downloaded the correct driver (e.g., ChromeDriver for Chrome) and placed it in JMeter’s
binfolder, or configure the path in JMeter’ssystem.propertiesfile. - Resource Management: 30 browser instances can eat up a lot of RAM. If your machine struggles, use headless mode to reduce resource usage. For Chrome, add this to your WebDriver Sampler’s browser arguments:
--headless=new. - Verify Results: Add a
View Results Treelistener to check each thread’s execution. You’ll see that each thread uses a uniqueusernameandpasswordfrom your CSV. - Avoid Duplicate Logins: Double-check that your CSV has exactly 30 unique rows (no duplicates) and that
Recycle on EOFis set toFalse—this prevents threads from reusing credentials.
That’s it! When you run your test, JMeter will spawn 30 browser instances, each using a unique set of credentials from your CSV to log into the site simultaneously.
内容的提问来源于stack exchange,提问作者James P.

