Advertisement

Responsive Ads Here

Saturday, September 27, 2025

Selenium Top 50 Automation Interview Questions and Answers (Basic to Advanced with Real-Time Examples) – Part-2

 


                                                    

26. How do you validate broken links using Selenium?

Answer:

  • Collect all links using findElements(By.tagName("a")).
  • Get href attribute.
  • Send HTTP request with Java/HttpURLConnection.
  • If response code >= 400 → Broken link.
Example:
List<WebElement> links = driver.findElements(By.tagName("a"));
for(WebElement link : links){
    String url = link.getAttribute("href");
    HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection();
    conn.setRequestMethod("HEAD");
    if(conn.getResponseCode() >= 400){
        System.out.println(url + " is broken");
    }
}
Real-time Use:
In a banking web app, I validated all "Help" and "FAQ" links to ensure no broken references before go-live.

27. What automation framework have you worked on? Can you explain its architecture?

Answer:
I worked on a Hybrid Selenium Framework (combination of Data-Driven + POM + TestNG).

📌 Components included:

  • TestNG → Test execution & reports
  • POM/Page Factory → Code reusability & readability
  • Data Handling → Excel, JSON, Properties files
  • Utilities → For waits, logging, screenshots
  • Listeners → For capturing failures
  • CI/CD → Integrated with Jenkins
Real-time Example:
In my telecom Salesforce project, we used this framework to automate order-to-billing workflows. The framework reduced execution time by 40% compared to manual regression.

28. How do you integrate Selenium with TestNG/JUnit?
Answer:

  • Write test classes with @Test annotation.
  • Use @BeforeMethod and @AfterMethod for setup/teardown.
  • Run via TestNG XML file or JUnit runner.

Example (TestNG):

@Test

public void loginTest(){
   driver.get("https://app.com");
   Assert.assertEquals(driver.getTitle(), "Home");

}

Real-time Use:
We ran TestNG suites in Jenkins with parallel execution enabled, saving regression execution time.


29. What are the advantages of TestNG over JUnit in Selenium?
Answer:

  • TestNG XML → Better test configuration & grouping.
  • Parallel Execution supported.
  • Flexible Annotations (before/after suite, groups).
  • Inbuilt Reporting.
  • Data Provider for parameterization.

Real-time Example:
In my project, I grouped smoke and regression tests in TestNG XML and executed them separately during releases.

30. How do you generate reports in Selenium?
Answer:
Options:

  • TestNG default reports
  • Extent Reports / Allure Reports (for rich HTML reports with screenshots)
  • Jenkins plugin reports

Real-time Use:
I implemented Extent Reports in my framework. Each failed step captured a screenshot and was embedded in the HTML report for defect tracking.


31. Have you integrated Selenium with CI/CD tools like Jenkins or GitLab? Explain.
Answer:
Yes. Steps:

  • Push code to GitHub.
  • Configure Jenkins Job → pull code via Git.
  • Add build step → mvn test or TestNG suite.
  • Configure email/slack notification for test results.
Real-time Example:
Our nightly regression suite ran in Jenkins at 2 AM. In the morning, the team received a mail with pass/fail summary + report link.

32. How do you handle test data in your framework (Excel, JSON, DB, Properties file)?
Answer:

Methods I used:

  • Excel (Apache POI) → Large data sets.
  • JSON → Structured test data.
  • Properties File → Environment configs (URLs, credentials).
  • Database Connection → Directly query DB for validation.

Real-time Example:
In a Salesforce integration project, customer data was stored in Excel. I used Apache POI to drive data-driven tests.


33. What is Data Driven vs Keyword Driven vs Hybrid Framework? Which one have you implemented?
Answer:

  • Data-Driven → Test data separated from scripts (Excel, CSV, DB).
  • Keyword-Driven → Uses keywords (Login, Click, Verify) stored in Excel; scripts read and execute them.
  • Hybrid → Combination of both + POM.

Real-time Example:
I implemented a Hybrid framework using POM + Data Driven + TestNG. It supported multiple environments and was scalable.

