Automating a Script to Get The Lowest Price for Travelling between two cities Using Apache POI

We already know that Apache provides a library POI which helps to perform read and write with an excel sheet while using selenium web driver. In the previous articles, we learnt how to read from an excel file as well as how to write in an excel file. But what if you want to fetch the values from the excel file i.e; read the values from an excel file and perform some operations using these value, say you want to pass these values as inputs to somewhere through your script. In this case, here we are taking an example in which you need to find the travel cost from one city to another city. This is actually easy to write a script for it, you don’t even need to create an excel file for it. Directly in the script you can mention the source and destination city to find the cost and automate it. But the problem comes when you have to find the travel cost for more than one cities. Suppose you want to find the travel cost from Bangalore-New York, Bangalore-Delhi, Bangalore-Lucknow, Bangalore-Dellas…and 100 more cities. In this case, it will be very hectic to write the same code for all the cities for which we want to find the travel cost.

So what we will do is make an excel file. Create two columns Departure City  and Arrival City. And in these columns, list down all the departure and arrival cities for which you want to find out the travel cost. And then we can fetch the values from here and can automate the script.

But first of all let us learn how to find the travel cost from one city to another using selenium web driver script. Then we will proceed with different other cities using excel file.

Steps to find the Travel Cost From one City to Another City

1. Launch Eclipse, create a project “test” and a package named “home”, and a class “Demo1” under this package.

2. Right click on the project, select Properties, and then click on Java Build Path. Click on the Libraries tab, and go to Add external jars to add all the jar files of  selenium-2.44.0.

3. You have to launch Orbitz.com, so add the needed code in your script.

WebDriver driver=new FirefoxDriver();
driver.get(“http://www.orbitz.com/”);

4. Now you have to click on Flight Only radio button, to do this first you need to locate the radio button on that particular web page and then add in your script :

driver.findElement(By.xpath(“//*[@id=’products’]/div/fieldset/div[2]/label[1]/div”)).click();

5. The next step is click on the From City name text box and enter some city name, here we are taking it as Bangalore , “BLR”. And then click on the To City and enter some value.

driver.findElement(By.name(“ar.rt.leaveSlice.orig.key”)).sendKeys(“BLR”);
driver.findElement(By.name(“ar.rt.leaveSlice.dest.key”)).sendKeys(dest);

In the To City field, we are passing an argument instead of any city because our aim is to get the travel cost of “Bangalore-Lucknow” and “Bangalore-Delhi”. So here the departure city remains the same and arrival city is getting changed, so we will pass it later on. For now, we will create a function and we will pass an argument dest  in it, which while calling in main, we can give the arrival city name. Click on Search Flights, you will get the prices arranged in from lowest travel price. Now you need to click on the first displayed price, click on the Select button, you will get all the details, where the price will be displayed, get it. After writing all the steps to get the price, we can accumulate it in a function, which will look like :

aaa

public static double findPrice(String dest) throws InterruptedException

{

WebDriver driver=new FirefoxDriver();

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
To launch the Site
driver.get(“http://www.orbitz.com/”);
Click on radio button
driver.findElement(By.xpath(“//*[@id=’products’]/div/fieldset/div[2]/label[1]/div”)).click();
click on From city textbox and pass some value
driver.findElement(By.name(“ar.rt.leaveSlice.orig.key”)).sendKeys(“BLR”);
click on To City text box and pass some Arrival city
driver.findElement(By.name(“ar.rt.leaveSlice.dest.key”)).sendKeys(dest);
Leave date
driver.findElement(By.name(“ar.rt.leaveSlice.date”)).sendKeys(“03/20/2015”);
Return Date
driver.findElement(By.name(“ar.rt.returnSlice.date”)).sendKeys(“03/27/2015”);
Click on Search Flights Button
driver.findElement(By.name(“search”)).click();
Thread.sleep(1000);
Select the lowest price from the list
driver.findElement(By.xpath(“//*[@id=’main’]/div[9]/div[2]/div[1]/div[1]/div/div[2]/a”)).click();
Get the price
String price=driver.findElement(By.className(“mirrorCash”)).getText().replace(“$”, “”).replace(“,”,””);
System.out.println(price);
double dprice=Double.parseDouble(price);
return dprice;

6. Now we can call this function in the main() method and can pass the different arrival city.

public static void main(String[] args) throws InterruptedException
{
double price1=findPrice(“LKO”);
System.out.println(“Price From BLR-LKO is :” +price1);
double price2=findPrice(“DEL”);
System.out.println(“Price From BLR-DEL is :” +price2);

//Compare the prices
if(price1 > price2)
{
System.out.println(“BLR-DEL is cheaper.”);
}else
{
System.out.println(“BLR-LKO is cheaper.”);
}}

This is all you have to do in order to get the lower prices between the travel cost of two different cities keeping the departure city common for both. Run the program and you can see the result. Your final code will look like :

package home;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Demo1 {
public static void main(String[] args) throws InterruptedException
{
double price1=findPrice(“LKO”);
System.out.println(“Price From BLR-LKO is :” +price1);
double price2=findPrice(“DEL”);
System.out.println(“Price From BLR-DEL is :” +price2);

if(price1 > price2)
{
System.out.println(“BLR-DEL is cheaper.”);
}else
{
System.out.println(“BLR-LKO is cheaper.”);
}}
public static double findPrice(String dest) throws InterruptedException
{
WebDriver driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//To launch the Site
driver.get(“http://www.orbitz.com/”);
//Click on radio button
driver.findElement(By.xpath(“//*[@id=’products’]/div/fieldset/div[2]/label[1]/div”)).click();
//click on From city textbox and pass some value
driver.findElement(By.name(“ar.rt.leaveSlice.orig.key”)).sendKeys(“BLR”);
//click on To City text box and pass some Arrival city
driver.findElement(By.name(“ar.rt.leaveSlice.dest.key”)).sendKeys(dest);
//Leave date
driver.findElement(By.name(“ar.rt.leaveSlice.date”)).sendKeys(“03/20/2015”);
//Return Date
driver.findElement(By.name(“ar.rt.returnSlice.date”)).sendKeys(“03/27/2015”);
//Click on Search Flights Button
driver.findElement(By.name(“search”)).click();
Thread.sleep(1000);
//Select the lowest price from the list
driver.findElement(By.xpath(“//*[@id=’main’]/div[9]/div[2]/div[1]/div[1]/div/div[2]/a”)).click();
//Get the price
String price=driver.findElement(By.className(“mirrorCash”)).getText().replace(“$”, “”).replace(“,”,””);
System.out.println(price);
double dprice=Double.parseDouble(price);
return dprice;
}}

That’s all in comparing the prices of two travel costs using selenium web driver… !!!

Add Comment