From 04372a5fa29330982acab87c2914808062615ba9 Mon Sep 17 00:00:00 2001 From: shubhanshum <40490896+shubhanshum@users.noreply.github.com> Date: Tue, 6 Aug 2019 13:41:20 +0530 Subject: [PATCH] Update HotelBookingTest.java --- src/main/java/HotelBookingTest.java | 58 ++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/src/main/java/HotelBookingTest.java b/src/main/java/HotelBookingTest.java index 2be026bb..ddb49055 100644 --- a/src/main/java/HotelBookingTest.java +++ b/src/main/java/HotelBookingTest.java @@ -8,35 +8,67 @@ public class HotelBookingTest { - WebDriver driver = new ChromeDriver(); + static WebDriver driver = new ChromeDriver(); @FindBy(linkText = "Hotels") - private WebElement hotelLink; + private WebElement hotelLink; - @FindBy(id = "Tags") - private WebElement localityTextBox; + @FindBy(id = "Tags") + private WebElement localityTextBox; - @FindBy(id = "SearchHotelsButton") - private WebElement searchButton; + @FindBy(css = "ul#ui-id-1>li>a") + private List autosuggestionlocalityTextBox; - @FindBy(id = "travellersOnhome") - private WebElement travellerSelection; + @FindBy(id = "SearchHotelsButton") + private WebElement searchButton; + + @FindBy(id = "travellersOnhome") + private WebElement travellerSelection; @Test public void shouldBeAbleToSearchForHotels() { setDriverPath(); - driver.get("https://www.cleartrip.com/"); - hotelLink.click(); + // Maximizing window + driver.manage().window().maximize(); + + // Initialize the page objects. + PageFactory.initElements(driver, this); + + hotelLink.click(); - localityTextBox.sendKeys("Indiranagar, Bangalore"); + localityTextBox.sendKeys("Indiranagar, Bangalore"); - new Select(travellerSelection).selectByVisibleText("1 room, 2 adults"); - searchButton.click(); + // Create a method to select value from auto-suggested values populated by Where text box. + selectAutoSuggestedValue(autosuggestionlocalityTextBox, "Indiranagar, Bangalore"); + + new Select(travellerSelection).selectByVisibleText("1 room, 2 adults"); + searchButton.click(); driver.quit(); } + + private static void selectAutoSuggestedValue(List elements, String partialLinkName) { + try { + explicitWait_till_ElementsVisibility(elements); + int lnkCnt = elements.size(); + for (int i = 0; i < lnkCnt; i++) { + WebElement link = elements.get(i); + if (link.getText().contains(partialLinkName)) { + link.click(); + break; + } + } + } catch (Exception e) { + System.out.println("Exception:" + e); + } + } + + static void explicitWait_till_ElementsVisibility(List elements) { + WebDriverWait wait = new WebDriverWait(driver, 10); + wait.until(ExpectedConditions.visibilityOfAllElements(elements)); + } private void setDriverPath() { if (PlatformUtil.isMac()) {