-3

I have to automate the Flight page for the below URL

URL: https://www.cheapoair.com/deals/business-class-airfares

Enter LAS in the 'Flying From ' Textbox and you will see the list getting displayed and have to select the First Airport code from the auto-populated list matching the Airport Code .

I have to automate using Selenium with java. Could you share the piece of code for it.

Flight image with Autopopulated origin List

Roy
  • 21
  • 1
  • 1
  • 8
  • 1
    Please read [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question). The expectation on SO is that the user asking a question not only does research to answer their own question but also shares that research, code attempts, and results. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! See also: [ask] – JeffC Oct 07 '17 at 22:27
  • I have already implemented mine code in the project . But it was not 100% efficient. I used to pick the origin or destination from Excel sheet and populate in sheet. Then once the list is autopopulated , I wait for the close button which is at bottom. Then I click on close button. This is process I used for similar scenario in my project. I have already lot of research and project is running for last 6 month – Roy Oct 08 '17 at 04:22
  • Great, then post the code that isn't working... it's not clear what your actual question is and you haven't met the basic guidelines for a question with the limited info you've provided. – JeffC Oct 08 '17 at 04:27
  • Please share your work – iamsankalp89 Oct 08 '17 at 06:53
  • @iamsankalp89 - URL: cheapoair.com/deals/business-class-airfares . if u eneter the origin as 'LAS', the list is autopopulated , but u cannot inspect element to select the xpath for the Autopopulated list. Currently Close icon appears in the bottom and i click on that and las get populated. But I have to select the Aiirport code from the List. – Roy Oct 08 '17 at 07:02

3 Answers3

1

Given this select:

<select name="airports" id="airport"> 
<option value="lax">Los Angeles</option >
<option value="sfo">San Francisco</option >
<option value="pdx">Portland</option >
</select>

...you can select by label text:

Select select = new Select(driver.findElement(By.name("airports"))); 
select.selectByVisibleText("Portland");

...or by value:

Select select = new Select(driver.findElement(By.name("airports"))); 
select.selectByValue("pdx");
SoCalLongboard
  • 11
  • 1
  • 1
  • 4
-1
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.expedia.co.in/Flights");
WebElement textbox=driver.findElement(By.xpath("//input[@id='flight-origin']"));
textbox.clear();
textbox.sendKeys("LAS");
Thread.sleep(4000);
List<WebElement> allOptions = driver.findElements(By.xpath("//div[@id='typeahead-list']"));
int count=allOptions.size();
System.out.println("No of autosuggestions"+count);
for(int i=0;i<count;i++)
{
String text=allOptions.get(i).getText();
System.out.println(text);
}
textbox.sendKeys(Keys.ARROW_DOWN);
textbox.sendKeys(Keys.ENTER);
}
}
Chan Sri
  • 7
  • 3
-2

Hi please find the solution to above problem

package com.daythree;

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 org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ExpediaProblem {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver", "F:\\workspace\\...\chromedriver.exe");
        WebDriver driver = new ChromeDriver(); 
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

        driver.navigate().to("https://www.expedia.co.in/Flights");
        driver.findElement(By.id("flight-origin")).click();
        // Thread.sleep(3000);
        driver.findElement(By.id("flight-origin")).sendKeys("las");

        // wait for the suggestions to appear

        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//*[@role='listbox']/div/li/a/div/div"))));

        List<WebElement> suggestions = driver.findElements(By.xpath("//*[@role='listbox']/div/li/a/div/div"));
        System.out.println("Size of the suggestions is : " + suggestions);

        String val = "Las Vegas, NV, US";
        for(int i=0;i<suggestions.size();i++){
            // this will print all the suggestions
            System.out.println("value is : " + suggestions.get(i).getText());

            // now logic to select the option
            if(suggestions.get(i).getText().contains(val)){
                suggestions.get(i).click();
                break;
            }
        }

    }

}
eduliant
  • 2,690
  • 4
  • 22
  • 36
  • You've got `Thread.sleep()` and implicit waits both of which are bad practices. You've also mixed implicit and explicit waits which is warned against in the official docs. – JeffC Oct 08 '17 at 02:35
  • @ jeffC i used Thread.sleep() just to make sure OS understands whats going in script line byline evne if u remove Thread.sleep() it is not going to hamper the way of execution,,also can u provide me official link where it says mixed implicit and explicit waits cannot be used – eduliant Oct 08 '17 at 06:05
  • @rajNishKuMar- URL URL: https://www.cheapoair.com/deals/business-class-airfares . if u eneter the origin as 'LAS', the list is autopopulated , but u cannot inspect element to select the xpath for the Autopopulated list. Currently Close icon appears in the bottom and i click on that and las get populated. But I have to select the Aiirport code from the List. – Roy Oct 08 '17 at 06:54
  • 1
    @rajNishKuMar https://stackoverflow.com/questions/29474296/clarification-on-the-cause-of-mixing-implicit-and-explicit-waits-of-selenium-doc – Grasshopper Oct 08 '17 at 09:12
  • public void FlightInput(){ System.setProperty("webdriver.chrome.driver","C:\\Workspace\\src\\test\\Resources\\com\\cucumber\\google\\Drivers\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.navigate().to("https://www.cheapoair.com/deals/business-class-airfares"); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.findElement(By.xpath("//a[@class='signupClose icon ic-cancel-fill']")).click(); driver.findElement(By.id("ember530")).clear(); – Roy Oct 08 '17 at 10:36
  • @rajNishKuMar http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-and-implicit-waits. `WARNING: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. ` – JeffC Oct 08 '17 at 14:04