Selenium WebDriver remains the most widely used browser automation framework for QA automation in India. SDET and automation engineer roles at TCS, Infosys, Wipro, Capgemini, Cognizant, and hundreds of Indian product companies all test Selenium. This guide covers Selenium interview questions with a focus on WebDriver internals, locator strategies, TestNG, and the Page Object Model.
Selenium WebDriver architecture and locator strategies
Selenium WebDriver fundamentals every interviewer tests:
1. WebDriver architecture: Selenium WebDriver is a W3C standard protocol. Client library (Java, Python, C#) sends HTTP requests to a browser driver (ChromeDriver, GeckoDriver). The browser driver controls the actual browser. The W3C WebDriver protocol replaced the JSON Wire Protocol in Selenium 4. Three layers: test script (your Java/Python code) → browser driver → browser. Remote WebDriver: test script runs on your machine; connects to a remote browser (Selenium Grid, cloud: BrowserStack, LambdaTest).
2. Locator strategies (best to worst): - ID: fastest and most reliable. driver.findElement(By.id("submit-btn")). - Name: fast when IDs are missing. By.name("username"). - CSS Selector: flexible and fast; preferred over XPath for static elements. By.cssSelector("#login-form .submit-btn"). - XPath: slower than CSS; use for complex scenarios (find element by text, navigate to parent). By.xpath("//button[text()='Submit']"). - Avoid: By.className (fails if multiple classes), By.tagName (too broad), By.linkText/PartialLinkText (only for anchor elements, fragile).
3. Handling dynamic elements: Dynamic IDs change on every page load. Solutions: use stable attributes (data-testid, aria-label, role); use partial attribute match (CSS: [id^='btn-'], XPath: contains(@id,'btn-')). Stale Element Reference: element reference becomes invalid when the DOM is refreshed (SPA navigation, AJAX updates). Fix: re-fetch the element inside a retry loop or wait for the element to reappear.
4. Explicit vs implicit waits: Implicit wait: global timeout applied to every findElement call (driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10))). Blunt; slows down tests for consistently-fast elements. Explicit wait: wait for a specific condition: WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); wait.until(ExpectedConditions.elementToBeClickable(By.id('submit'))). Preferred. Never use Thread.sleep: it always waits the full duration regardless of element state.
TestNG framework and test organisation
TestNG is the dominant Java test framework for Selenium automation in India:
1. TestNG annotations: @Test: marks a test method. @BeforeSuite / @AfterSuite: run once before/after the entire suite. @BeforeClass / @AfterClass: run once before/after the first/last test in the class. @BeforeMethod / @AfterMethod: run before/after each test method (commonly used to launch/quit the browser). @BeforeTest / @AfterTest: run before/after each <test> tag in testng.xml.
2. TestNG features: Parallel execution: <suite parallel="tests" thread-count="3"> runs test groups in parallel. Groups: @Test(groups={"smoke","regression"}), run subsets. Data providers: @DataProvider feeds multiple data sets to a test method. Priority: @Test(priority=1), control execution order (lower number runs first). Soft assertions: TestNG's SoftAssert collects all assertion failures instead of stopping at the first failure.
3. testng.xml: Organises test execution. Specify which classes and methods to include/exclude. Define listeners (ITestListener for reporting, IRetryAnalyzer for flaky test retry). Example: <suite name="Regression"><test name="LoginTests"><classes><class name="com.hirestepx.tests.LoginTest"/></classes></test></suite>.
4. Reporting: Built-in TestNG HTML reports. Extent Reports (popular in India): generates rich HTML reports with screenshots, test steps, and pass/fail statistics. Allure: widely used at product companies for detailed, navigable reports with trend data. Screenshots on failure: use @AfterMethod with ITestResult to capture a screenshot when a test fails.
Page Object Model and Selenium Grid
Architecture and scaling topics:
1. Page Object Model (POM): Design pattern that separates page structure (locators, page actions) from test logic. Each web page is represented by a Java class. Locators are private fields (@FindBy). Page actions are public methods. Tests interact only with page methods, not locators. Benefits: if the UI changes, you update only the page class, not every test. Example: LoginPage.java has usernameField, passwordField, loginButton; login(String user, String pass) method. LoginTest.java: new LoginPage(driver).login("user", "pass").
2. Page Factory: Selenium's built-in POM support. @FindBy(id="username") private WebElement usernameField. PageFactory.initElements(driver, this) in the constructor initialises all @FindBy-annotated fields using lazy proxy elements (fetched only when accessed).
3. Selenium Grid: Distributed test execution across multiple machines and browsers. Hub: central server that receives test requests and distributes to nodes. Node: machine that runs browsers. Grid 4: uses W3C WebDriver protocol; supports Kubernetes/Docker deployment. Use cases: parallel execution across browsers (Chrome, Firefox, Edge), parallel execution across OS (Windows, macOS, Linux), cross-browser compatibility testing.
4. Hybrid framework: Combines keyword-driven, data-driven, and POM approaches. Excel/CSV drives test data (data-driven); keywords map to framework actions (keyword-driven); page objects separate locators from tests. Common in large QA teams at Indian IT companies where test cases are authored by non-programmers using Excel sheets.
Frequently asked questions
Explore more