SOFTWARE TESTING AND AUTOMATION LAB
Practical Laboratory Manual
Manual Testing | Selenium WebDriver | JUnit
15 Lab ExercisesLAB 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)
java -version
javac -version
Step 7: Verify that Java version information is displayed.
2. Install Visual Studio Code
- Add to PATH
- Add Open with Code action
- Create a desktop icon
- Register Code as an editor
3. Install Java Extensions in VS Code
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.
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.
mvn -version
Step 7: Verify the Maven version information.
5. Install Google Chrome
6. Create a Selenium Maven Project in VS Code
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>
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
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
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
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
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.
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.
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
Expected Output
Login Test Passed
Registration Test Passed
Tests run: 2
Failures: 0
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
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.
No comments:
Post a Comment