JUnit Writing Your First Web Test Case with JUnit Estimated reading: 2 minutes 169 views Now that JUnit is set up, it’s time to write our first test. In this guide, I will walk you through creating a simple test case for verifying web elements on a webpage using JUnit and Selenium. We will use this demo web page as the basis for our test.Setting Up Web Testing with JUnitJUnit alone is not designed for web testing, but it works well with Selenium WebDriver, a powerful tool for automating browser interactions. To test elements on a webpage, we need to add the Selenium dependency to our project.Adding Selenium DependencyIf you are using Maven, add the following dependencies to your pom.xml:Add Maven Selenium Dependency <!-- Selenium Java --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.20.0</version> </dependency> <!-- JUnit 5 --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.9.0</version> <scope>test</scope> </dependency>Writing a JUnit Test to Verify Web Elements We will now write a test case that verifies elements on the Locked Screen page of the demo web application. The test will: Open the test webpage. Verify that the name John Doe is displayed. Close the browser once the test is done. Locked Screen Test PageCreating the LockScreenTest test case.LockedScreenSimpleTest Classpackage com.mqa.junit.tests; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; class LockedScreenSimpleTest { private WebDriver driver; @Test void testNameIsDisplayed() throws InterruptedException { // setup chrome driver System.setProperty("webdriver.chrome.driver", "src/test/resources/driver/chromedriver"); driver = new ChromeDriver(); // launch URL for testing driver.get("https://jahmalrichard.github.io/mqa-demo-test-app/pages-lock-screen.html"); // wait for 5 seconds (not recommended for testing purpose) Thread.sleep(3000); // find the name element WebElement name = driver.findElement(By.className("user-name")); // verify that name displayed is John Doe assertTrue(name.getText().trim().matches("John Doe"), "User name should be John Doe"); // close the browser when the test is complete driver.quit(); } }Explanation of testNameIsDisplayed Test CodeThis JUnit test verifies that the Locked Screen Page correctly displays the user name John Doe. Setup Chrome WebDriver to automate the browser. Open the Locked Screen page using Selenium driver. Wait for 3 seconds (not recommended, should use explicit waits). Locate the name element using its class name: user-name. Validate if the displayed name matches John Doe. Close the browser after the test.Running the TestTo execute the test, use the following command: Run JUnit Test# choose one mvn test # to run all tests mvn -Dtest=LockedScreenSimpleTest test # to run specific testConclusionBy writing your first test with JUnit, you have taken an essential step in ensuring the reliability of your web application. In the next guide, we will cover Understanding JUnit Assertions and Annotations to help you create more comprehensive test cases.Tagged:JUnit JUnit - Previous Setting Up JUnit in Your Development Environment Next - JUnit Understanding JUnit Assertions and Annotations