LAB EXERCISES
- Identify Testing Types for a Sample Application
- Write Test Cases for a Login Page
- Design Test Scenarios for a Web Form
- Perform Manual Testing and Record Defects
- Install Selenium and Configure Environment
- Launch Browser Using Selenium WebDriver
- Locate Web Elements Using Different Locators
- Perform Actions (Click, Send Keys, Submit)
- Automate Form Filling Using Selenium
- Handle Alerts and Pop-ups
- Implement Wait Conditions in Selenium
- Write JUnit Test Cases
- Use Assertions in JUnit
- Execute Test Suite Using JUnit
- Integrate Selenium with JUnit and Run Automation Tests
STEP 1: SOFTWARE INSTALLATION AND ENVIRONMENT SETUP
Before performing the laboratory exercises, install and configure the required software. Visual Studio Code (VS Code) will be used to write and execute Java programs with Selenium WebDriver and JUnit.
1. Install Java Development Kit (JDK)
Step 1: Open the official Java website.
Download Java JDK
Step 2: Select the Windows installer according to your computer (64-bit installer for most modern Windows systems).
Step 3: Download the installer and open it.
Step 4: Follow the installation instructions and complete the setup.
Step 5: Open Command Prompt.
Step 6: Type the following commands:
java -version
javac -version
Step 7: Verify that Java version information is displayed.
Expected Result: Java JDK is successfully installed and configured.
2. Install Visual Studio Code
Step 1: Visit the official Visual Studio Code website.
Download Visual Studio Code
Step 2: Click Download for Windows.
Step 3: Open the downloaded installer.
Step 4: Accept the license agreement.
Step 5: Select the following options if available:
- Add to PATH
- Add Open with Code action
- Create a desktop icon
- Register Code as an editor
Step 6: Click Install.
Step 7: After installation, open Visual Studio Code.
3. Install Java Extensions in VS Code
Step 1: Open Visual Studio Code.
Step 2: Click the Extensions icon on the left sidebar.
Step 3: Search for:
Extension Pack for Java
Step 4: Select the Microsoft Extension Pack for Java.
Step 5: Click Install.
Step 6: Search for Maven for Java and install it.
Step 7: Restart VS Code if required.
Important Extensions:
| Extension | Purpose |
| Extension Pack for Java | Java development and execution |
| Maven for Java | Project and dependency management |
| Debugger for Java | Debug Java programs |
| Test Runner for Java | Run JUnit test cases |
4. Install Apache Maven
Maven is used to download and manage Selenium and JUnit libraries automatically.
Step 1: Open the official website.
Download Apache Maven
Step 2: Download the Binary zip archive.
Step 3: Extract the downloaded ZIP file to a folder, for example:
C:\apache-maven
Step 4: Configure the system environment variables.
- Create MAVEN_HOME pointing to the extracted Maven folder.
- Add %MAVEN_HOME%\bin to the system PATH.
Step 5: Open a new Command Prompt.
Step 6: Enter:
mvn -version
Step 7: Verify the Maven version information.
If Maven is already available through VS Code or another installation, verify it using mvn -version before installing it again.
5. Install Google Chrome
Step 1: Open the official Chrome website.
Download Google Chrome
Step 2: Download and install Google Chrome.
Step 3: Open the browser and verify that it works.
Note: Selenium 4 includes Selenium Manager. It can automatically manage compatible browser drivers in normal supported configurations. Manual ChromeDriver installation is generally not required.
6. Create a Selenium Maven Project in VS Code
Step 1: Open VS Code.
Step 2: Press Ctrl + Shift + P.
Step 3: Search for:
Java: Create Java Project
Step 4: Select Maven.
Step 5: Select Maven Archetype Quickstart or an available Maven project template.
Step 6: Enter the project name:
SoftwareTestingLab
Step 7: Select a folder to save the project.
Step 8: Wait until Maven finishes creating the project.
Step 9: Open the project folder in VS Code.
7. Configure Selenium and JUnit Dependencies
Open the pom.xml file in the project folder. Add the following dependencies inside the dependencies section.
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.27.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<version>1.11.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.2</version>
</plugin>
</plugins>
</build>
Also ensure the project uses Java 17 or a compatible newer JDK. If necessary, add the following inside the pom.xml project element:
<properties>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Step 1: Save pom.xml.
Step 2: Wait for VS Code to download the required libraries.
Step 3: If required, open the terminal using:
Terminal → New Terminal
Step 4: Enter:
mvn clean install
Step 5: Wait until the BUILD SUCCESS message appears.
8. How to Write and Run Java Programs in VS Code
Step 1: Open the project folder.
Step 2: Locate:
src/main/java
Step 3: Right-click and create a new Java file.
Step 4: Give the file a name, for example:
BrowserLaunch.java
Step 5: Type the Java program.
Step 6: Save using Ctrl + S.
Step 7: Click the Run button above the main method.
Step 8: Observe the output in the VS Code terminal.
9. How to Run JUnit Test Cases in VS Code
Step 1: Save the test file under:
src/test/java
Step 2: Use the @Test annotation.
Step 3: Save the file.
Step 4: Click Run Test displayed above the test method, if available.
Step 5: Alternatively, execute the following command in the terminal:
mvn test
Step 6: Check the test results in the terminal or Testing panel.
Successful Installation: The environment is ready when Java, Maven, Selenium and JUnit are configured and a simple browser automation program executes successfully.
STEP 2: LABORATORY EXERCISES
LAB EXERCISE 1: Identify Testing Types for a Sample Application
Aim
To identify different types of software testing using a sample application.
Requirements
- Computer system
- Internet browser
- Sample online shopping application
Procedure
- Select an online shopping application.
- Identify its major functionalities.
- Study different software testing types.
- Match each testing type with the appropriate application functionality.
- Record the testing types and their purposes.
Testing Types
| Testing Type | Description | Example |
| Unit Testing | Tests individual components. | Testing login function. |
| Integration Testing | Tests combined modules. | Payment gateway integration. |
| System Testing | Tests the complete application. | Complete shopping process. |
| Acceptance Testing | Checks user requirements. | Customer order completion. |
| Functional Testing | Checks application functionality. | Add to Cart button. |
| Performance Testing | Checks application speed. | Multiple users accessing website. |
| Security Testing | Checks security vulnerabilities. | Password protection. |
| Usability Testing | Checks user-friendliness. | Easy website navigation. |
Expected Output
A list of software testing types with suitable examples for the selected application.
Result: Different software testing types were identified successfully.
LAB EXERCISE 2: Write Test Cases for a Login Page
Aim
To prepare and execute test cases for validating a login page.
Procedure
- Open a sample login page.
- Identify the username and password fields.
- Prepare positive and negative test cases.
- Enter different input values.
- Compare expected and actual results.
- Record the results.
Test Cases
| ID | Test Scenario | Input | Expected Result |
| TC01 | Valid login | Correct username and password | Login successful |
| TC02 | Invalid password | Correct username, wrong password | Error message |
| TC03 | Invalid username | Wrong username | Error message |
| TC04 | Empty fields | No input | Validation message |
| TC05 | Password masking | Enter password | Password hidden |
| TC06 | Forgot password | Click forgot password | Password reset page |
Expected Output
Login test cases with expected results.
Result: Test cases for the login page were prepared successfully.
LAB EXERCISE 3: Design Test Scenarios for a Web Form
Aim
To design test scenarios for validating a registration form.
Procedure
- Open a registration form.
- Identify the input fields.
- Prepare different test scenarios.
- Check mandatory field validation.
- Verify the email and mobile number formats.
- Check form submission.
Sample Test Scenarios
| ID | Test Scenario | Expected Result |
| TS01 | Enter valid name | Name accepted |
| TS02 | Enter invalid email | Validation error |
| TS03 | Enter valid mobile number | Number accepted |
| TS04 | Leave mandatory fields empty | Error displayed |
| TS05 | Enter weak password | Password validation message |
| TS06 | Submit valid information | Registration successful |
Expected Output
Test scenarios for a registration form.
Result: Test scenarios were designed successfully.
LAB EXERCISE 4: Perform Manual Testing and Record Defects
Aim
To perform manual testing and identify defects in a sample web application.
Procedure
- Select a sample web application.
- Prepare test cases.
- Execute each test case manually.
- Compare actual results with expected results.
- Identify application defects.
- Prepare a defect report.
Sample Defect Report
| Defect ID | Description | Severity | Status |
| BUG01 | Login accepts empty password | High | Open |
| BUG02 | Submit button not responding | Medium | Open |
| BUG03 | Error message not displayed | Low | Open |
Expected Output
Manual testing report containing identified defects.
Result: Manual testing was performed and defects were recorded successfully.
LAB EXERCISE 5: Install Selenium and Configure Environment
Aim
To install Selenium WebDriver and configure the automation testing environment using VS Code.
Requirements
- Java JDK
- Visual Studio Code
- Google Chrome
- Apache Maven
- Selenium WebDriver
- JUnit 5
Procedure
- Install Java JDK.
- Verify Java using java -version.
- Install Visual Studio Code.
- Install the Extension Pack for Java.
- Install Maven for Java.
- Install Apache Maven and configure PATH.
- Create a Maven project.
- Add Selenium and JUnit dependencies in pom.xml.
- Execute mvn clean install.
- Verify successful dependency installation.
Maven Dependency
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.27.0</version>
</dependency>
Verification Command
java -version
mvn -version
mvn clean install
Expected Output
Java, Maven and Selenium environment configured successfully.
Result: Selenium WebDriver environment was installed and configured successfully.
LAB EXERCISE 6: Launch Browser Using Selenium WebDriver
Aim
To launch a web browser using Selenium WebDriver.
Procedure
- Open VS Code.
- Open the Maven project.
- Create BrowserLaunch.java inside src/main/java.
- Import Selenium packages.
- Create a ChromeDriver object.
- Open Google using get().
- Display the page title.
- Close the browser.
Program
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class BrowserLaunch {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
System.out.println(
"Title: " + driver.getTitle()
);
driver.quit();
}
}
Execution
Save the file and click Run above the main method.
Expected Output
Title: Google
Chrome browser opens successfully.
Result: Browser was launched successfully using Selenium WebDriver.
LAB EXERCISE 7: Locate Web Elements Using Different Locators
Aim
To locate web elements using different Selenium WebDriver locators.
Types of Locators
| Locator | Purpose | Example |
| ID | Find using element ID | By.id("username") |
| Name | Find using name | By.name("q") |
| Class Name | Find using CSS class | By.className("search") |
| Tag Name | Find using HTML tag | By.tagName("input") |
| Link Text | Find complete link text | By.linkText("Login") |
| Partial Link Text | Find partial link text | By.partialLinkText("Log") |
| CSS Selector | Find using CSS selector | By.cssSelector("#username") |
| XPath | Find using XPath | By.xpath("//input[@id='username']") |
Program
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class LocatorsDemo {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
WebElement search =
driver.findElement(By.name("q"));
search.sendKeys("Software Testing");
search.submit();
driver.quit();
}
}
Expected Output
Google search results for Software Testing are displayed.
Result: Web elements were located successfully using Selenium locators.
LAB EXERCISE 8: Perform Actions (Click, Send Keys, Submit)
Aim
To perform basic web element actions using Selenium WebDriver.
Important Methods
| Method | Purpose |
| click() | Clicks an element. |
| sendKeys() | Enters text into a field. |
| submit() | Submits a form. |
| clear() | Clears an input field. |
Program
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class ActionsDemo {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
WebElement search =
driver.findElement(By.name("q"));
search.sendKeys("Selenium Automation");
search.submit();
System.out.println(driver.getTitle());
driver.quit();
}
}
Expected Output
The browser searches for Selenium Automation and displays the search results.
Result: Click, sendKeys and submit actions were performed successfully.
LAB EXERCISE 9: Automate Form Filling Using Selenium
Aim
To automate filling and submitting a registration form using Selenium WebDriver.
Procedure
- Create a sample HTML registration form.
- Identify the form fields using ID attributes.
- Open the form using Selenium.
- Enter name, email and password.
- Click the Register button.
- Verify the successful submission message.
HTML Form
<!DOCTYPE html>
<html>
<body>
<h2>Registration Form</h2>
<form id="registerForm">
Name:
<input type="text" id="name" required>
<br><br>
Email:
<input type="email" id="email" required>
<br><br>
Password:
<input type="password"
id="password" required>
<br><br>
<button type="submit">Register</button>
</form>
<p id="message"></p>
<script>
document.getElementById("registerForm")
.addEventListener("submit", function(e) {
e.preventDefault();
document.getElementById("message")
.innerText = "Registration Successful";
});
</script>
</body>
</html>
Save the File
Save the above code as:
form.html
Place it inside:
src/test/resources/form.html
Selenium Program
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.nio.file.Paths;
public class FormAutomation {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
try {
String filePath = Paths.get(
"src/test/resources/form.html"
).toAbsolutePath().toUri().toString();
driver.get(filePath);
driver.findElement(By.id("name"))
.sendKeys("Student");
driver.findElement(By.id("email"))
.sendKeys("test@gmail.com");
driver.findElement(By.id("password"))
.sendKeys("Test123");
driver.findElement(
By.cssSelector("button[type='submit']")
).click();
String message = driver.findElement(
By.id("message")
).getText();
System.out.println(message);
} finally {
driver.quit();
}
}
}
Expected Output
Registration Successful
Result: Registration form fields were automatically filled and submitted successfully.
LAB EXERCISE 10: Handle Alerts and Pop-ups
Aim
To handle JavaScript alerts using Selenium WebDriver.
Types of Alerts
- Simple Alert
- Confirmation Alert
- Prompt Alert
Alert Methods
| Method | Purpose |
| accept() | Clicks OK. |
| dismiss() | Clicks Cancel. |
| getText() | Reads the alert message. |
| sendKeys() | Enters text in a prompt. |
HTML Alert Page
<html>
<body>
<button id="alertBtn"
onclick="alert('Welcome to Selenium Testing')">
Show Alert
</button>
</body>
</html>
Save as alert.html inside src/test/resources.
Java Program
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.nio.file.Paths;
public class AlertDemo {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
try {
String path = Paths.get(
"src/test/resources/alert.html"
).toAbsolutePath().toUri().toString();
driver.get(path);
driver.findElement(By.id("alertBtn")).click();
Alert alert = driver.switchTo().alert();
System.out.println(alert.getText());
alert.accept();
} finally {
driver.quit();
}
}
}
Expected Output
Welcome to Selenium Testing
Result: JavaScript alerts were handled successfully.
LAB EXERCISE 11: Implement Wait Conditions in Selenium
Aim
To implement different wait conditions for handling dynamic web elements.
Types of Waits
| Wait Type | Description |
| Implicit Wait | Waits for elements while searching for them. |
| Explicit Wait | Waits for a specific condition. |
| Fluent Wait | Waits with configurable polling intervals and ignored exceptions. |
Program – Explicit Wait
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.*;
import java.time.Duration;
public class WaitDemo {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
try {
driver.get("https://www.google.com");
WebDriverWait wait =
new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement search = wait.until(
ExpectedConditions
.visibilityOfElementLocated(
By.name("q")
)
);
search.sendKeys("Software Testing");
search.submit();
System.out.println("Search Completed");
} finally {
driver.quit();
}
}
}
Expected Output
Search Completed
Result: Explicit wait was implemented successfully.
LAB EXERCISE 12: Write JUnit Test Cases
Aim
To create and execute test cases using the JUnit 5 framework.
Important JUnit Annotation
| Annotation | Purpose |
| @Test | Identifies a test method. |
| @BeforeEach | Executes before each test. |
| @AfterEach | Executes after each test. |
| @BeforeAll | Executes once before all tests. |
| @AfterAll | Executes once after all tests. |
Program
Save this file inside src/test/java.
import org.junit.jupiter.api.Test;
public class SampleTest {
@Test
public void testLogin() {
System.out.println(
"Testing Login Function"
);
}
@Test
public void testRegistration() {
System.out.println(
"Testing Registration Function"
);
}
}
Execution
mvn test
Expected Output
JUnit executes both test methods successfully.
Result: JUnit test cases were created and executed successfully.
LAB EXERCISE 13: Use Assertions in JUnit
Aim
To validate actual results against expected results using JUnit assertions.
Types of Assertions
| Assertion | Purpose |
| assertEquals() | Compares two values. |
| assertNotEquals() | Checks unequal values. |
| assertTrue() | Checks whether a condition is true. |
| assertFalse() | Checks whether a condition is false. |
| assertNull() | Checks for null. |
| assertNotNull() | Checks for non-null. |
Program
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class AssertionDemo {
@Test
public void testAddition() {
int a = 10;
int b = 20;
int result = a + b;
assertEquals(30, result);
}
@Test
public void testCondition() {
assertTrue(10 > 5);
}
@Test
public void testString() {
String name = "Selenium";
assertNotNull(name);
}
}
Expected Output
All three test methods pass when their conditions are satisfied.
Result: JUnit assertions were implemented successfully.
LAB EXERCISE 14: Execute Test Suite Using JUnit
Aim
To execute multiple test classes using a JUnit test suite.
Procedure
- Create separate JUnit test files.
- Write test methods in each file.
- Create a test suite file.
- Use @Suite annotation.
- Use @SelectClasses to select the test files.
- Run the test suite using Maven.
First Test File – LoginTest.java
import org.junit.jupiter.api.Test;
public class LoginTest {
@Test
public void testLogin() {
System.out.println("Login Test Passed");
}
}
Second Test File – RegistrationTest.java
import org.junit.jupiter.api.Test;
public class RegistrationTest {
@Test
public void testRegistration() {
System.out.println(
"Registration Test Passed"
);
}
}
Test Suite – TestSuiteDemo.java
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;
@Suite
@SelectClasses({
LoginTest.class,
RegistrationTest.class
})
public class TestSuiteDemo {
}
Execution
mvn test
Place all three files inside src/test/java. The Maven Surefire plugin discovers the suite using its standard test naming patterns. Alternatively, run the suite directly using VS Code's Run Test option.
Expected Output
Login Test Passed
Registration Test Passed
Tests run: 2
Failures: 0
Result: Multiple JUnit test cases were executed through a single test suite successfully.
LAB EXERCISE 15: Integrate Selenium with JUnit and Run Automation Tests
Aim
To integrate Selenium WebDriver with JUnit and execute automated browser testing.
Procedure
- Create a Maven project.
- Configure Selenium and JUnit dependencies.
- Create a JUnit test file inside src/test/java.
- Initialize the browser before each test.
- Write test methods using @Test.
- Use assertions to verify expected results.
- Close the browser after each test.
- Execute tests using Maven.
Program
import org.junit.jupiter.api.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.junit.jupiter.api.Assertions.*;
public class SeleniumJUnitTest {
WebDriver driver;
@BeforeEach
public void setUp() {
driver = new ChromeDriver();
driver.get("https://www.google.com");
}
@Test
public void testPageTitle() {
String title = driver.getTitle();
assertTrue(title.contains("Google"));
}
@Test
public void testSearch() {
WebElement search =
driver.findElement(By.name("q"));
search.sendKeys("Selenium");
search.submit();
assertTrue(
driver.getTitle().contains("Selenium")
);
}
@AfterEach
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
Execution
Save the file as SeleniumJUnitTest.java inside src/test/java.
Open the VS Code terminal and execute:
mvn test
Expected Output
Tests run: 2
Failures: 0
Errors: 0
BUILD SUCCESS
Result: Selenium WebDriver was integrated with JUnit and automated browser test cases were executed successfully.
STEP 3: IMPORTANT PROJECT STRUCTURE
The following is the recommended Maven project folder structure for completing all the practical exercises.
SoftwareTestingLab/
│
├── pom.xml
│
└── src/
│
├── main/
│ └── java/
│ ├── BrowserLaunch.java
│ ├── LocatorsDemo.java
│ ├── ActionsDemo.java
│ ├── FormAutomation.java
│ ├── AlertDemo.java
│ └── WaitDemo.java
│
└── test/
│
├── java/
│ ├── SampleTest.java
│ ├── AssertionDemo.java
│ ├── LoginTest.java
│ ├── RegistrationTest.java
│ ├── TestSuiteDemo.java
│ └── SeleniumJUnitTest.java
│
└── resources/
├── form.html
└── alert.html
STEP 4: COMMON EXECUTION COMMANDS
| Command | Purpose |
| java -version | Check Java installation |
| javac -version | Check Java compiler |
| mvn -version | Check Maven installation |
| mvn clean | Clean previous build files |
| mvn compile | Compile main source files |
| mvn test | Execute JUnit tests |
| mvn clean install | Build the complete project |
STEP 5: TROUBLESHOOTING
| Problem | Solution |
| Java not recognized | Check JDK installation and PATH. |
| Maven not recognized | Configure MAVEN_HOME and PATH. |
| Package not found | Check pom.xml dependencies and reload Maven. |
| ChromeDriver error | Check Chrome installation and Selenium Manager access. |
| JUnit test not running | Check JUnit dependency and test file location. |
| Element not found | Verify the locator and use suitable wait conditions. |
| Browser closes immediately | Check program execution and browser quit statements. |
CONCLUSION
Through these 15 laboratory exercises, students gain practical knowledge of software testing techniques, test case preparation, manual testing, defect identification, Selenium WebDriver automation, web element interaction, wait conditions, JUnit testing, assertions and automated test suite execution.