34. How do you manage reusable components in your framework?
Answer:
  • Create Utility classes for common methods like waits, dropdown selection, screenshots.
  • Create Base class for driver setup & teardown.
  • Store Locators in POM classes.

Example:

public class WaitUtils {
   public static void waitForVisible(WebDriver driver, By locator) {
    new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(locator));
  }
}

Real-time Use:
This reduced code duplication by 30% in my regression suite.


35. How do you handle logging in Selenium? Have you used log4j or other tools?
Answer:

Yes, I implemented Log4j/SLF4J for logging.

  • Info logs → Test steps
  • Error logs → Failures
  • Debug logs → For troubleshooting

Example:

private static Logger log = LogManager.getLogger(LoginTest.class);
log.info("Navigating to login page");
log.error("Login failed due to invalid credentials");

Real-time Use:
Logs helped developers reproduce failures quickly without running the script again.


36. How do you handle parallel execution in Selenium?
Answer:
  • TestNG → parallel="tests" or parallel="methods" in XML.
  • Grid / Selenium Grid / Docker → Run tests across multiple machines/browsers.
  • Cloud platforms → BrowserStack, SauceLabs.

Real-time Example:
I executed regression suite on BrowserStack across Chrome, Firefox, and Safari in parallel, which reduced test cycle time from 4 hours to 1.5 hours.


37. If your script is failing randomly, how will you debug and fix it?

Answer:

Steps I follow:

  • Check waits → Often caused by synchronization issues.
  • Review locators → Might be dynamic or incorrect.
  • heck test data → Data may have changed.
  • Check environment → Sometimes staging/QA environments are unstable.
  • Add logging & screenshots → To see exact failure point.

Real-time Example:
My checkout script in an e-commerce app failed randomly. I found that the “Place Order” button loaded late due to AJAX. Fixed it with Explicit Wait and it became stable.


38. How do you verify that an element is visible and clickable?
Answer:


  •    Use isDisplayed() to check visibility.
  •     Use ExpectedConditions.elementToBeClickable().

Example:

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("submitBtn")));

Real-time Use:
In Salesforce, “Save” button was disabled until mandatory fields were filled. I used this check before clicking.


39. How do you automate dropdowns (single-select & multi-select)?
Answer:
Use Select class.

Select dropdown = new Select(driver.findElement(By.id("country")));
dropdown.selectByVisibleText("India");   // Single-select
dropdown.selectByIndex(2);               // Multi-select

Real-time Example:
In a telecom app, I automated country and plan selection dropdowns. For multi-select, I validated all selected options using getAllSelectedOptions().


40. How do you handle tooltips in Selenium?
Answer:
Tooltips are usually in title attribute.

String tooltip = driver.findElement(By.id("help")).getAttribute("title");
If it’s a custom tooltip (hover), use Actions class.
Actions action = new Actions(driver);
action.moveToElement(element).perform();

Real-time Example:
I validated tooltip messages in Salesforce help icons using attribute extraction.


41. How do you scroll down/up using Selenium?
Answer:
Use JavaScriptExecutor.

