Real-World Java Selenium Automation Interview question
🔧 Automation Troubleshooting Mastery
1. Automate a functionality not yet fully developed
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.
@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?
- Check error message & logs (console, TestNG report, screenshot).
- Analyze screenshot – UI change? popup? element missing?
- Re-run the test (could be flaky).
- Validate manually – does the issue reproduce?
- If bug → report in Jira; if script issue → fix locator/wait.
3. Application has minor changes – how modify automation?
Scenario: Login button ID changes from login-btn → login-button.
Bad practice: updating every test. Solution: Page Object Model (POM).
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.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:
- Configure browser download directory (ChromeOptions).
- Click download button.
- Wait for file to appear (polling).
- Assert file exists & verify content (read CSV/PDF).
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.
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.
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.
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.
- Check logs & screenshot.
- Inspect element – locator changed?
- Manual test – is it a bug?
- Fix script or report defect.
13. Automation scripts running slowly – improve speed
- Replace
Thread.sleepwith explicit waits. - Run tests in parallel (TestNG:
parallel="tests" thread-count="3"). - Headless mode:
--headless. - Optimize locators: ID > CSS > XPath.
14. Script failing due to application change – handling
If locator changed (e.g., login-btn → login-button), update in Page Object only.
For frequent changes, use dynamic XPath: //button[contains(text(),'Login')].
POM = one change, all tests fixed
Comments
Post a Comment