Test Overview
This test opens a web page, finds a button using its class name, clicks it, and verifies that a confirmation message appears.
This test opens a web page, finds a button using its class name, clicks it, and verifies that a confirmation message appears.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class FindElementByClassNameTest { private WebDriver driver; private WebDriverWait wait; @BeforeEach public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, 10); } @Test public void testClickButtonByClassName() { driver.get("https://example.com/testpage"); WebElement button = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("submit-btn"))); button.click(); WebElement message = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("confirmation-message"))); assertEquals("Submission successful", message.getText()); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Chrome browser window is open and ready | - | PASS |
| 2 | Navigates to https://example.com/testpage | Page loads with a button having class 'submit-btn' and an empty confirmation message area | - | PASS |
| 3 | Waits until element with class name 'submit-btn' is present and finds the button | Button with class 'submit-btn' is located on the page | Element presence verified | PASS |
| 4 | Clicks the button found by class name | Button is clicked, triggering submission action | - | PASS |
| 5 | Waits until confirmation message with id 'confirmation-message' is visible | Confirmation message area is visible with text 'Submission successful' | Message text equals 'Submission successful' | PASS |
| 6 | Test ends and browser closes | Browser window is closed | - | PASS |