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

Author: neptune | 08th-Aug-2023
#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




anonymous | July 25, 2023, 11:27 p.m.

Nice selenium WebDriver coding👍


anonymous | June 23, 2022, 10:32 p.m.

Really helpful 👍


anonymous | April 15, 2022, 3:01 p.m.

Add more details


anonymous | Feb. 18, 2022, 12:33 p.m.

Nice Content


anonymous | Sept. 8, 2021, 11:41 a.m.

Try to check locator for date


anonymous | Aug. 31, 2021, 11:48 a.m.

Not able to select date



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...

How to create a Selenium, Cucumber Automation project in Eclipse ?
Author: neptune | 02nd-Apr-2023
#Selenium #Testing
Problem Statement : Let’s consider this particular project where we will try automating the process of booking a flight ticket. Let’s get started and see how it’s done using Selenium...

Challenges faced in automating Web Applications using Selenium
Author: neptune | 02nd-Oct-2022
#Selenium #Testing
List of Challenges faced by testers using Selenium 1. Popup and Alert Handling, 2. Dynamic element Handling 3. Restricted to only Desktop Browsers Testing...

20 TestNG Interview Questions
Author: neptune | 17th-Dec-2022
#Selenium #Interview
There is always more than one test or method in the class. If we do not prioritize these tests or methods, then the methods are selected alphabetically and executed while execution...

Getting started with Selenium WebDriver.
Author: neptune | 02nd-Apr-2023
#Selenium #Testing
In this blog I will give you an overview of Selenium WebDriver and Also discuss about advantages and disadvantages of Selenium WebDriver...

Why we use Robot Class in Automation Testing?
Author: neptune | 23rd-Aug-2022
#Selenium
Robot class is used to generate native system input events for the purpose of automation testing. It is used to perform mouse and keyboard events on windows applications or popups...

Top 10 Selenium Interview Questions.
Author: neptune | 02nd-Apr-2023
#Selenium #Interview
Locator is a command that tells Selenium IDE which GUI elements (like Text Box, Buttons, Check Boxes etc) we are going to use or perform some automation task...

How to send POST request using Rest Assured framework in Java ?
Author: neptune | 26th-Mar-2023
#Selenium #API
Rest Assured is a popular Java-based framework that is used for testing RESTful web services. It provides a simple and intuitive API for sending HTTP requests and validating the responses...

View More