Automate Ticket Booking in SpiceJet from Delhi to Bengaluru using Selenium and Cucumber.

Author: neptune | 06th-Jul-2024
#Selenium

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.



Prerequisites:

Before we begin, make sure you have the following:

  1. Maven installed on your system.

  2. Eclipse or IntelliJ IDE set up.

  3. Basic knowledge of Java programming language.

Setting up the Project:

Follow the steps below to set up the project:

Step 1: Create a Maven 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.

Step 2: Add Dependencies

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.

Step 3: Create Feature File

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


Step 4: Create Step Definitions

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();

    }

}


Running the Test:

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.

Conclusion:

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




Related Blogs
Selenium, Cucumber, JUnit, TestNG dependencies for Selenium project.
Author: neptune | 02nd-Apr-2023
#Selenium #Testing
We guide you how to update the pom.xml file for Selenium Maven project...

5 Selenium Project Ideas & for Beginners in Automation Testing
Author: neptune | 30th-Mar-2023
#Selenium #Testing #Projects
In this article, we will discuss 5 interesting Selenium project ideas for beginners in automation testing...

How to use wait commands in Selenium WebDriver in Java ?
Author: neptune | 22nd-Feb-2022
#Selenium #Testing #Java
We are going to explore different types of waits in Selenium WebDriver. Implicit wait, Explicit wait, and Fluent wait with examples...

Top 50+ Selenium Interviews Questions 2023 based on Years of Experience
Author: neptune | 02nd-Apr-2023
#Selenium #Testing #Interview
Every interview difficulty is based on how many years of experience you have in that field. For the Selenium Automation Tester I have divided the question on the number of years of experience...

Top 10 Selenium Interview Questions with answers (2021).
Author: neptune | 02nd-Apr-2023
#Selenium #Interview
In this article I will cover top 10 Selenium interview questions...

How to create Selenium Cucumber Project in Eclipse.
Author: neptune | 25th-May-2022
#Selenium
First, divide the process into the various steps to understand working in brief of a project. Steps in brief: We’ll start with initializing the browser driver and then log in to the web page...

View More