Java Selenium and API Testing InterView Question
Java Selenium and API Testing InterView Question
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:
NoSuchElementExceptionElementNotInteractableExceptionTimeoutException
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.
🔹 Explicit Wait
Used for specific conditions (element visible, clickable, present). More granular.
WebElement loginBtn = wait.until(ExpectedConditions.elementToBeClickable(By.id("login")));
loginBtn.click();
🔹 Fluent Wait
Allows polling interval and ignoring exceptions.
.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.
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).
| Authentication | Authorization |
|---|---|
| "Who are you?" | "What can you do?" |
| Login credentials | Role-based access (Admin, User) |
| First step | Second 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:
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
- Step 1 – Requirement Understanding – BRD, user stories, acceptance criteria.
- Step 2 – Application Understanding – workflow, modules, business logic (login, cart, payment).
- Step 3 – Test Planning – scenarios, test cases, test data.
- Step 4 – Environment Setup – test env ready, credentials, tools installed.
- Step 5 – Manual Testing – functional, UI, API, integration.
- Step 6 – Bug Reporting – Jira/Azure with steps, expected/actual, screenshots.
- 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:
- Send POST request to /register
- Payload: { "name":"Sunil", "email":"sunil@test.com", "password":"12345678" }
- 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
Post a Comment