I'm trying to test a travel website. When I enter the city code, the correct airport should be picked from the list. However, it enters the city code and does not proceed to pick up the city from the list.
@org.testng.annotations.Test
public void flightTest() throws InterruptedException {
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.get("https://phptravels.net/");
//--------------- Departure -------------------
WebElement fromBox = driver.findElement(By.name("from"));
fromBox.sendKeys("DAC");
wait = new WebDriverWait(driver, Duration.ofSeconds(10));
By lst1 = By.xpath("//div[@class='most--popular-from fadein results-container-from rounded-2 shadow border-0']//button[text()='DAC']");
Thread.sleep(1000);
WebElement from = wait.until(ExpectedConditions.visibilityOfElementLocated(lst1));
Thread.sleep(1000);
from.click();
//--------------- Destination -------------------
WebElement toBox = driver.findElement(By.name("to"));
toBox.sendKeys("JED");
wait = new WebDriverWait(driver, Duration.ofSeconds(10));
By lst2 = By.xpath("//div[@class='d-flex align-items-center p-2 to--insert overflow-hidden result-option']//button[text()='JED']");
WebElement to = wait.until(ExpectedConditions.visibilityOfElementLocated(lst2));
Thread.sleep(1000);
to.click();
}
It's not the issue in your code. It's just the way website behaves. i.e. if you enter(in this case selenium) the airport code(JED) too fast, system won't auto suggest just one matching airport, it suggests random but many airports, this is where your code fails. I don't know if this is a system bug, but that's not important to us.
You need to handle this. i.e. make selenium type the keys slowly as if a human is typing. I have written a method to enter the keys slowly. See the working code below:
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.get("https://phptravels.net/");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
//--------------- Departure -------------------
WebElement fromField = wait.until(ExpectedConditions.elementToBeClickable(By.name("from")));
typeSlowly(fromField,"DAC",500);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='DAC']"))).click();
//--------------- Destination -------------------
WebElement toField = wait.until(ExpectedConditions.elementToBeClickable(By.name("to")));
typeSlowly(toField,"JED",500);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='JED']"))).click();
//--------------- Departure Date -------------------
wait.until(ExpectedConditions.elementToBeClickable(By.id("departure"))).click();
}
public static void typeSlowly(WebElement element, String text, int delayMillis) {
for (char ch : text.toCharArray()) {
element.sendKeys(Character.toString(ch));
try {
Thread.sleep(delayMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}