- Web Automation Testing with Selenium
Selenium is a robust framework designed for automating web browser interactions across various platforms and programming languages.
Core Locators and Element Selection
Efficient testing relies on accurate element targeting. Selenium supports several CSS and XPath strategies:
- CSS Selectors:
#id(ID),.className(Class),elementName(Tag), andparent descendanthierarchy. - XPath Expressions:
- Absolute path:
/html/head/title - Relative indexing:
//form/span[1]/input(Note: Index starts at 1) - Attribute match:
//input[@name='search_field'] - Text match:
//a[text()='Submit']
- Absolute path:
Interaction Methods
Common actions include click(), sendKeys() (for input simulation), clear() (to reset fields), getText(), and getAttribute().
Synchronous and Asynchronous Waits
To handle dynamic page loading, use explicit wait strategies instead of static sleeps:
WebDriverWait driverWait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = driverWait.until(ExpectedConditions.elementToBeClickable(By.id("login-btn")));
Browser and Advanced Event Handling
Browsers can be managed through window sizing, navigation (forward/back/refresh), and scrolling via JavaScript injection. For complex interactions like drag-and-drop or right-click, utilize the Actions class:
Actions builder = new Actions(driver);
builder.moveToElement(targetElement).contextClick().perform();
- Performance Testing Concepts
Performance testing evaluates system stability, responsiveness, and resource utilization under specific loads.
Key Metrics
- Response Time: The duration from request initiation to completion.
- TPS (Transactions Per Second): The number of operations processed in one second, serving as a primary indicator of system capacity.
- Throughput: Total volume of data or requests handled within a defined period.
- Resource Utilization: Metrics related to CPU, memory, and disk I/O.
Testing Methodology
- Baseline: Establishing the system's performance under normal conditions.
- Load Test: Observing performance under expected user traffic.
- Stress Test: Identifying the "breaking point" of the system under extreme pressure.
- Endurance Test: Testing system stability over an extended operation period to identify leaks.
- Performance Modeling with LoadRunner
LoadRunner simplifies enterprise-level performance engineering through three main components: VuGen (Scripting), Controller (Scenario Design), and Analysis (Reporting).
Script Development
Scripts consist of three parts: vuser_init (initialization), Action (core business logic), and vuser_end (cleanup). Use lr_start_transaction and lr_end_transaction to wrap business operations for accurate timing analysis. Parametrization is crucial for simulating multiple unique users during high-concurrency tests.
- Unit Testing with JUnit 5
JUnit 5 is the standard testing framework for Java, providing annotation-driven lifecycle management and robust assertions.
Essential Annotations
@Test: Defines a test method.@BeforeEach/@AfterEach: Executed before/after every individual test method.@BeforeAll/@AfterAll: Executed once for all tests in the class.
Parameterization and Assertions
Use @ParameterizedTest with @ValueSource or @CsvSource to run the same logic with different datasets. Validate outcomes using standard assertions:
Assertions.assertEquals(expectedResult, actualResult, "Result mismatch");
Assertions.assertNotNull(objectReference);