Test Overview
This test opens a web page, logs each step for clarity, clicks a button, and verifies the result. It shows how logging helps track test progress.
This test opens a web page, logs each step for clarity, clicks a button, and verifies the result. It shows how logging helps track test progress.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.logging.Logger; import org.junit.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; public class LoggingTestSteps { private WebDriver driver; private Logger logger = Logger.getLogger(LoggingTestSteps.class.getName()); @Before public void setUp() { logger.info("Starting browser setup"); driver = new ChromeDriver(); logger.info("Browser started"); } @Test public void testButtonClick() { logger.info("Navigating to example page"); driver.get("https://example.com/buttonpage"); logger.info("Locating the button by id 'click-me'"); WebElement button = driver.findElement(By.id("click-me")); logger.info("Clicking the button"); button.click(); logger.info("Checking if the result text is displayed"); WebElement resultText = driver.findElement(By.id("result")); Assert.assertEquals("Button clicked!", resultText.getText()); logger.info("Assertion passed: Button click result is correct"); } @After public void tearDown() { logger.info("Closing browser"); if (driver != null) { driver.quit(); } logger.info("Browser closed"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Logger records 'Starting browser setup'; ChromeDriver instance created | Browser window opens, blank page | - | PASS |
| 2 | Logger records 'Navigating to example page'; driver.get loads https://example.com/buttonpage | Page with a button having id 'click-me' is displayed | - | PASS |
| 3 | Logger records 'Locating the button by id 'click-me''; driver finds the button element | Button element is found and ready to interact | - | PASS |
| 4 | Logger records 'Clicking the button'; button.click() is executed | Page updates to show result text with id 'result' | - | PASS |
| 5 | Logger records 'Checking if the result text is displayed'; driver finds result element; Assert.assertEquals verifies text | Result text reads 'Button clicked!' | Assert.assertEquals confirms text matches expected | PASS |
| 6 | Logger records 'Assertion passed: Button click result is correct' | Test passed, browser ready to close | - | PASS |
| 7 | Logger records 'Closing browser'; driver.quit() closes browser | Browser window closed | - | PASS |
| 8 | Logger records 'Browser closed'; test ends | No browser open, test complete | - | PASS |