Real-World Java Selenium Automation Interview question

Real-World Automation Interview question: 14+ Scenarios and Solutions (Selenium)

🔧 Automation Troubleshooting Mastery

14+ real‑world scenarios: from unstable features to speed optimization – with code & strategy

1. Automate a functionality not yet fully developed

Interview Answer & Step-by-Step

Understanding Automating too early causes flaky tests and maintenance nightmare.

  • Step 1 – Understand requirement: user story, acceptance criteria.
  • Step 2 – Check feature stability: UI + locators + workflow finalized?
  • Step 3 – Prepare framework components: Page Object classes, test data, skeleton.
  • Step 4 – Create skeleton scripts: structure without full logic.
// Skeleton example
@Test
public void verifyUserRegistration() {
RegisterPage register = new RegisterPage(driver);
register.enterEmail("test@gmail.com");
register.enterPassword("password123");
register.clickRegister();
}

Final: Automate only after feature stabilizes & manual verification passes.

2. If a test fails, what is your next step?

Troubleshooting process
  1. Check error message & logs (console, TestNG report, screenshot).
  2. Analyze screenshot – UI change? popup? element missing?
  3. Re-run the test (could be flaky).
  4. Validate manually – does the issue reproduce?
  5. If bug → report in Jira; if script issue → fix locator/wait.
Always include evidence: logs, screenshots, steps.

3. Application has minor changes – how modify automation?

Scenario: Login button ID changes from login-btnlogin-button.

Bad practice: updating every test. Solution: Page Object Model (POM).

// Before
By loginButton = By.id("login-btn");

// After – only one change
By loginButton = By.id("login-button");

Benefits: centralized locators, reduced maintenance.

4. How would you automate login functionality?

Scenarios: valid/invalid credentials, empty fields, SQL injection.

driver.get("https://example.com/login");
driver.findElement(By.id("username")).sendKeys("testuser");
driver.findElement(By.id("password")).sendKeys("pass123");
driver.findElement(By.id("loginBtn")).click();
Assert.assertTrue(driver.getCurrentUrl().contains("dashboard"));

Use Page Object Model + TestNG DataProvider for data-driven.

5. Automate verification of email sent after registration

Approaches:

  • Email testing tools: Mailinator, Mailtrap – open inbox via API/UI.
  • Backend API validation: check email service response (200 OK).
  • Database check: query email queue table.

Example: after registration, fetch email from Mailinator and assert subject.

6. Long execution scenario – manual or automation?

Correct answer: Automation. Example: order → payment → inventory → invoice → email. Manual takes 2h, automation 10 min.

Benefits: faster regression, CI/CD integration, consistent.

7. Validate downloaded file after clicking button

Challenge: file stored on disk, not in browser. Steps:

  1. Configure browser download directory (ChromeOptions).
  2. Click download button.
  3. Wait for file to appear (polling).
  4. Assert file exists & verify content (read CSV/PDF).
File file = new File("C:\\Downloads\\report.csv");
int wait = 0;
while(!file.exists() && wait<10) { Thread.sleep(1000); wait++; }
Assert.assertTrue(file.exists());
// read content with BufferedReader

8. Verify color, font, position of element

Use getCssValue() for styling, getLocation() / getSize() for position.

WebElement btn = driver.findElement(By.id("loginBtn"));
String color = btn.getCssValue("background-color"); // rgba
String fontSize = btn.getCssValue("font-size");
Point loc = btn.getLocation();
Dimension dim = btn.getSize();

9. How do you handle popups or alerts?

Three types: JavaScript alert, confirmation, prompt.

Alert alert = driver.switchTo().alert();
String msg = alert.getText();
alert.accept(); // OK
// alert.dismiss(); // Cancel
// alert.sendKeys("input"); // for prompt

Always use explicit wait: wait.until(ExpectedConditions.alertIsPresent())

10. Verify scrolling until footer becomes visible

Use JavaScriptExecutor.

WebElement footer = driver.findElement(By.id("footer"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true);", footer);
Assert.assertTrue(footer.isDisplayed());

Alternative: window.scrollTo(0, document.body.scrollHeight)

11. Automating a legacy application – approach

Challenges: no IDs, frames, slow UI, poor HTML.

  • Identify stable locators (name, css, xpath).
  • Handle frames: driver.switchTo().frame("mainFrame").
  • Use explicit waits generously.
  • Prioritize critical flows (login, payment).

12. Script worked yesterday, failing today – troubleshooting

Possible causes: locator changed, app update, environment, test data, timing.

  1. Check logs & screenshot.
  2. Inspect element – locator changed?
  3. Manual test – is it a bug?
  4. Fix script or report defect.

13. Automation scripts running slowly – improve speed

  • Replace Thread.sleep with explicit waits.
  • Run tests in parallel (TestNG: parallel="tests" thread-count="3").
  • Headless mode: --headless.
  • Optimize locators: ID > CSS > XPath.
<suite name="Suite" parallel="tests" thread-count="4">

14. Script failing due to application change – handling

If locator changed (e.g., login-btnlogin-button), update in Page Object only.

For frequent changes, use dynamic XPath: //button[contains(text(),'Login')].

POM = one change, all tests fixed


Comments

Popular posts from this blog

What is Software Testing? Complete Beginner-Friendly Guide

Defect Management: Complete Guide with Tools & Best Practices

SDLC Models: Complete Guide with Advantages & Disadvantages