In this article, we will guide you through the process of automating the ticket booking scenario on the SpiceJet website. We will use Cucumber and Selenium WebDriver for this automation project. By the end of this article, you will have a clear understanding of the steps involved in automating ticket booking on SpiceJet and be able to create an automation framework for similar scenarios.
Before we begin, make sure you have the following:
Maven installed on your system.
Eclipse or IntelliJ IDE set up.
Basic knowledge of Java programming language.
Setting up the Project:
Follow the steps below to set up the project:
Create a new Maven project in Eclipse or IntelliJ IDE. This will serve as the base for our automation framework.
Check here to create a project.
Open the pom.xml file and add the necessary dependencies for Cucumber, TestNG, and Selenium WebDriver. Make sure to include the appropriate versions for each dependency.
Check dependencies here.
<!-- Cucumber dependencies -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>6.10.0</version>
<scope>test</scope>
</dependency>
<!-- Selenium WebDriver dependency -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
Make sure to save the pom.xml file to download the dependencies.
Create a new feature file named SpiceJetTicketBooking.feature. This file will contain the Gherkin syntax-based test scenarios.
@Smoke
Feature: Book Ticket
I want to book a ticket in SpiceJet
@Test1
Scenario: Book Ticket from Delhi to Bengaluru
Given I Open URL "https://www.spicejet.com/" and launch driver
When I search ticket from "Delhi" to "Bengaluru" Depart on "10-April-2021" return on "11-June-2021"
Then I close driver
Create a new Java class named SpiceJetStepDef in the package TestAutomation.StepDef. This class will contain the step definitions for the scenarios defined in the feature file.
package TestAutomation.StepDef;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class SpiceJetStepDef {
public static WebDriver driver;
@Given("^I Open URL \"([^\"]*)\" and launch driver$")
public void i_Open_URL_and_launch_driver(String url) throws Throwable {
// Set ChromeDriver path
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver.exe");
// Initialize ChromeDriver
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Open the URL
driver.get(url);
}
@When("^I search ticket from \"([^\"]*)\" to \"([^\"]*)\" Depart on \"([^\"]*)\" return on \"([^\"]*)\"$")
public void i_search_ticket_from_to_Depart_on_return_on(String depcity, String arricity, String depdate,
String returndate) throws Throwable {
// Select round trip
driver.findElement(By.xpath("//input[@id='ctl00_mainContent_rbtnl_Trip_1']")).click();
// Enter Depart city and arrival city
driver.findElement(By.xpath("(//span[@id='ctl00_mainContent_ddl_originStation1_CTXTaction'])")).click();
WebElement departcity = driver.findElement(By.xpath("(//a[@value='DEL'])"));
departcity.click();
Thread.sleep(2000);
WebElement arrivalcity = driver.findElement(By.xpath("(//a[@value='BLR'])[2]"));
arrivalcity.click();
// Select Depart date
String dep_bookdate = depdate.split("-")[0];
String dep_bookmonth = depdate.split("-")[1];
String dep_bookyear = depdate.split("-")[2];
String currentmonth = driver.findElement(By.xpath("(//span[@class='ui-datepicker-month'])[1]")).getText()
.trim();
String currentyear = driver.findElement(By.xpath("(//span[@class='ui-datepicker-year'])[1]")).getText()
.trim();
while (!currentmonth.equals(dep_bookmonth) || (!currentyear.equals(dep_bookyear))) {
driver.findElement(By.xpath("(//span[text()='Next'])")).click();
currentmonth = driver.findElement(By.xpath("(//span[@class='ui-datepicker-month'])[1]")).getText().trim();
currentyear = driver.findElement(By.xpath("(//span[@class='ui-datepicker-year'])[1]")).getText().trim();
}
List<WebElement> dates = driver.findElements(By.xpath("//a[@class='ui-state-default']"));
for (WebElement e : dates) {
if (e.getText().trim().equals(dep_bookdate)) {
e.click();
break;
}
}
// Select Return date
WebElement returndatepicker = driver.findElement(By.xpath("(//button[@class='ui-datepicker-trigger'])[2]"));
returndatepicker.click();
String return_bookdate = returndate.split("-")[0];
String return_bookmonth = returndate.split("-")[1];
String return_bookyear = returndate.split("-")[2];
String return_currentmonth = driver.findElement(By.xpath("(//span[@class='ui-datepicker-month'])[2]"))
.getText().trim();
String return_currentyear = driver.findElement(By.xpath("(//span[@class='ui-datepicker-year'])[2]")).getText()
.trim();
while (!return_currentmonth.equals(return_bookmonth) || (!return_currentyear.equals(return_bookyear))) {
driver.findElement(By.xpath("(//span[text()='Next'])")).click();
return_currentmonth = driver.findElement(By.xpath("(//span[@class='ui-datepicker-month'])[2]")).getText()
.trim();
return_currentyear = driver.findElement(By.xpath("(//span[@class='ui-datepicker-year'])[2]")).getText()
.trim();
}
List<WebElement> return_dates = driver.findElements(By.xpath("//a[@class='ui-state-default']"));
for (WebElement e : return_dates) {
if (e.getText().trim().equals(return_bookdate)) {
e.click();
break;
}
}
// Click on Find Flights button
driver.findElement(By.xpath("(//input[@id='ctl00_mainContent_btn_FindFlights'])")).click();
Thread.sleep(5000);
}
@Then("^I close driver$")
public void i_close_driver() {
// Close the driver
driver.close();
}
}
To run the test, right-click on the TestRunner class and select "Run as" > "JUnit Test". This will execute the Cucumber scenarios defined in the feature file.
In this article, we have automated the ticket booking scenario on the SpiceJet website using Cucumber and Selenium WebDriver. We have covered the steps involved in setting up the project, creating feature files, step definitions, and the test runner class. By following these steps, you can automate other scenarios and expand the automation framework according to your requirements. Automation projects like this are a great way to enhance your skills and gain hands-on experience in test automation.
Happy testing !!!
Read more : ROADMAP TO BECOME TEST AUTOMATION ENGINEER