Java Selenium and API Testing InterView Question

Selenium Deep Dive: Waits, Cookies, Flaky Tests, API Testing & Project Approach

Java Selenium and API Testing InterView Question

Synchronization • Cookies • Flaky Tests • Auth • API Test Cases • Project Approach

1. Synchronization in Selenium

Concept: Synchronization means making the automation script wait until the web element or page is ready before performing an action. Modern web apps are dynamic; elements load after AJAX/JS. Without proper wait, tests fail with:

  • NoSuchElementException
  • ElementNotInteractableException
  • TimeoutException

Types of Waits in Selenium

🔹 Implicit Wait

Global wait applied to all elements. If Selenium cannot find an element immediately, it waits up to the specified time.

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

🔹 Explicit Wait

Used for specific conditions (element visible, clickable, present). More granular.

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement loginBtn = wait.until(ExpectedConditions.elementToBeClickable(By.id("login")));
loginBtn.click();

🔹 Fluent Wait

Allows polling interval and ignoring exceptions.

Wait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(20))
.pollingEvery(Duration.ofSeconds(2))
.ignoring(NoSuchElementException.class);

Best practice: Avoid Thread.sleep(); use explicit waits for reliability. Example: after login, wait for dashboard element.

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dashboard")));

2. Handling Cookies in Selenium

What are Cookies? Small data stored by websites — login sessions, preferences, tokens.

Selenium Cookie Class:

  • Get all cookies: Set<Cookie> cookies = driver.manage().getCookies();
  • Add cookie: Cookie cookie = new Cookie("username","sunil"); driver.manage().addCookie(cookie);
  • Delete all: driver.manage().deleteAllCookies();

Project use case: Speed up tests by reusing session cookies. Login once, capture cookie, inject in later tests.

3. What is a Flaky Test?

A flaky test passes or fails inconsistently without code changes. Example: today PASS, tomorrow FAIL.

Common Causes

  • Synchronization issues (no proper wait)
  • Dynamic elements / changing locators
  • Network delays
  • Test data conflicts
  • Environment instability

How to Fix Flaky Tests

  • Use explicit waits instead of sleep
  • Avoid fragile locators: //*[@class='btn'] → use //*[@id='loginBtn']
  • Stable test data (isolated per run)
  • Retry mechanism: @Test(retryAnalyzer = RetryAnalyzer.class)

4. Authentication vs Authorization

Authentication: Verifying identity (login with username/password).

Authorization: Checking permissions (what you can access).

AuthenticationAuthorization
"Who are you?""What can you do?"
Login credentialsRole-based access (Admin, User)
First stepSecond step after auth

Example: user logs in (authentication), then tries to delete user – only Admin allowed (authorization).

5. API Test Cases for Registration API

Endpoint: POST /register Payload:

{ "name":"Sunil", "email":"sunil@test.com", "password":"12345678" }

Positive Test Case

  • Valid data → 201 Created

Negative Test Cases

  • Email already exists → 409 Conflict
  • Invalid email format (sunil@abc) → 400 Bad Request
  • Missing required fields (e.g., only name) → 400

Boundary Test Cases (password length 8-20)

  • 7 characters → 400
  • 8 characters → 201
  • 20 characters → 201
  • 21 characters → 400

6. Approach to Start Testing a New Project

  1. Step 1 – Requirement Understanding – BRD, user stories, acceptance criteria.
  2. Step 2 – Application Understanding – workflow, modules, business logic (login, cart, payment).
  3. Step 3 – Test Planning – scenarios, test cases, test data.
  4. Step 4 – Environment Setup – test env ready, credentials, tools installed.
  5. Step 5 – Manual Testing – functional, UI, API, integration.
  6. Step 6 – Bug Reporting – Jira/Azure with steps, expected/actual, screenshots.
  7. Step 7 – Automation Planning – identify smoke/regression tests; tools: Selenium, RestAssured.

7. How to Report a Bug in API

Example bug report:

  • Title: Registration API returns 500 Internal Server Error for valid request
  • Steps:
    1. Send POST request to /register
    2. Payload: { "name":"Sunil", "email":"sunil@test.com", "password":"12345678" }
    3. Click Send
  • Expected: 201 Created
  • Actual: 500 Internal Server Error
  • Attachments: Request/response payload, Postman screenshot, server logs if available.

Always include enough detail for developer to reproduce.


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