((JavascriptExecutor)driver).executeScript(
"window.scrollBy(0,500)");
Or scroll to an element:((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", element);

Real-time Example:
In my project, error messages appeared at the bottom of the form. I scrolled into view before capturing screenshots.


42. How do you drag and drop an element?
Answer:

Use 
Actions class.

Actions action = new Actions(driver);
action.dragAndDrop(source, target).build().perform();

Real-time Example:
In a Kanban board application, I tested dragging tasks from “To Do” to “In Progress” column.


43. How to handle calendars/date pickers in Selenium?
Answer:
  • If it’s a text field → Directly sendKeys("2025-09-07").
  • If it’s a UI date picker → Loop through months/days until desired date is found.

Example:

while(!driver.findElement(By.className("ui-datepicker-month")).getText().equals("September")) {

    driver.findElement(By.xpath("//a[@title='Next']")).click();

}

driver.findElement(By.xpath("//a[text()='7']")).click();

Real-time Example:
In a booking app, I automated selecting a journey date 3 months ahead by navigating the calendar widget.


44. How do you assert page title and page URL?
Answer:
Use TestNG/JUnit assertions.

Assert.assertEquals(driver.getTitle(), "Dashboard");
Assert.assertTrue(driver.getCurrentUrl().contains("dashboard"));

Real-time Example:
In Salesforce, after login, I validated that the title contained “Home | Salesforce” to confirm login success.


45. How do you validate text present on a webpage?
Answer:
  • Locate element and get text → getText().
  • Use assertion.

Example:

String msg = driver.findElement(By.id("successMsg")).getText();
Assert.assertEquals(msg, "Order placed successfully");

Real-time Example:
I validated success/error messages after order submission in a telecom application.


46. What steps do you take when your test suite execution time is too high?
Answer:
  • Run tests in parallel.
  • Identify redundant test cases and remove duplicates.
  • Use headless browsers for faster execution.
  • Optimize locators & waits.
  • Categorize tests (Smoke, Sanity, Regression).

Real-time Example:
Our regression suite initially took 6 hours. After parallelizing on Selenium Grid, execution time reduced to 2 hours.

-------------------------------------------------------------------------------------------------------------------
47. How do you handle test cases that fail only in CI but pass locally?

Answer:
This usually happens due to environment differences. Steps I take
  • Check browser/driver versions in CI vs local.   
  • Check browser/driver versions in CI vs local.
  • Add waits – CI is slower due to shared resources.
  • Check resolution settings – CI may run in headless or lower resolution.
  • Review test data – Sometimes test data is missing in CI DB.
  • Review logs & screenshots from CI run.

Real-time Example:
In Jenkins, my login test failed but worked locally. I discovered the issue was due to headless Chrome not handling animations well. Fixed it by adding 
--disable-gpu in ChromeOptions.


48. What challenges have you faced in Selenium automation, and how did you overcome them?
Answer:
Some common challenges I faced:
  • Dynamic elements → Solved with robust XPaths and waits.
  • Captcha/OTP → Handled via API/DB fetch instead of UI.
  • Stale Element Exceptions → Solved with re-locating elements.
  • CI/CD failures → Fixed with headless browser configs and retries.

Real-time Example:
In Salesforce Lightning UI, elements often refreshed dynamically. I overcame this by writing a custom wait utility for visibility + clickability checks, which improved stability by 25%.


49. How do you decide what to automate and what not to automate?
Answer:

Criteria I use:
✅ Automate when →

  • Test is repetitive & stable
  • Test has large data sets
  • Test is critical for regression
  • Test needs multiple environment runs

❌ Avoid automating when →

  • Feature is still evolving
  • Validation requires visual/manual judgment
  • Scenarios involve Captcha, handwriting, biometric inputs

Real-time Example:
In telecom CRM, I automated order processing, billing, and reporting but left Captcha verification for login as manual.


50. Have you worked with APIs in your automation framework? How did you integrate them with Selenium?
Answer:
Yes, I integrated REST Assured with Selenium.

  • Used Selenium for UI validations.
  • Used REST Assured for backend data validation.

Example:

Response res = given().get("https://api.myapp.com/orders/123");
Assert.assertEquals(res.getStatusCode(), 200);

Real-time Example:
When placing an order, I used Selenium to submit the form, then used API validation to confirm the order was actually created in the backend before asserting UI.


51. How do you ensure your test cases are maintainable and scalable?
Answer:
Best practices I follow:

  • Implement POM + Page Factory for reusable locators.
  • Maintain test data externally (Excel/JSON/DB).
  • Use utility/helper classes for common actions.
  • Apply naming conventions and proper folder structure.
  • Regular refactoring to remove duplicates.
  • Run parallel tests with TestNG/Selenium Grid.

Real-time Example:
In my Salesforce project, new features were added frequently. By following POM + modular design, only locator files needed updates, not the whole scripts — saving huge effort.

 ----------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment