Web Application Testing with Selenium WebDriver & Java
This skill enables comprehensive testing, debugging, and execution of browser-based automation for web applications
using Selenium WebDriver within a Java environment.
When to use this skill
Use this skill when you need to:
- Create new Selenium WebDriver tests using Java (JUnit 5)
- Interact with web elements (clicks, inputs, selections) in a real browser
- Verify UI behavior and interactions
- Implement robust synchronization using Explicit Waits
- Debug UI behavior and DOM interactions
- Debug failing browser tests
- Debug web application issues
- Inspect browser console logs
- Set up test infrastructure for a new project using Maven
- Capture screenshots (
TakesScreenshot) for reporting, documentation or debugging
- Validate complex user flows and form submissions
- Check responsive design across viewports
- Validate accessibility when necessary: integrate automated checks (e.g.,
axe-core with Selenium)
- Validate API calls with
Rest-Assured to ensure backend responses meet expectations.
Your Role
You coordinate the entire Selenium WebDriver test creation process:
- Analyze: Understand the user's test requirements and data needs.
- Inspect: Identify locators strategies prioritizing
id, data-testid, or cssSelector.
- Design: Apply the Page Object Model (POM) strictly.
- Implement: Generate the test script with complete error handling and logging.
- Verify: Ensure assertions use AssertJ fluent style.
Reference Documentation
Before starting any test creation, read these files to understand the standards:
- Recommended: Use the
.github/instructions/selenium-webdriver-java.instructions.md for the standard test instructions
Prerequisites
- Java JDK 21 or higher installed
- Maven for dependency management
- Standard dependencies:
selenium-java, rest-assured, lombok, assertj-core, slf4j-api, javafaker, and a test
runner (junit-jupiter)
- A locally running web application (or accessible URL) for testing
- Selenium WebDriver will be configured automatically via
Maven dependencies
- Note: Selenium Manager (included in Selenium 4.6+) automatically handles browser driver binaries
Core Capabilities
1. Browser Automation
- Navigation: Navigate to URLs (
driver.get(), driver.navigate().to()), handling tabs/windows (
getWindowHandles), managing browser history (driver.navigate().back(), forward(), refresh()).
- Interaction: Click buttons and links (
click()), input text (sendKeys()), clear fields (clear()), submit
forms (submit()), drag and drop (Actions class), hover over elements, right-click, double-click.
- Selects: Handling dropdowns using the
Select class.
- Frames/Alerts: Switching context with
driver.switchTo() (e.g., frame(), alert()).
- Cookies: Managing browser cookies (
manage().addCookie(), getCookieNamed(), deleteCookieNamed()).
- Forms: Fill form fields, handle file uploads.
2. Verification (Assertions)
- Library: STRICTLY use
AssertJ for all validations.
- Soft Assertions: Use
SoftAssertions when validating multiple fields in a single form/page to detect all errors in one run.
- Checks: Visibility, text content, attributes, URL, and page titles.
- Assert element presence and state (
isEnabled, isSelected, isDisplayed).
- Validate element attributes (e.g.,
hasAttribute, attributeContains).
- Verify text content (
getText).
- Validate page titles and URLs.
- Check element visibility
- Test responsive behavior
- Assert element attributes and properties.
3. Debugging & Reporting
- Capture screenshots on failure or demand.
- Extract page source for DOM analysis.
- View browser console logs (where supported by the driver).
- Inspect network requests
- Debug failed tests
4. Code Style & Patterns
Use these patterns to ensure code quality:
- Data Models: Use Lombok
@Data and @Builder for Test Data Objects (POJOs).
- Waits: NEVER use
Thread.sleep. Use WebDriverWait with ExpectedConditions.
- Variables: Use Java 21
var inference for local variables to improve readability.
- Locators: Avoid XPATH unless absolutely necessary.
- Assertions: Use
AssertJ for readable validations.
- Dynamic Data: Use
JavaFaker for generating random test data (names, emails).
Best Practices Checklist
✅ Always verify the app is running - Check that the URL is accessible before running tests
✅ Clean up resources - Always close the browser when done
✅ Implement Page Object Model - Separate location logic from test logic
✅ Handle timeouts gracefully - Set reasonable timeouts for slow operations
✅ Test incrementally - Start with simple interactions before complex flows
✅ Use selectors wisely - Prefer data-testid or prioritize stable selectors over CSS classes
✅ Avoid hard-coded waits - Don't use Thread.sleep(), use always explicit waits
✅ Keep tests independent - Each test should be able to run in isolation
Running tests
To run tests using Maven:
Run all tests
mvn test
Run specific test class
mvn test -Dtest=LoginTest
Run specific test method
mvn test -Dtest=LoginTest#testValidLogin
Now help the user create their Selenium test!
1---2name: webapp-selenium-testing3description: Toolkit for interacting with and testing local web applications using Selenium WebDriver and Java. Supports verifying frontend functionality, managing synchronization with explicit waits, and viewing browser and page source for analyzing logs.4---56# Web Application Testing with Selenium WebDriver & Java78This skill enables comprehensive testing, debugging, and execution of browser-based automation for web applications9using Selenium WebDriver within a Java environment.1011## When to use this skill1213Use this skill when you need to:1415- Create new Selenium WebDriver tests using Java (JUnit 5)16- Interact with web elements (clicks, inputs, selections) in a real browser17- Verify UI behavior and interactions18- Implement robust synchronization using Explicit Waits19- Debug UI behavior and DOM interactions20- Debug failing browser tests21- Debug web application issues22- Inspect browser console logs23- Set up test infrastructure for a new project using Maven24- Capture screenshots (`TakesScreenshot`) for reporting, documentation or debugging25- Validate complex user flows and form submissions26- Check responsive design across viewports27- Validate accessibility when necessary: integrate automated checks (e.g., `axe-core` with Selenium)28- Validate API calls with `Rest-Assured` to ensure backend responses meet expectations.2930## Your Role3132You coordinate the entire Selenium WebDriver test creation process:3334- **Analyze**: Understand the user's test requirements and data needs.35- **Inspect**: Identify locators strategies prioritizing `id`, `data-testid`, or `cssSelector`.36- **Design**: Apply the Page Object Model (POM) strictly.37- **Implement**: Generate the test script with complete error handling and logging.38- **Verify**: Ensure assertions use AssertJ fluent style.3940## Reference Documentation4142Before starting any test creation, read these files to understand the standards:4344- *Recommended*: Use the `.github/instructions/selenium-webdriver-java.instructions.md` for the standard test instructions4546## Prerequisites4748- Java JDK 21 or higher installed49- Maven for dependency management50- Standard dependencies: `selenium-java`, `rest-assured`, `lombok`, `assertj-core`, `slf4j-api`, `javafaker`, and a test51 runner (`junit-jupiter`)52- A locally running web application (or accessible URL) for testing53- Selenium WebDriver will be configured automatically via `Maven` dependencies54- *Note*: Selenium Manager (included in Selenium 4.6+) automatically handles browser driver binaries5556## Core Capabilities5758### 1. Browser Automation5960- **Navigation**: Navigate to URLs (`driver.get()`, `driver.navigate().to()`), handling tabs/windows (61 `getWindowHandles`), managing browser history (`driver.navigate().back()`, `forward()`, `refresh()`).62- **Interaction**: Click buttons and links (`click()`), input text (`sendKeys()`), clear fields (`clear()`), submit63 forms (`submit()`), drag and drop (`Actions` class), hover over elements, right-click, double-click.64- **Selects**: Handling dropdowns using the `Select` class.65- **Frames/Alerts**: Switching context with `driver.switchTo()` (e.g., `frame()`, `alert()`).66- **Cookies**: Managing browser cookies (`manage().addCookie()`, `getCookieNamed()`, `deleteCookieNamed()`).67- **Forms**: Fill form fields, handle file uploads.6869### 2. Verification (Assertions)7071- **Library**: STRICTLY use `AssertJ` for all validations.72- **Soft Assertions**: Use `SoftAssertions` when validating multiple fields in a single form/page to detect all errors in one run.73- **Checks**: Visibility, text content, attributes, URL, and page titles.74- Assert element presence and state (`isEnabled`, `isSelected`, `isDisplayed`).75- Validate element attributes (e.g., `hasAttribute`, `attributeContains`).76- Verify text content (`getText`).77- Validate page titles and URLs.78- Check element visibility79- Test responsive behavior80- Assert element attributes and properties.8182### 3. Debugging & Reporting8384- Capture screenshots on failure or demand.85- Extract page source for DOM analysis.86- View browser console logs (where supported by the driver).87- Inspect network requests88- Debug failed tests8990### 4. Code Style & Patterns9192Use these patterns to ensure code quality:9394- **Data Models**: Use Lombok `@Data` and `@Builder` for Test Data Objects (POJOs).95- **Waits**: NEVER use `Thread.sleep`. Use `WebDriverWait` with `ExpectedConditions`.96- **Variables**: Use Java 21 `var` inference for local variables to improve readability.97- **Locators**: Avoid XPATH unless absolutely necessary.98- **Assertions**: Use `AssertJ` for readable validations.99- **Dynamic Data**: Use `JavaFaker` for generating random test data (names, emails).100101## Best Practices Checklist102103✅ Always verify the app is running - Check that the URL is accessible before running tests104✅ Clean up resources - Always close the browser when done105✅ Implement Page Object Model - Separate location logic from test logic106✅ Handle timeouts gracefully - Set reasonable timeouts for slow operations107✅ Test incrementally - Start with simple interactions before complex flows108✅ Use selectors wisely - Prefer `data-testid` or prioritize stable selectors over CSS classes109✅ Avoid hard-coded waits - Don't use `Thread.sleep()`, use always explicit waits110✅ Keep tests independent - Each test should be able to run in isolation111112## Running tests113114To run tests using Maven:115116### Run all tests117118```bash119mvn test120```121122### Run specific test class123124```bash125mvn test -Dtest=LoginTest126```127128### Run specific test method129130```bash131mvn test -Dtest=LoginTest#testValidLogin132```133134Now help the user create their Selenium test!