diff --git a/src.main.java/common/CommonMethods.java b/src.main.java/common/CommonMethods.java new file mode 100644 index 00000000..2dc8ac2b --- /dev/null +++ b/src.main.java/common/CommonMethods.java @@ -0,0 +1,36 @@ +package common; + +import java.util.Properties; +import org.openqa.selenium.By; +import org.openqa.selenium.NoSuchElementException; +import org.openqa.selenium.WebDriver; +import utils.ReadProperty; + +public class CommonMethods { + WebDriver driver; + public static String pfileName = "testData.properties"; + public static Properties prop = new ReadProperty().readPropertyFile(pfileName); + + + public CommonMethods (WebDriver driver){ + this.driver = driver; + } + + public boolean isElementPresent(By by) { + try { + driver.findElement(by); + return true; + } catch (NoSuchElementException e) { + return false; + } + } + + public void waitFor(int durationInMilliSeconds) { + try { + Thread.sleep(durationInMilliSeconds); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + +} diff --git a/src.main.java/common/DriverFactory.java b/src.main.java/common/DriverFactory.java new file mode 100644 index 00000000..30bc283a --- /dev/null +++ b/src.main.java/common/DriverFactory.java @@ -0,0 +1,55 @@ +package common; + +import java.util.Properties; +import java.util.concurrent.TimeUnit; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.chrome.ChromeDriver; +import com.sun.javafx.PlatformUtil; + +import utils.ReadProperty; + + +public class DriverFactory { + + static String pfileName = "global.properties"; + static Properties prop = new ReadProperty().readPropertyFile(pfileName); + + static WebDriver driver; + static String url = prop.getProperty("url"); + static String browser = prop.getProperty("browser"); + static int timeOut = Integer.parseInt(prop.getProperty("timeOut")); + static int implecitWait = Integer.parseInt(prop.getProperty("implecitWait")); + + + public static WebDriver setUpDriver(){ + if (browser.equals("chrome") && PlatformUtil.isWindows() ){ + System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); + driver = new ChromeDriver(); + driver.get(url); + driver.manage().timeouts().implicitlyWait(implecitWait, TimeUnit.SECONDS); + //driver.manage().timeouts().setScriptTimeout(implecitWait, TimeUnit.SECONDS); + //driver.manage().timeouts().pageLoadTimeout(timeOut, TimeUnit.SECONDS); + driver.manage().window().maximize(); + } + + else if (browser.equals("chrome") && PlatformUtil.isMac() ){ + System.setProperty("webdriver.chrome.driver", "chromedriver"); + driver = new ChromeDriver(); + driver.get(url); + driver.manage().timeouts().implicitlyWait(implecitWait, TimeUnit.SECONDS);; + driver.manage().window().maximize(); + } + return driver; + } + + public static void shutDownDriver(){ + driver.close(); + driver.quit(); + } + public static WebDriver getDriver(){ + return driver; + } + + + +} diff --git a/src.main.java/resources/global.properties b/src.main.java/resources/global.properties new file mode 100644 index 00000000..963ff17a --- /dev/null +++ b/src.main.java/resources/global.properties @@ -0,0 +1,7 @@ +url = https://www.cleartrip.com/ +browser = chrome +implecitWait = 5 +timeOut = 30 + + + diff --git a/src.main.java/resources/testData.properties b/src.main.java/resources/testData.properties new file mode 100644 index 00000000..0743b7ae --- /dev/null +++ b/src.main.java/resources/testData.properties @@ -0,0 +1,2 @@ +userName = cleartrip +password = password \ No newline at end of file diff --git a/src.main.java/utils/ReadProperty.java b/src.main.java/utils/ReadProperty.java new file mode 100644 index 00000000..74258422 --- /dev/null +++ b/src.main.java/utils/ReadProperty.java @@ -0,0 +1,32 @@ +package utils; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + + public class ReadProperty { + + private Properties propertyLoad; + public Properties readPropertyFile(final String pFileName) { + InputStream fileSource = null; + try { + fileSource = this.getClass().getResourceAsStream("/resources/"+pFileName); + propertyLoad = new Properties(); + propertyLoad.load(fileSource); + } catch (IOException e) { + e.printStackTrace(); + } + finally { + if(fileSource!=null) { + try { + fileSource.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return propertyLoad; + } + +} + diff --git a/src/main/java/FlightBookingTest.java b/src/main/test/FlightBookingTest.java similarity index 100% rename from src/main/java/FlightBookingTest.java rename to src/main/test/FlightBookingTest.java diff --git a/src/main/java/HotelBookingTest.java b/src/main/test/HotelBookingTest.java similarity index 100% rename from src/main/java/HotelBookingTest.java rename to src/main/test/HotelBookingTest.java diff --git a/src/main/java/SignInTest.java b/src/main/test/SignInTest.java similarity index 100% rename from src/main/java/SignInTest.java rename to src/main/test/SignInTest.java diff --git a/src/main/test/pom/FlightBookingPage.java b/src/main/test/pom/FlightBookingPage.java new file mode 100644 index 00000000..2de10228 --- /dev/null +++ b/src/main/test/pom/FlightBookingPage.java @@ -0,0 +1,105 @@ +package pom; + +import java.util.List; + +import org.openqa.selenium.By; +import org.openqa.selenium.NoSuchElementException; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebDriverException; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.ui.Select; +import org.openqa.selenium.support.ui.WebDriverWait; +import common.CommonMethods; + +public class FlightBookingPage { + + WebDriver driver; + CommonMethods cm; + boolean flag; + + public FlightBookingPage(WebDriver driver) { + this.driver = driver; + cm = new CommonMethods(driver); + } + + By oneWay = By.id("OneWay"); + By fromTag = By.id("FromTag"); + By toTag = By.id("toTag"); + By originOption = By.id("ui-id-1"); + + + public void clickonFrom() { + driver.findElement((oneWay)).click(); + } + + public void typeStartingplace() { + driver.findElement(fromTag).sendKeys("Bangalore"); + } + + public void clickonTo() { + driver.findElement(By.id("toTag")).click(); + } + + public void typeEndDestination() { + driver.findElement(By.id("toTag")).sendKeys("Delhi"); + } + + public void searchButton() { + driver.findElement(By.id("SearchBtn")).click(); + } + + //public void dateSelection() { + //driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/table/tbody/tr[3]/td[7]/a")).click(); + //} + public void searchforFlights() { + try { + List originOptionsFrom = null; + List originOptionsTo = null; + + + driver.findElement(oneWay).click(); + driver.findElement(fromTag).click(); + driver.findElement(toTag).sendKeys("Bangalore"); + + //wait for the auto complete options to appear for the origin + cm.waitFor(2000); + originOptionsFrom = driver.findElement(originOption).findElements(By.tagName("li")); + for(WebElement ele : originOptionsFrom) { + if(ele.getText().trim().contains("Bangalore")) { + ele.click(); + + } + } + driver.findElement(By.id("toTag")).clear(); + driver.findElement(By.id("toTag")).sendKeys("Delhi"); + originOptionsFrom = driver.findElement(By.id("ui-id-2")).findElements(By.tagName("li")); + for(WebElement ele : originOptionsTo) { + if(ele.getText().trim().contains("Delhi")) { + ele.click(); + + } + } + } + + catch(Exception e) { + System.out.println("Flightbooking Failed"+e.getMessage()); + } + + } + public void clickonSearchButton() { + //dateSelection(); + searchButton(); + + + } + + public boolean isSummaryPresent() { + flag = false; + try { + flag = cm.isElementPresent(By.className("searchSummary")); + } catch (Exception wde) { + } + return flag; + } + + } \ No newline at end of file diff --git a/src/main/test/pom/HotelBookingPage.java b/src/main/test/pom/HotelBookingPage.java new file mode 100644 index 00000000..75811e66 --- /dev/null +++ b/src/main/test/pom/HotelBookingPage.java @@ -0,0 +1,43 @@ +package pom; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.ui.Select; +import org.testng.annotations.Test; + +import common.CommonMethods; + +public class HotelBookingPage { + + WebDriver driver; + CommonMethods cm; + boolean flag; + //WebDriverWait wait = new WebDriverWait(driver, 20000); + + public HotelBookingPage(WebDriver driver) { + this.driver = driver; + cm = new CommonMethods(driver); + } + + @FindBy(linkText = "Hotels") + private WebElement hotelLink; + + @FindBy(id = "Tags") + private WebElement localityTextBox; + + @FindBy(id = "SearchHotelsButton") + private WebElement searchButton; + + @FindBy(id = "travellersOnhome") + private WebElement travellerSelection; + + + public void hotelBooking() { + + hotelLink.click(); + localityTextBox.sendKeys("Indiranagar, Bangalore"); + new Select(travellerSelection).selectByVisibleText("1 room, 2 adults"); + searchButton.click(); +} +} diff --git a/src/main/test/pom/LoginPage.java b/src/main/test/pom/LoginPage.java new file mode 100644 index 00000000..ebe2fa6b --- /dev/null +++ b/src/main/test/pom/LoginPage.java @@ -0,0 +1,51 @@ +package pom; + +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.testng.Assert; + +import common.CommonMethods; + + + +public class LoginPage { + + WebDriver driver; + CommonMethods cm; + + public LoginPage(WebDriver driver){ + this.driver = driver; + cm = new CommonMethods(driver); + } + + + By your_Trip = By.linkText("Your trips"); + By btn_SignIn = By.id("signInButton"); + By sigin_aftertopic = By.id("SignIn"); + + public void clickYourtrip(){ + driver.findElement(your_Trip).click(); + } + + public void clicksignIn() { + driver.findElement(sigin_aftertopic).click(); + } + + public void clickSignInButton(){ + driver.findElement(btn_SignIn).click(); + } + + + public void loginerror() { + try { + clickYourtrip(); + clicksignIn(); + cm.waitFor(2000); + clickSignInButton(); + }catch(Exception e) { + System.out.println("Login Error verification fail "); + } + + } + +} diff --git a/src/main/test/test/FlightBookingTest.java b/src/main/test/test/FlightBookingTest.java new file mode 100644 index 00000000..a85e9507 --- /dev/null +++ b/src/main/test/test/FlightBookingTest.java @@ -0,0 +1,44 @@ +package test; +import common.CommonMethods; +import common.DriverFactory; +import pom.FlightBookingPage; +import org.junit.AfterClass; +import org.openqa.selenium.WebDriver; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import java.util.Properties; + +public class FlightBookingTest { + + + static WebDriver driver; + FlightBookingPage flightBookingPage; + + Properties prop = CommonMethods.prop; + String userName = prop.getProperty("userName1"); + String password = prop.getProperty("password1"); + + @BeforeClass + public void setUp() { + driver = DriverFactory.setUpDriver(); + flightBookingPage = new FlightBookingPage(driver); + } + + + @Test + public void test1() { + + flightBookingPage.searchforFlights(); + flightBookingPage.clickonSearchButton(); + Assert.assertTrue(flightBookingPage.isSummaryPresent()); + } + + @AfterClass + public void closeBrowser(){ + driver.quit(); + } + + +} diff --git a/src/main/test/test/HotelBookingTest.java b/src/main/test/test/HotelBookingTest.java new file mode 100644 index 00000000..ffe6e877 --- /dev/null +++ b/src/main/test/test/HotelBookingTest.java @@ -0,0 +1,47 @@ +package test; +import com.sun.javafx.PlatformUtil; + +import common.CommonMethods; +import common.DriverFactory; +import pom.FlightBookingPage; +import pom.HotelBookingPage; + +import java.util.Properties; + +import org.junit.AfterClass; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.ui.Select; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +public class HotelBookingTest { + + static WebDriver driver; + HotelBookingPage hotelBookingPage; + + Properties prop = CommonMethods.prop; + String userName = prop.getProperty("userName1"); + String password = prop.getProperty("password1"); + + @BeforeClass + public void setUp() { + driver = DriverFactory.setUpDriver(); + hotelBookingPage = new HotelBookingPage(driver); + } + + + @Test + public void shouldBeAbleToSearchForHotels() { + hotelBookingPage.hotelBooking(); + + } + + @AfterClass + public void closeBrowser(){ + driver.quit(); + } + +} diff --git a/src/main/test/test/SignInTest.java b/src/main/test/test/SignInTest.java new file mode 100644 index 00000000..81181b15 --- /dev/null +++ b/src/main/test/test/SignInTest.java @@ -0,0 +1,44 @@ +package test; +import com.sun.javafx.PlatformUtil; + +import common.CommonMethods; +import common.DriverFactory; +import pom.LoginPage; +import java.util.Properties; + +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +public class SignInTest { + + + static WebDriver driver; + LoginPage loginPage; + + Properties prop = CommonMethods.prop; + String userName = prop.getProperty("userName"); + String password = prop.getProperty("password"); + + @BeforeClass + public void setUp(){ + driver = DriverFactory.setUpDriver(); + loginPage = new LoginPage(driver); + } + + @Test(groups = "Login") + private void invalidlogin() throws Exception { + loginPage.loginerror(); + Assert.assertEquals(driver.findElement(By.id("errors1")).getText(),"There were errors in your submission"); + } + + + @AfterClass + public void tearDown(){ + DriverFactory.shutDownDriver(); + } + +}