单物理机上Selenium脚本并行执行方案及Grid节点数咨询
Hey there! Let's tackle your two questions one by one—speeding up your 2-3 hour Selenium test suite on a single machine, and figuring out the max Selenium Grid nodes you can run when hub and node are on the same box, especially since you aren't using TestNG.
Since you're not using TestNG, here are the most practical, low-fuss options tailored to different tech stacks:
Option 1: pytest-xdist (For Python-Based Frameworks)
If your framework is built with Python, pytest-xdist is hands-down the easiest way to parallelize tests. It leverages your machine's multi-core CPU without needing extra services:
- Install it via pip:
pip install pytest-xdist - Run your tests with parallelism:
pytest -n auto(theautoflag automatically uses all available CPU cores; you can also specify a number like-n 6to cap parallel processes) - Pro tip: Make sure each test case initializes and tears down its own WebDriver instance—no shared drivers, or you'll get race conditions and flaky tests.
Option 2: JUnit 5 Parallel Execution (For Java-Based Frameworks)
For Java stacks, JUnit 5 has built-in parallel support that works great without TestNG:
- Add these configs to
src/test/resources/junit-platform.properties:junit.jupiter.execution.parallel.enabled = true junit.jupiter.execution.parallel.mode.default = concurrent junit.jupiter.execution.parallel.config.strategy = dynamic - The
dynamicstrategy adjusts parallelism based on your CPU cores, but you can set a fixed number if you prefer. Again, ensure each test manages its own WebDriver lifecycle.
Option 3: Custom Multi-Process/Thread Scripts
If you're using a different language or want full control, write a simple wrapper script using your language's built-in concurrency libraries:
- For Python: Use the
multiprocessingmodule to split test cases into batches and run each batch in a separate process. - For Java: Use
ExecutorServiceto manage a thread pool that runs test cases in parallel. - Critical note: Isolate each test's WebDriver instance completely—shared resources will break your tests.
Option 4: Selenium Grid (Single Machine Hub + Node)
While it's a bit overkill for a single machine, this setup works if you might scale to multiple machines later. We'll dive into node limits next, but the basics are:
- Start the hub first:
java -jar selenium-server-4.x.x.jar hub - Then start the node on the same machine:
java -jar selenium-server-4.x.x.jar node --hub http://localhost:4444
There's no hard number—it depends entirely on your machine's hardware, specifically CPU cores, available RAM, and browser resource usage:
- Key constraints: Each browser instance (Chrome, Firefox, etc.) uses ~100-200MB of RAM plus CPU overhead. Headless browsers use significantly less (~50-100MB per instance), so always use
--headless=newfor Chrome or equivalent flags for other browsers if you can. - Practical guidelines:
- For an 8-core CPU with 16GB RAM (after system/background processes take ~4GB): You can safely run 8-12 concurrent sessions. Start with 6-8 and adjust based on machine load.
- For a 4-core CPU with 8GB RAM: Stick to 3-5 concurrent sessions to avoid lag or test failures from resource starvation.
- To configure max sessions per node, add the
--max-sessionflag when starting the node:java -jar selenium-server-4.x.x.jar node --hub http://localhost:4444 --max-session 8
This lets the node handle up to 8 parallel test sessions at once. Remember: On a single machine, the "nodes" here are concurrent sessions on the same node instance—you don't need multiple separate node processes.
Final Tip
No matter which parallel method you choose, test case independence is non-negotiable. Each test must set up its own WebDriver, navigate to the starting page, clean up after itself, and never rely on state from another test. This prevents flaky results and ensures parallelism works smoothly.
内容的提问来源于stack exchange,提问作者User

