ReckA
ReckA

Reputation: 13

Selenium WebDriver automation of Flight Booking for MakeMyTrip.com, getting 'Element not found' error for selecting flight after search

Trying to automate the flight booking for MakeMyTrip.com using Selenium WebDriver. Have separate pom class and TestNG class. My code automates it correctly upto clicking on search button after selecting the required options of From, To, Date, etc... After clicking on search it opens the new page with result of available flights, but then it is unable to locate the button to select the flight. Tried using diff. locators, like id, xpath but not working. Need your help and a bit of your precious time. Thank you! And here's the code...

POM

package flightsModule;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class FlightPagePOM {
    
    WebDriver driver;
    JavascriptExecutor js = (JavascriptExecutor) driver;  
    
    @FindBy(xpath="//*[@id='SW']/div[1]/div[2]/div/div/nav/ul/li[1]/a")
    WebElement flightsTab;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[1]/ul/li[1]")
    WebElement flightType;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[1]/label")
    WebElement fromField;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[1]/div[1]/div/div/div/input")
    WebElement writeFromField;
    
    @FindBy(xpath="//*[text()='Nagpur, India']")
    WebElement selectFromSuggestion;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[2]/div[1]/div/div/div[1]/input")
    WebElement toField;
    
    @FindBy(xpath="//*[text()='Bengaluru, India']")
    WebElement selectToSuggestion;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[3]/div[1]/div/div/div/div[2]/div/div[2]/div[2]/div[3]/div[1]/div[4]")
    WebElement dDate;
    
    //@FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[3]/div[1]/div/div/div/div[2]/div/div[2]/div[2]/div[3]/div[1]/div[7]")
    //WebElement rDate;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[5]")
    WebElement optionBox;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[5]/div[2]/div[1]/ul[1]/li[1]")
    WebElement adults;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[5]/div[2]/div[1]/div/div[1]/ul/li[2]")
    WebElement children;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[5]/div[2]/div[1]/div/div[2]/ul/li[1]")
    WebElement infant;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[5]/div[2]/div[1]/ul[2]/li[3]")
    WebElement tClass;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[5]/div[2]/div[2]/button")
    WebElement apply;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[2]/div[1]/ul/li[1]/p")
    WebElement fare;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/p/a")
    WebElement searchBtn;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div[2]/div[2]/div/span")
    WebElement lockPrice;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div[2]/div/div[1]/div[2]/div[3]/div/label[1]/div/span[2]/span/span")
    WebElement oneStopD;
    
    //@FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div[2]/div/div[1]/div[2]/div[4]/div/div[1]/div/label[1]/div/span[1]/span")
    //WebElement oneStopR;

[![flight selection][1]][1] 

    @FindBy(xpath="//*[@id="bookbutton-RKEY:b204bbe6-210d-4197-a91e-1bad39c6ad9f:0_0"]")
    WebElement flight;
    
    @FindBy(id="bookbutton-RKEY:c3d9b30d-d2f7-42fb-a99c-2ddf401a2326:5_1")
    WebElement bookBtn;
    
    //////////////////////////////////////////////////////////////
    

    //constructor
    public FlightPagePOM(WebDriver driver) {
        this.driver=driver;
        PageFactory.initElements(driver,this);
    }
    
    //methods
    public void clickFlightTab() {
        flightsTab.click();
    }
    
    public void clickOnRoundTrip() {
        flightType.click();
    }
    
    public void cities (String from, String To) {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
        fromField.click();
        writeFromField.sendKeys(from);
        selectFromSuggestion.click();
        toField.sendKeys(To);
        selectToSuggestion.click();
    }

    
    public void dates() {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
        dDate.click();
        //rDate.click();
    }
    
    public void travellersAndClass () {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
        optionBox.click();
        adults.click();
        children.click();
        infant.click();
        tClass.click();
        apply.click();
    }
    
    public void search () {
        fare.click();
        searchBtn.click();  
    }
    
    public void flights () {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
        lockPrice.click();
        oneStopD.click();
        //oneStopR.click();
        flight.click();
        bookBtn.click();
    }
    
    public void test() {
        
    }
}

Test Class

package flightsModule;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import flightsModule.FlightPagePOM;

public class FlightPageTest {
    WebDriver driver;
    FlightPagePOM fp;

    @BeforeTest
    public void beforeTest() {
        System.setProperty("webdriver.chrome.driver","E:\\Program Files\\Selenium Software\\chromedriver_win32\\chromedriver.exe");
        driver=new ChromeDriver();
        driver.get("https://www.makemytrip.com/");
        driver.manage().window().maximize();
        fp=new FlightPagePOM(driver);
    }
    
    @Test(priority=1)
    public void flightsSearch() {
        fp.clickFlightTab();
    }
    
    @Test(priority=2)
    public void roundTrip() {
        fp.clickOnRoundTrip();
    }
    
    @Test(priority=3)
    public void cities() {
        fp.cities("Nagp", "Benga");
    }
    
    @Test(priority=4)
    public void dates() {
        fp.dates(); 
    }

    @Test(priority=5)
    public void travellersAndClass() {
        fp.travellersAndClass();
        }
    
    @Test(priority=6) 
    public void search() {
        fp.search();
        }
    
    @Test(priority=7) 
    public void selectFlight() {
        fp.flights();
        }
    
}

Upvotes: 0

Views: 7515

Answers (1)

Bharath
Bharath

Reputation: 1

the code shown here looks fine. But as you said the result is opening in a new webpage you will have to switch the browser 'window' on which the automation code is running. The program doesn't switch between tabs with click. Hope this helps.

Upvotes: 0

Related Questions