Test Overview
This test opens a webpage and uses findElements to locate multiple buttons with the same class. It verifies that the correct number of buttons is found and that each button is displayed.
This test opens a webpage and uses findElements to locate multiple buttons with the same class. It verifies that the correct number of buttons is found and that each button is displayed.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.List; import static org.junit.jupiter.api.Assertions.*; public class MultipleButtonsTest { WebDriver driver; @BeforeEach public void setUp() { driver = new ChromeDriver(); driver.get("https://example.com/buttons"); } @Test public void testFindMultipleButtons() { List<WebElement> buttons = driver.findElements(By.className("btn-primary")); assertFalse(buttons.isEmpty(), "No buttons found with class 'btn-primary'"); assertEquals(3, buttons.size(), "Expected exactly 3 buttons with class 'btn-primary'"); for (WebElement button : buttons) { assertTrue(button.isDisplayed(), "Button should be visible"); } } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open, ready to navigate | - | PASS |
| 2 | Browser navigates to https://example.com/buttons | Page with multiple buttons having class 'btn-primary' is loaded | - | PASS |
| 3 | Find all elements with class name 'btn-primary' using findElements | List of WebElements representing buttons is retrieved | Verify list is not empty | PASS |
| 4 | Assert that exactly 3 buttons are found | List size is checked | buttons.size() == 3 | PASS |
| 5 | For each button found, check if it is displayed | Each button element is visible on the page | button.isDisplayed() == true for all buttons | PASS |
| 6 | Test ends and browser closes | Browser window is closed | - | PASS |