Assignment 2 -
✅ Prerequisites:
● Eclipse
● Java JDK
● Maven
● Cucumber Eclipse Plugin
● Selenium WebDriver
● Cucumber JUnit
✅ Project Structure:
src/test/java/
├── features/
│ └── login.feature
├── stepdefinitions/
│ └── LoginSteps.java
│ └── Hooks.java
├── testrunner/
│ └── TestRunner.java
✅ Step-by-Step Implementation
1. pom.xml Dependencies
<dependencies>
<!-- Selenium -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.12.0</version>
</dependency>
<!-- Cucumber Java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.14.0</version>
</dependency>
<!-- Cucumber JUnit -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>7.14.0</version>
<scope>test</scope>
</dependency>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
2. Feature File – login.feature
Feature: User Login
Scenario: Successful login with valid credentials
Given the user is on the login page
When the user enters valid username and password
And clicks the login button
Then the user should be redirected to the dashboard
3. Step Definitions – LoginSteps.java
package stepdefinitions;
import io.cucumber.java.en.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class LoginSteps {
WebDriver driver;
@Given("the user is on the login page")
public void the_user_is_on_the_login_page() {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
driver = new ChromeDriver();
driver.get("https://example.com/login"); // Replace with real login URL
}
@When("the user enters valid username and password")
public void the_user_enters_valid_credentials() {
driver.findElement(By.id("username")).sendKeys("testuser");
driver.findElement(By.id("password")).sendKeys("password123");
}
@When("clicks the login button")
public void clicks_the_login_button() {
driver.findElement(By.id("loginButton")).click();
}
@Then("the user should be redirected to the dashboard")
public void the_user_should_be_redirected_to_the_dashboard() {
String expectedUrl = "https://example.com/dashboard";
boolean success = driver.getCurrentUrl().equals(expectedUrl);
assert success : "User not redirected correctly.";
driver.quit();
}
}
4. Hooks – Hooks.java
package stepdefinitions;
import io.cucumber.java.*;
public class Hooks {
@Before
public void beforeScenario() {
System.out.println("=== Script Start Execution ===");
}
@After
public void afterScenario() {
System.out.println("=== Script End Execution ===");
}
}
5. Test Runner – TestRunner.java
package testrunner;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/java/features",
glue = {"stepdefinitions"},
plugin = {"pretty", "html:target/cucumber-reports"},
monochrome = true
)
public class TestRunner {}
✅ How to Run:
1. Right-click TestRunner.java → Run As → JUnit Test