All Posts

Core Java & Selenium ๐—ง๐—ฒ๐—ฐ๐—ต๐—ป๐—ถ๐—ฐ๐—ฎ๐—น Interview Questions

 

QA Automation โ€“ Core Java & Selenium ๐—ง๐—ฒ๐—ฐ๐—ต๐—ป๐—ถ๐—ฐ๐—ฎ๐—น Interview Questions


1๏ธโƒฃ What are the differences between findElement() and findElements()?

Sample Answer:

  • findElement() returns a single WebElement.
  • If the element is not found, it throws NoSuchElementException.
  • findElements() returns a List<WebElement>.
  • If no elements are found, it returns an empty list (does not throw exception).
  • findElement() is used when exactly one element is expected.
  • findElements() is used for multiple elements like dropdown options, lists, tables.

Example:

WebElement loginBtn = driver.findElement(By.id("login"));
List<WebElement> links = driver.findElements(By.tagName("a"));

2๏ธโƒฃ How do you handle dynamic web elements in Selenium?

Sample Answer:

I handle dynamic elements using:

  • Dynamic XPath: using contains(), starts-with()
  • CSS Selectors with partial attributes
  • Explicit Waits for dynamic loading
  • Handling StaleElementReferenceException with re-locating element

Example:

driver.findElement(By.xpath("//input[contains(@id,'user')]"));

3๏ธโƒฃ Explain different types of waits used in Selenium.

Sample Answer:

  • Implicit Wait โ€“ Applied globally, waits for elements to be present.
  • Explicit Wait (WebDriverWait) โ€“ Waits for a specific condition.
  • Fluent Wait โ€“ Custom polling time, ignores specific exceptions.

Example of Explicit Wait:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("submit")));

4๏ธโƒฃ Whatโ€™s the difference between XPath and CSS Selector?

Sample Answer:

XPath CSS Selector
Can traverse forward & backward Only forward traversal
Supports contains(), text() No text() support
Slower compared to CSS Generally faster
More powerful for complex DOM Cleaner & shorter syntax

5๏ธโƒฃ How do you handle multiple windows/tabs in Selenium?

Sample Answer:

I use getWindowHandles() to get all window IDs and switch using switchTo().window().

String parent = driver.getWindowHandle();
Set<String> windows = driver.getWindowHandles();

for(String window : windows){
    if(!window.equals(parent)){
        driver.switchTo().window(window);
    }
}

6๏ธโƒฃ How do you design a Page Object Model (POM) framework?

Sample Answer:

In POM framework:

  • Each page is represented as a separate class.
  • Web elements are declared using @FindBy.
  • Page methods contain business logic.
  • Test classes call page methods.
  • Common utilities like BaseTest, DriverFactory are separated.

Advantages:

  • Code reusability
  • Maintainability
  • Separation of concerns

7๏ธโƒฃ What is the use of final, static, and this keywords in Java?

Sample Answer:

  • final โ€“ used for constants, preventing inheritance or method overriding.
  • static โ€“ belongs to class, shared across objects.
  • this โ€“ refers to current class instance variable.

Example:

public class Test {
    static int count = 0;
    final int MAX = 10;

```
public void setCount(int count){
    this.count = count;
}
```

}

8๏ธโƒฃ Explain Collection Framework โ€” where have you used HashMap or ArrayList in your automation?

Sample Answer:

I have used:

  • ArrayList โ€“ storing multiple WebElements or test data rows.
  • HashMap โ€“ storing key-value pairs like username-password, column-value mapping.

Example:

HashMap<String, String> userData = new HashMap<>();
userData.put("username", "admin");
userData.put("password", "admin123");

9๏ธโƒฃ How do you handle exceptions in Selenium scripts?

Sample Answer:

  • Using try-catch blocks
  • Using throws declaration
  • Custom exception handling in framework
  • Retry mechanism for flaky tests
  • Logging errors using Log4j

Example:

try {
    driver.findElement(By.id("login")).click();
} catch (NoSuchElementException e) {
    System.out.println("Element not found");
}

๐Ÿ”Ÿ Whatโ€™s your approach to data-driven testing using TestNG or Excel?

Sample Answer:

I use @DataProvider in TestNG to pass multiple test data sets.

For Excel integration:

  • Use Apache POI to read Excel file.
  • Store data in Object[][] format.
  • Pass to @DataProvider method.

Example:

@DataProvider(name="loginData")
public Object[][] getData(){
    return new Object[][] {
        {"admin","admin123"},
        {"user","user123"}
    };
}