Answer
Reducing Test Execution Time for Large Suites
A 2-hour suite is too slow for CI/CD. Here is the systematic optimization approach:
Step 1 — Parallel Execution (Biggest Impact: 3-5× speedup)
XML
<!-- testng.xml — run methods in parallel with 10 threads -->
<suite name="Suite" parallel="methods" thread-count="10">
<test name="Regression">
<classes>
<class name="tests.LoginTest"/>
<class name="tests.CartTest"/>
<class name="tests.CheckoutTest"/>
</classes>
</test>
</suite>
500 tests at 15 min each sequentially = 7,500 min With 10 parallel threads = 750 min (7.5× speedup, accounting for overhead)
Step 2 — Selenium Grid / Cloud Parallel (10× speedup)
Java
// Run 50 tests simultaneously on Selenium Grid
WebDriver driver = new RemoteWebDriver(
new URL("http://selenium-hub:4444/wd/hub"),
new ChromeOptions()
);
XML
<!-- 50 simultaneous threads across Grid nodes -->
<suite parallel="methods" thread-count="50">
Step 3 — Split by Priority
Bash
# Pipeline Stage 1: Smoke (15 tests, 3 min) — every commit
mvn test -Dgroups="smoke" -Dthread-count=5
# Pipeline Stage 2: Regression (500 tests, 20 min) — every PR
mvn test -Dgroups="regression" -Dthread-count=20
# Pipeline Stage 3: Full suite (nightly only)
mvn test -Dthread-count=50
Step 4 — Replace UI Steps with API Calls
Java
// SLOW: Login via UI every test (30 seconds each)
// × 500 tests = 4 hours just on login!
@BeforeMethod
public void loginViaUI() {
driver.get(baseUrl + "/login");
driver.findElement(By.id("username")).sendKeys("admin");
driver.findElement(By.id("password")).sendKeys("pass");
driver.findElement(By.id("loginBtn")).click();
}
// FAST: Login via API once, inject cookie
@BeforeSuite
public void loginViaAPI() {
Response r = RestAssured.given()
.body("{\"email\":\"admin@test.com\",\"password\":\"pass\"}")
.post("/api/login");
this.authCookie = r.getCookie("session");
}
@BeforeMethod
public void injectSession() {
driver.get(baseUrl);
driver.manage().addCookie(new Cookie("session", authCookie));
// 2 seconds instead of 30!
}
Step 5 — Eliminate Unnecessary Waits
Java
// SLOW: Fixed waits everywhere
Thread.sleep(5000); // wasting time 500× = 41 minutes wasted!
// FAST: Wait only as long as needed
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("result")));
// Continues as soon as element appears
Step 6 — Headless Mode in CI
Java
// Headless is 20-40% faster than headed
options.addArguments("--headless=new");
Results After Optimization
CODE
Before: 2 hours sequential
After:
Step 1 (parallel, 10 threads): 12 minutes
Step 2 (Grid, 50 threads): 3 minutes
Step 3 (API login): -40 min saved
Step 4 (no Thread.sleep): -30 min saved
Final: ~8-12 minutes for 500 tests on Grid
Optimization Priority Matrix
| Optimization | Effort | Impact |
|---|---|---|
| Parallel execution | Low | Very High |
| Selenium Grid | Medium | Very High |
| API login bypass | Medium | High |
| Remove Thread.sleep | Low | High |
| Headless mode | Low | Medium |
| Smoke/regression split | Low | Medium |
| Test data optimization | Medium | Medium |
