Cucumber Spicejet
Feature file:
Feature: SpiceJet Flight Booking
Scenario: Login to SpiceJet
Given the user navigates to the SpiceJet homepage
When the user clicks on the Login button
And enters the mobile number "9025661542" and password "Suganya@123"
Then the user should be logged in successfully
Scenario: Search flights for a one-way trip
Given the user is on the SpiceJet homepage
When the user searches for a flight from "Dharamshala" to "Goa International Airport"
And selects the departure date
Then the search results should display available flights
Scenario: Book a flight
Given the user has selected a flight
When the user enters passenger details as "SUGANYA" and "SS"
And confirms the booking
Then the booking should proceed to the payment page
Scenario: Handle payment
Given the user is on the payment page
When the user enters card details "4016333300000026"
Then an error message should be displayed if the payment fails
Login page:
package pages;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedConditions;
import java.time.Duration;
public class LoginPage { WebDriver driver; WebDriverWait wait;
// Locators @FindBy(xpath = "//div[contains(text(),'Login')]") WebElement loginButton;
@FindBy(xpath = "//input[@data-testid='user-mobileno-input-box']") WebElement mobileField;
@FindBy(xpath = "//input[@type='password']") WebElement passwordField;
@FindBy(css = ".css-1dbjc4n.r-1awozwy.r-184aecr.r-z2wwpe.r-1loqt21.r-18u37iz.r-tmtnm0.r-1777fci.r-1x0uki6.r-1w50u8q.r-ah5dr5.r-1otgn73") WebElement loginSubmitButton;
// Constructor public LoginPage(WebDriver driver) { this.driver = driver; this.wait = new WebDriverWait(driver, Duration.ofSeconds(15)); PageFactory.initElements(driver, this); }
// Actions public void clickLoginButton() { loginButton.click(); }
public void enterMobileNumber(String mobile) { wait.until(ExpectedConditions.visibilityOf(mobileField)); mobileField.sendKeys(mobile); }
public void enterPassword(String password) { passwordField.sendKeys(password); }
public void clickSubmit() { loginSubmitButton.click(); } }
Search flights:
package pages;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory;
public class SearchFlightsPage { WebDriver driver;
@FindBy(xpath = "//div[@data-testid='to-testID-origin']//input[@type='text']") WebElement originInput;
@FindBy(xpath = "//div[contains(text(),'Dharamshala')]") WebElement originOption;
@FindBy(css = ".css-76zvg2.r-cqee49.r-ubezar.r-1kfrs79.r-1ozqkpa") WebElement destinationInput;
@FindBy(xpath = "//div[normalize-space()='Goa International Airport']") WebElement destinationOption;
@FindBy(css = "div[class='css-1dbjc4n r-18u37iz'] div:nth-child(1)") WebElement dateSelect;
@FindBy(css = ".css-1dbjc4n.r-1awozwy.r-z2wwpe.r-1loqt21.r-18u37iz.r-1777fci.r-1g94qm0.r-1w50u8q.r-ah5dr5.r-1otgn73") WebElement searchButton;
// Constructor public SearchFlightsPage(WebDriver driver) { this.driver = driver; PageFactory.initElements(driver, this); }
// Actions public void enterOrigin() { originInput.click(); originOption.click(); }
public void enterDestination() { destinationInput.click(); destinationOption.click(); }
public void selectDate() { dateSelect.click(); }
public void clickSearch() { searchButton.click(); } }
Booking page
package pages;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory;
public class BookingPage { WebDriver driver;
@FindBy(xpath = "(//input[@type='text'])[6]") WebElement firstNameField;
@FindBy(xpath = "(//input[@type='text'])[7]") WebElement lastNameField;
@FindBy(css = ".css-1dbjc4n.r-1kihuf0.r-ometjm.r-1251kcm") WebElement bookButton;
// Constructor public BookingPage(WebDriver driver) { this.driver = driver; PageFactory.initElements(driver, this); }
// Actions public void enterPassengerDetails(String firstName, String lastName) { firstNameField.sendKeys(firstName); lastNameField.sendKeys(lastName); }
public void confirmBooking() { bookButton.click(); } }
Stepdefinition
package stepdefinition;
import io.cucumber.java.en.*;
import org.junit.After; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import pages.*;
public class SpiceJetStepDefinitions { WebDriver driver; LoginPage loginPage; SearchFlightsPage searchFlightsPage; BookingPage bookingPage;
@Given("the user navigates to the SpiceJet homepage") public void navigateToHomepage() { driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://www.spicejet.com/");
// Initialize page objects loginPage = new LoginPage(driver); searchFlightsPage = new SearchFlightsPage(driver); bookingPage = new BookingPage(driver); }
@When("the user clicks on the Login button") public void clickLoginButton() { loginPage.clickLoginButton(); }
@And("enters the mobile number {string} and password {string}") public void enterLoginDetails(String mobile, String password) { loginPage.enterMobileNumber(mobile); loginPage.enterPassword(password); loginPage.clickSubmit(); }
@When("the user searches for a flight from {string} to {string}") public void searchFlight(String origin, String destination) { searchFlightsPage.enterOrigin(); searchFlightsPage.enterDestination(); searchFlightsPage.selectDate(); searchFlightsPage.clickSearch(); }
@And("enters passenger details as {string} and {string}") public void enterPassengerDetails(String firstName, String lastName) { bookingPage.enterPassengerDetails(firstName, lastName); bookingPage.confirmBooking(); }
@Then("the booking should proceed to the payment page") public void proceedToPayment() { System.out.println("Proceeded to the payment page."); }
@After public void tearDown() { if (driver != null) { driver.quit(); } } }
Test runner
package testrunner;
import io.cucumber.junit.Cucumber; import io.cucumber.junit.CucumberOptions; import org.junit.runner.RunWith; @RunWith(Cucumber.class) @CucumberOptions( features = "src/test/java/featurefile/SpiceJetBooking.feature", glue = "stepdefinition", plugin = {"pretty", "html:target/cucumber-reports.html"} )
public class TestRunner { }