diff --git a/src/main/java/FlightBookingTest.java b/src/main/java/FlightBookingTest.java index 19d98ddf..ecb311c7 100644 --- a/src/main/java/FlightBookingTest.java +++ b/src/main/java/FlightBookingTest.java @@ -4,58 +4,91 @@ import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.chrome.ChromeOptions; +import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.Select; +import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; +import org.testng.annotations.AfterTest; +import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; +import java.text.ParseException; import java.util.List; +import java.util.concurrent.TimeUnit; public class FlightBookingTest { - WebDriver driver = new ChromeDriver(); - - + WebDriver driver ; + FlightBookingTestElements fbte ; + WebDriverWait wait; + String srcCity = "Bangalore"; + String destCity = "Delhi"; + String day = "27"; + String month = "October"; + String year = "2020"; + + @BeforeTest + public void beforeTest() { + setDriverPath(); + ChromeOptions options = new ChromeOptions(); + options.addArguments("--disable-notifications"); + driver = new ChromeDriver(options); + fbte = new FlightBookingTestElements(driver); + wait = new WebDriverWait(driver, 10); + + } @Test - public void testThatResultsAppearForAOneWayJourney() { + public void testThatResultsAppearForAOneWayJourney() throws ParseException { - setDriverPath(); +// setDriverPath(); + driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); driver.get("https://www.cleartrip.com/"); - waitFor(2000); - driver.findElement(By.id("OneWay")).click(); + driver.manage().window().fullscreen(); +// waitFor(2000); + fbte.oneWayElement().click(); - driver.findElement(By.id("FromTag")).clear(); - driver.findElement(By.id("FromTag")).sendKeys("Bangalore"); + fbte.srcElement().clear(); + fbte.srcElement().sendKeys(srcCity); + wait.until(ExpectedConditions.presenceOfNestedElementLocatedBy(fbte.sourceOptions, fbte.liTag)); + - //wait for the auto complete options to appear for the origin - - waitFor(2000); - List originOptions = driver.findElement(By.id("ui-id-1")).findElements(By.tagName("li")); +// waitFor(5000); + List originOptions = fbte.srcOptions(); originOptions.get(0).click(); + + waitFor(1000); + + fbte.destElement().clear(); + fbte.destElement().sendKeys(destCity); - driver.findElement(By.id("toTag")).clear(); - driver.findElement(By.id("toTag")).sendKeys("Delhi"); - + wait.until(ExpectedConditions.presenceOfNestedElementLocatedBy(fbte.destOptions, fbte.liTag)); //wait for the auto complete options to appear for the destination - waitFor(2000); + //waitFor(5000); //select the first item from the destination auto complete list - List destinationOptions = driver.findElement(By.id("ui-id-2")).findElements(By.tagName("li")); + List destinationOptions = fbte.destOptions(); destinationOptions.get(0).click(); - driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/table/tbody/tr[3]/td[7]/a")).click(); - +// driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/table/tbody/tr[4]/td[7]/a")).click(); + fbte.selectDate(year, month, day); //all fields filled in. Now click on search - driver.findElement(By.id("SearchBtn")).click(); + System.out.println(driver.findElement(fbte.calendar).getText()); + if(fbte.searchElement().isEnabled() && fbte.srcElement().getText()!=null && fbte.destElement()!=null && driver.findElement(fbte.calendar).getText()!=null ) + fbte.searchElement().click(); - waitFor(5000); +// waitFor(5000); //verify that result appears for the provided journey search - Assert.assertTrue(isElementPresent(By.className("searchSummary"))); - - //close the browser - driver.quit(); - + + Assert.assertTrue(isElementPresent(fbte.seachSummary)); } + @AfterTest + public void afterTest() + { + driver.close(); + driver.quit(); + } private void waitFor(int durationInMilliSeconds) { try { @@ -68,7 +101,7 @@ private void waitFor(int durationInMilliSeconds) { private boolean isElementPresent(By by) { try { - driver.findElement(by); + wait.until(ExpectedConditions.presenceOfElementLocated(by)); return true; } catch (NoSuchElementException e) { return false; diff --git a/src/main/java/FlightBookingTestElements.java b/src/main/java/FlightBookingTestElements.java new file mode 100644 index 00000000..04d04ec9 --- /dev/null +++ b/src/main/java/FlightBookingTestElements.java @@ -0,0 +1,108 @@ +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.List; + +import org.openqa.selenium.By; +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; + +public class FlightBookingTestElements { + WebDriver driver; + By oneWay = By.id("OneWay"); + By source = By.id("FromTag"); + By destination = By.name("destination"); + By sourceOptions = By.id("ui-id-1"); + By destOptions = By.id("ui-id-2"); + By liTag = By.tagName("li"); + By searchBt = By.id("SearchBtn"); + By seachSummary = By.className("searchSummary"); + By calendar = By.xpath("//*[@id='ui-datepicker-div']"); + + public FlightBookingTestElements(WebDriver driver) { + this.driver = driver; + } + + public WebElement oneWayElement() + { + return driver.findElement(oneWay); + } + public WebElement srcElement() + { + return driver.findElement(source); + } + public WebElement destElement() + { + return driver.findElement(destination); + } + public List srcOptions() + { + return driver.findElement(sourceOptions).findElements(liTag); + } + public List destOptions() + { + return driver.findElement(destOptions).findElements(liTag); + } + public WebElement searchElement() + { + return driver.findElement(searchBt); + } + public WebElement searchSumElement() + { + return driver.findElement(seachSummary); + } + public void selectDate(String year, String monthName, String day) throws ParseException + { + driver.findElement(calendar).click(); + + String currentYear= driver.findElement(By.xpath("//div[@class='monthBlock first']//div[@class='title']/span[@class='ui-datepicker-year']")).getText(); +// System.out.println(currentYear); + if(!currentYear.equals(year)) + { + do{ + driver.findElement(By.xpath("//a[@class='nextMonth ']")).click(); + }while(!driver.findElement(By.xpath("//div[@class='monthBlock first']//div[@class='title']/span[@class='ui-datepicker-year']")).getText().equals(year)); + + } + + String currentMonth= driver.findElement(By.xpath("//div[@class='monthBlock first']//div[@class='title']/span[@class='ui-datepicker-month']")).getText(); +// System.out.println("mont;"+currentMonth); + if(!currentMonth.equalsIgnoreCase(monthName)) + { + do{ + driver.findElement(By.xpath("//a[@class='nextMonth ']")).click(); + }while(!driver.findElement(By.xpath("//div[@class='monthBlock first']//div[@class='title']/span[@class='ui-datepicker-month']")).getText().trim().equalsIgnoreCase(monthName)); + + } + + int javaMonthInt= getMonthJavaInt(monthName); + + + List allDateOfDesiredMonth= driver.findElements(By.xpath("//div[@class='monthBlock first']/table/tbody[1]//td[(contains(@class,'undefined') and @data-month='"+javaMonthInt+"')]")); + for(WebElement d:allDateOfDesiredMonth ) + { + + if(d.getText().trim().equals(day)) + { + System.out.println("date:"+d.getAttribute("data-month")); + System.out.println(d.findElement(By.className("ui-state-default")).getText()); + JavascriptExecutor executor = (JavascriptExecutor) driver; + executor.executeScript("arguments[0].click();", d); + break; + } + } + + } + + public int getMonthJavaInt(String monthName) throws ParseException + { + + Date date = new SimpleDateFormat("MMMM").parse(monthName); + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + return cal.get(Calendar.MONTH); + } + +} diff --git a/src/main/java/HotelBookingTest.java b/src/main/java/HotelBookingTest.java index 2be026bb..d3ddf927 100644 --- a/src/main/java/HotelBookingTest.java +++ b/src/main/java/HotelBookingTest.java @@ -1,15 +1,22 @@ import com.sun.javafx.PlatformUtil; + +import java.util.concurrent.TimeUnit; + import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; import org.openqa.selenium.support.ui.Select; +import org.testng.annotations.AfterTest; +import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class HotelBookingTest { - WebDriver driver = new ChromeDriver(); - + WebDriver driver ; + @FindBy(linkText = "Hotels") private WebElement hotelLink; @@ -21,12 +28,25 @@ public class HotelBookingTest { @FindBy(id = "travellersOnhome") private WebElement travellerSelection; + + @BeforeTest + public void beforeTest() { + setDriverPath(); + ChromeOptions options = new ChromeOptions(); + options.addArguments("--disable-notifications"); + driver = new ChromeDriver(options); + + PageFactory.initElements(driver, this); + } @Test public void shouldBeAbleToSearchForHotels() { - setDriverPath(); - + // setDriverPath(); + + driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS); driver.get("https://www.cleartrip.com/"); + driver.manage().window().fullscreen(); + hotelLink.click(); localityTextBox.sendKeys("Indiranagar, Bangalore"); @@ -34,10 +54,15 @@ public void shouldBeAbleToSearchForHotels() { new Select(travellerSelection).selectByVisibleText("1 room, 2 adults"); searchButton.click(); - driver.quit(); - } + @AfterTest + public void afterTest() + { + driver.close(); + driver.quit(); + } + private void setDriverPath() { if (PlatformUtil.isMac()) { System.setProperty("webdriver.chrome.driver", "chromedriver"); diff --git a/src/main/java/SignInTest.java b/src/main/java/SignInTest.java index 2c109950..d3beea71 100644 --- a/src/main/java/SignInTest.java +++ b/src/main/java/SignInTest.java @@ -1,30 +1,60 @@ -import com.sun.javafx.PlatformUtil; +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.chrome.ChromeOptions; import org.testng.Assert; +import org.testng.annotations.AfterTest; +import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; -public class SignInTest { +import com.sun.javafx.PlatformUtil; - WebDriver driver = new ChromeDriver(); +public class SignInTest { + WebDriver driver ; + SignInTestElements sine; + + @BeforeTest + public void beforeTest() { + setDriverPath(); + ChromeOptions options = new ChromeOptions(); + options.addArguments("--disable-notifications"); + driver = new ChromeDriver(options); + sine = new SignInTestElements(driver); + } @Test public void shouldThrowAnErrorIfSignInDetailsAreMissing() { - setDriverPath(); - driver.get("https://www.cleartrip.com/"); - waitFor(2000); - - driver.findElement(By.linkText("Your trips")).click(); - driver.findElement(By.id("SignIn")).click(); + driver.manage().window().fullscreen(); + driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS); + driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); + +// waitFor(2000); + + //driver.findElement(By.linkText("Your trips")).click(); + sine.yourTripsElement().click(); + //driver.findElement(By.id("SignIn")).click(); + sine.signInElement().click(); - driver.findElement(By.id("signInButton")).click(); - - String errors1 = driver.findElement(By.id("errors1")).getText(); + //driver.findElement(By.id("signInButton")).click(); + driver.switchTo().frame(sine.iFrameElement()); + sine.signInButtonElement().click(); + + String errors1 = sine.errorMsgElement().getText(); Assert.assertTrue(errors1.contains("There were errors in your submission")); - driver.quit(); + + driver.switchTo().parentFrame(); + } + + @AfterTest + public void afterTest() + { + driver.close(); + driver.quit(); } private void waitFor(int durationInMilliSeconds) { @@ -35,7 +65,8 @@ private void waitFor(int durationInMilliSeconds) { } } - private void setDriverPath() { + @SuppressWarnings("restriction") + private void setDriverPath() { if (PlatformUtil.isMac()) { System.setProperty("webdriver.chrome.driver", "chromedriver"); } diff --git a/src/main/java/SignInTestElements.java b/src/main/java/SignInTestElements.java new file mode 100644 index 00000000..d55617e5 --- /dev/null +++ b/src/main/java/SignInTestElements.java @@ -0,0 +1,38 @@ +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; + +public class SignInTestElements { + WebDriver driver; + By yourTrips = By.linkText("Your trips"); + By signIn = By.id("SignIn"); + By signInButton = By.id("signInButton"); + By errors = By.id("errors1"); + By iframe = By.name("modal_window"); + + public SignInTestElements(WebDriver driver) { + this.driver = driver; + } + + public WebElement yourTripsElement() + { + return driver.findElement(yourTrips); + } + + public WebElement signInElement() + { + return driver.findElement(signIn); + } + public WebElement signInButtonElement() + { + return driver.findElement(signInButton); + } + public WebElement errorMsgElement() + { + return driver.findElement(errors); + } + public WebElement iFrameElement() + { + return driver.findElement(iframe); + } +}