</>

Technology

Cucumber

Difficulty

Advanced

Interview Question

Tests pass on your local machine but consistently fail in the Jenkins CI pipeline. What do you investigate?

Answer

Local Pass, CI Fail — Debugging Strategy

Immediate Checks (First 10 Minutes)

Bash
# 1. Check Jenkins console output for actual error
tail -100 jenkins-build.log

# 2. Compare Maven commands
# Local:  mvn test -Dbrowser=chrome
# Jenkins: what exact command is Jenkins running?
#   Go to: Jenkins → Build → Console Output → find the mvn command

# 3. Check environment variables
# Jenkins → Build → Environment Variables → compare to local

Root Cause 1: Non-Headless Browser (Most Common)

CODE
Local: Chrome opens visually (you can see it)
CI:    Jenkins runs on server with no display → Chrome crashes

Fix: Enable headless mode in CI
Java
// Hooks.java
@Before
public void setUp() {
    ChromeOptions options = new ChromeOptions();

    // Detect CI environment
    if (System.getenv("CI") != null || System.getenv("JENKINS_URL") != null) {
        options.addArguments(
            "--headless=new",
            "--no-sandbox",
            "--disable-dev-shm-usage",
            "--disable-gpu",
            "--window-size=1920,1080"
        );
    }
    ctx.driver = new ChromeDriver(options);
}

Or via Maven:

Bash
mvn test -Dheadless=true  # Jenkins always passes this flag

Root Cause 2: Missing Environment Variables

Java
// Config reads from system property or env var
String baseUrl = System.getProperty("base.url",
    System.getenv("BASE_URL"));  // check env var too
GROOVY
// Jenkinsfile — set env vars
environment {
    BASE_URL = 'https://staging.example.com'
    DB_PASSWORD = credentials('staging-db-password')
}

Root Cause 3: File Path Differences

Java
// BAD — Windows absolute path hardcoded
String filePath = "C:\\Users\\dev\\testdata\\users.xlsx";

// GOOD — classpath-relative
String filePath = getClass().getClassLoader()
    .getResource("testdata/users.xlsx").getPath();

Root Cause 4: Timing — CI Server Is Slower

Java
// Local: app responds in 1 second → wait of 5s is fine
// CI: shared server, slower app → 5s timeout expires

// Fix: Increase timeouts for CI
int timeout = Integer.parseInt(
    System.getProperty("wait.timeout", "15")  // 15s default, override in CI
);
this.wait = new WebDriverWait(driver, Duration.ofSeconds(timeout));
Bash
# Jenkins runs with longer timeout
mvn test -Dwait.timeout=30

Root Cause 5: ChromeDriver Version Mismatch

Java
// Always use WebDriverManager — no version pinning needed
WebDriverManager.chromedriver().setup();
// WebDriverManager downloads the correct driver for the installed Chrome

Root Cause 6: Port Already in Use (For API Tests)

Bash
# CI server has lingering processes
kill $(lsof -t -i:8080) 2>/dev/null || true
mvn test

Root Cause 7: Shared CI Resources — Parallel Tests Conflict

CODE
5 Jenkins jobs running at the same time?
All using the same staging database?
→ One job's test data is deleted by another job

Fix: Isolated test data per build
ctx.email = "qa-build-" + System.getenv("BUILD_NUMBER") + "@test.com";

Debugging Checklist

CODE
☐ Run with -X (debug mode): mvn test -X > debug.log
☐ Screenshot on every step in CI: @AfterStep
☐ Log browser console errors: DevTools CDP
☐ Compare env vars: Jenkins vs local
☐ Check Chrome/ChromeDriver version on CI agent
☐ Check available memory: CI agents often have less RAM
☐ Check /tmp space: screenshots can fill disk
☐ Add explicit --window-size to headless Chrome

Follow AutomateQA

Related Topics