diff --git a/build.gradle b/build.gradle index 00692901..24198a52 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,10 @@ repositories { } dependencies { - compile group: 'junit', name: 'junit', version: '4.11' compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.6.0' compile group: 'org.testng', name: 'testng', version: '6.11' } + +test { + useTestNG() +} \ No newline at end of file diff --git a/clearTrip.xml b/clearTrip.xml new file mode 100644 index 00000000..42fbf142 --- /dev/null +++ b/clearTrip.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/pom.xml b/pom.xml index 839ed21f..e2504e2a 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 4.0.0 com.testvagrant.codingRound - codoingRound + codingRound 1.0-SNAPSHOT diff --git a/settings.gradle b/settings.gradle index 7b80fd47..385d8323 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,2 @@ -rootProject.name = 'codoingRound' - +rootProject.name = 'codingRound' + diff --git a/src/main/java/FlightBookingTest.java b/src/main/java/FlightBookingTest.java deleted file mode 100644 index 19d98ddf..00000000 --- a/src/main/java/FlightBookingTest.java +++ /dev/null @@ -1,89 +0,0 @@ -import com.sun.javafx.PlatformUtil; -import org.openqa.selenium.By; -import org.openqa.selenium.NoSuchElementException; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.WebElement; -import org.openqa.selenium.chrome.ChromeDriver; -import org.openqa.selenium.support.ui.Select; -import org.testng.Assert; -import org.testng.annotations.Test; - -import java.util.List; - -public class FlightBookingTest { - - WebDriver driver = new ChromeDriver(); - - - @Test - public void testThatResultsAppearForAOneWayJourney() { - - setDriverPath(); - driver.get("https://www.cleartrip.com/"); - waitFor(2000); - driver.findElement(By.id("OneWay")).click(); - - driver.findElement(By.id("FromTag")).clear(); - driver.findElement(By.id("FromTag")).sendKeys("Bangalore"); - - //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")); - originOptions.get(0).click(); - - driver.findElement(By.id("toTag")).clear(); - driver.findElement(By.id("toTag")).sendKeys("Delhi"); - - //wait for the auto complete options to appear for the destination - - waitFor(2000); - //select the first item from the destination auto complete list - List destinationOptions = driver.findElement(By.id("ui-id-2")).findElements(By.tagName("li")); - destinationOptions.get(0).click(); - - driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/table/tbody/tr[3]/td[7]/a")).click(); - - //all fields filled in. Now click on search - driver.findElement(By.id("SearchBtn")).click(); - - waitFor(5000); - //verify that result appears for the provided journey search - Assert.assertTrue(isElementPresent(By.className("searchSummary"))); - - //close the browser - driver.quit(); - - } - - - private void waitFor(int durationInMilliSeconds) { - try { - Thread.sleep(durationInMilliSeconds); - } catch (InterruptedException e) { - e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. - } - } - - - private boolean isElementPresent(By by) { - try { - driver.findElement(by); - return true; - } catch (NoSuchElementException e) { - return false; - } - } - - private void setDriverPath() { - if (PlatformUtil.isMac()) { - System.setProperty("webdriver.chrome.driver", "chromedriver"); - } - if (PlatformUtil.isWindows()) { - System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); - } - if (PlatformUtil.isLinux()) { - System.setProperty("webdriver.chrome.driver", "chromedriver_linux"); - } - } -} diff --git a/src/main/java/HotelBookingTest.java b/src/main/java/HotelBookingTest.java deleted file mode 100644 index 2be026bb..00000000 --- a/src/main/java/HotelBookingTest.java +++ /dev/null @@ -1,53 +0,0 @@ -import com.sun.javafx.PlatformUtil; -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.Test; - -public class HotelBookingTest { - - WebDriver driver = new ChromeDriver(); - - @FindBy(linkText = "Hotels") - private WebElement hotelLink; - - @FindBy(id = "Tags") - private WebElement localityTextBox; - - @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(); - - localityTextBox.sendKeys("Indiranagar, Bangalore"); - - new Select(travellerSelection).selectByVisibleText("1 room, 2 adults"); - searchButton.click(); - - driver.quit(); - - } - - private void setDriverPath() { - if (PlatformUtil.isMac()) { - System.setProperty("webdriver.chrome.driver", "chromedriver"); - } - if (PlatformUtil.isWindows()) { - System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); - } - if (PlatformUtil.isLinux()) { - System.setProperty("webdriver.chrome.driver", "chromedriver_linux"); - } - } - -} diff --git a/src/main/java/SignInTest.java b/src/main/java/SignInTest.java deleted file mode 100644 index 2c109950..00000000 --- a/src/main/java/SignInTest.java +++ /dev/null @@ -1,51 +0,0 @@ -import com.sun.javafx.PlatformUtil; -import org.openqa.selenium.By; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.chrome.ChromeDriver; -import org.testng.Assert; -import org.testng.annotations.Test; - -public class SignInTest { - - WebDriver driver = new ChromeDriver(); - - @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.findElement(By.id("signInButton")).click(); - - String errors1 = driver.findElement(By.id("errors1")).getText(); - Assert.assertTrue(errors1.contains("There were errors in your submission")); - driver.quit(); - } - - private void waitFor(int durationInMilliSeconds) { - try { - Thread.sleep(durationInMilliSeconds); - } catch (InterruptedException e) { - e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. - } - } - - private void setDriverPath() { - if (PlatformUtil.isMac()) { - System.setProperty("webdriver.chrome.driver", "chromedriver"); - } - if (PlatformUtil.isWindows()) { - System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); - } - if (PlatformUtil.isLinux()) { - System.setProperty("webdriver.chrome.driver", "chromedriver_linux"); - } - } - - -} diff --git a/src/test/java/FlightBookingTest.java b/src/test/java/FlightBookingTest.java new file mode 100644 index 00000000..821825bf --- /dev/null +++ b/src/test/java/FlightBookingTest.java @@ -0,0 +1,33 @@ +import org.openqa.selenium.By; +import org.openqa.selenium.NoSuchElementException; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.support.ui.Select; +import org.testng.Assert; +import org.testng.annotations.Test; + +import pages.FlightBookingPage; +import supportlibraries.TestBase; + +public class FlightBookingTest extends TestBase { + + @Test + public void testThatResultsAppearForAOneWayJourney() { + System.out.println("Started to Validate Filght one way trip form"); + FlightBookingPage flightBookPage = new FlightBookingPage(scriptHelper); + + flightBookPage.clickOneWayTripOption(); + flightBookPage.fillFromInput(); + flightBookPage.selectFromPlaceDropDownValue(); + flightBookPage.fillToInput(); + flightBookPage.selectToPlaceDropDownValue(); + flightBookPage.selectDepartOnDate(); + flightBookPage.searchForOneWayFilght(); + // Wait for loading the Summary Page + waitForPageLoad(1000); + // verify that result appears for the provided journey search + Assert.assertTrue(flightBookPage.verifySummaryPageIsDisplayed(), "Filght Results Summary Section is displayed"); + + } +} diff --git a/src/test/java/HotelBookingTest.java b/src/test/java/HotelBookingTest.java new file mode 100644 index 00000000..f9458fa9 --- /dev/null +++ b/src/test/java/HotelBookingTest.java @@ -0,0 +1,32 @@ +import supportlibraries.ScriptHelper; +import supportlibraries.TestBase; +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.Assert; +import org.testng.annotations.Test; + +import pages.HotelBookingPage; + +public class HotelBookingTest extends TestBase { + + @Test + public void shouldBeAbleToSearchForHotels() { + + HotelBookingPage hotelBookingPage = new HotelBookingPage(scriptHelper); + + hotelBookingPage.selectHotelLeftNav(); + hotelBookingPage.searchForLocality(); + // Selecting value from Suggestion drop down + hotelBookingPage.selectLocationSuggestion(); + hotelBookingPage.selectFromAndToDateFromPicker(); + hotelBookingPage.selectTravellerSection(); + // Select Search Button + hotelBookingPage.clickSearchButton(); + + Assert.assertTrue(hotelBookingPage.verifyHotelResultsPageIsDisplayed(), "Hotel Summary Section is Displayed"); + } + +} diff --git a/src/test/java/SignInTest.java b/src/test/java/SignInTest.java new file mode 100644 index 00000000..ef33a604 --- /dev/null +++ b/src/test/java/SignInTest.java @@ -0,0 +1,25 @@ +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.chrome.ChromeDriver; +import org.testng.Assert; +import org.testng.annotations.Test; + +public class SignInTest { + + WebDriver driver = new ChromeDriver(); + + @Test + public void shouldThrowAnErrorIfSignInDetailsAreMissing() { + + driver.findElement(By.linkText("Your trips")).click(); + driver.findElement(By.id("SignIn")).click(); + + driver.findElement(By.id("signInButton")).click(); + + String errors1 = driver.findElement(By.id("errors1")).getText(); + Assert.assertTrue(errors1.contains("There were errors in your submission")); + driver.quit(); + } + + +} diff --git a/src/test/java/pages/FlightBookingPage.java b/src/test/java/pages/FlightBookingPage.java new file mode 100644 index 00000000..3d30bf6c --- /dev/null +++ b/src/test/java/pages/FlightBookingPage.java @@ -0,0 +1,84 @@ +package pages; + +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; + +import supportlibraries.ScriptHelper; + +public class FlightBookingPage extends PageBase { + + @FindBy(id = "OneWay") + WebElement check_OneWayTrip; + + @FindBy(css = "[id='FromTag']") + WebElement input_FromTag; + + @FindBy(id = "ToTag") + WebElement input_ToTag; + + @FindBy(xpath = "//*[@id='ui-id-1']") + WebElement dropDown_FromLabel; + + @FindBy(xpath = "//*[@id='ui-id-2']") + WebElement dropDown_ToLabel; + + @FindBy(xpath = "//*[@id='ui-datepicker-div']//div/table/tbody/tr[3]/td[7]/a") + WebElement link_PickFromDate; + + @FindBy(id = "SearchBtn") + WebElement button_SearchFlight; + + @FindBy(className = "searchSummary") + WebElement div_SearchSummary; + + public String fromPlace = "Bangalore"; + public String toPlace = "Delhi"; + + public FlightBookingPage(ScriptHelper scriptHelper) { + super(scriptHelper); + PageFactory.initElements(driver, this); + } + + public void clickOneWayTripOption() { + waitForElement(check_OneWayTrip, 1000); + click(check_OneWayTrip); + } + + public void fillFromInput() { + click(input_FromTag); + input_FromTag.sendKeys(fromPlace); + } + + public void fillToInput() { + click(input_ToTag); + input_ToTag.sendKeys(toPlace); + } + + public void selectFromPlaceDropDownValue() { + //wait for the auto complete options to appear for the origin + waitForElement(dropDown_FromLabel, 30); + selectValueFromOptionsList(dropDown_FromLabel, fromPlace); + } + + public void selectToPlaceDropDownValue() { + //wait for the auto complete options to appear for the origin + waitForElement(dropDown_ToLabel, 30); + selectValueFromOptionsList(dropDown_ToLabel, toPlace); + } + + public void selectDepartOnDate() { + click(link_PickFromDate); + } + + public void searchForOneWayFilght() { + click(button_SearchFlight); + } + + public boolean verifySummaryPageIsDisplayed() { + waitForElement(div_SearchSummary, 2000); + + return isElementPresent(div_SearchSummary); + } + +} diff --git a/src/test/java/pages/HotelBookingPage.java b/src/test/java/pages/HotelBookingPage.java new file mode 100644 index 00000000..8abf9b75 --- /dev/null +++ b/src/test/java/pages/HotelBookingPage.java @@ -0,0 +1,77 @@ +package pages; + +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; + +import supportlibraries.ScriptHelper; + +public class HotelBookingPage extends PageBase { + + @FindBy(linkText = "Hotels") + private WebElement link_HotelLink; + + @FindBy(id = "Tags") + private WebElement input_LocalityTextBox; + + @FindBy(id = "SearchHotelsButton") + private WebElement button_SearchButton; + + @FindBy(id = "travellersOnhome") + private WebElement select_TravellerSelection; + + @FindBy(xpath = "//*[@id='ui-id-1']") + WebElement dropDown_LocationSuggestion; + + @FindBy(xpath = "//*[@id='ui-datepicker-div']//div/table/tbody/tr[3]/td[7]/a") + WebElement link_PickDate; + + @FindBy(className = "searchSummary") + WebElement div_SearchSummary; + + public String locality = "Indiranagar, Bangalore"; + + public HotelBookingPage(ScriptHelper scriptHelper) { + super(scriptHelper); + PageFactory.initElements(driver, this); + } + + public void selectHotelLeftNav() { + System.out.println("Click Hotel Left Nav"); + click(link_HotelLink); + } + + public void searchForLocality() { + waitForElement(input_LocalityTextBox, 2000); + input_LocalityTextBox.clear(); + input_LocalityTextBox.sendKeys(locality); + } + + public void selectTravellerSection() { + String travellersValue = "1 room, 2 adults"; + selectByVisibleText(select_TravellerSelection, travellersValue); + System.out.println(travellersValue+" value is selected"); + } + + public void selectLocationSuggestion() { + waitForElement(dropDown_LocationSuggestion, 10); + selectValueFromOptionsList(dropDown_LocationSuggestion, locality); + } + + public void selectFromAndToDateFromPicker() { + click(link_PickDate); + System.out.println("Select Checkout date"); + click(link_PickDate); + } + + public void clickSearchButton() { + click(button_SearchButton); + } + + public boolean verifyHotelResultsPageIsDisplayed() { + waitForElement(div_SearchSummary, 20); + + return isElementPresent(div_SearchSummary); + } + +} diff --git a/src/test/java/pages/PageBase.java b/src/test/java/pages/PageBase.java new file mode 100644 index 00000000..7ff83bac --- /dev/null +++ b/src/test/java/pages/PageBase.java @@ -0,0 +1,64 @@ +package pages; + +import java.util.List; + +import org.openqa.selenium.By; +import org.openqa.selenium.NoSuchElementException; +import org.openqa.selenium.WebElement; +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 supportlibraries.ReusableLibrary; +import supportlibraries.ScriptHelper; + +public class PageBase extends ReusableLibrary { + + + public Select select; + + public PageBase(ScriptHelper scriptHelper) { + super(scriptHelper); + } + + public void click(WebElement element) { + if (isElementPresent(element)) + element.click(); + } + + public boolean isElementPresent(WebElement element) { + WebDriverWait wait = new WebDriverWait(driver, 30); + WebElement ele = wait.until(ExpectedConditions.visibilityOf(element)); + if (ele != null) + return true; + return false; + } + + public WebElement waitForElement(WebElement ele, int timeout) { + WebDriverWait wait = new WebDriverWait(driver, timeout); + return wait.until(ExpectedConditions.visibilityOf(ele)); + } + + public void selectValueFromOptionsList(WebElement ele, String value) { + List options = ele.findElements(By.tagName("li")); + boolean found = false; + + for (WebElement option : options) { + if (option.getText().toLowerCase().contains(value.toLowerCase())) { + click(option); + found = true; + break; + } + } + + if (!found) { + Assert.fail(value +" is not available"); + } + } + + public void selectByVisibleText(WebElement ele, String value) { + new Select(ele).selectByVisibleText(value); + } + +} diff --git a/src/test/java/supportlibraries/ReusableLibrary.java b/src/test/java/supportlibraries/ReusableLibrary.java new file mode 100644 index 00000000..81770c71 --- /dev/null +++ b/src/test/java/supportlibraries/ReusableLibrary.java @@ -0,0 +1,17 @@ +package supportlibraries; + +import org.openqa.selenium.WebDriver; + +public abstract class ReusableLibrary { + + protected WebDriver driver; + + protected ScriptHelper scriptHelper; + + public ReusableLibrary(ScriptHelper scriptHelper) { + this.scriptHelper = scriptHelper; + this.driver = scriptHelper.getDriver(); + + } + +} diff --git a/src/test/java/supportlibraries/ScriptHelper.java b/src/test/java/supportlibraries/ScriptHelper.java new file mode 100644 index 00000000..e4a151c4 --- /dev/null +++ b/src/test/java/supportlibraries/ScriptHelper.java @@ -0,0 +1,16 @@ +package supportlibraries; + +import org.openqa.selenium.WebDriver; + +public class ScriptHelper { + private final WebDriver driver; + + public ScriptHelper(WebDriver driver) { + this.driver = driver; + } + + public WebDriver getDriver() { + return driver; + } + +} diff --git a/src/test/java/supportlibraries/TestBase.java b/src/test/java/supportlibraries/TestBase.java new file mode 100644 index 00000000..e4c9a844 --- /dev/null +++ b/src/test/java/supportlibraries/TestBase.java @@ -0,0 +1,87 @@ +package supportlibraries; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Properties; +import java.util.concurrent.TimeUnit; + +import org.openqa.selenium.WebDriver; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; + +public abstract class TestBase { + + protected WebDriver driver; + + protected Properties properties; + + protected ScriptHelper scriptHelper; + + protected static String globalSettingsPath = "settings.gradle"; + + // initialize the driver + @BeforeClass(alwaysRun = true) + public void testClassSetup() { + System.out.println("before class"); + // properties = getPropInstance(); + initializeDriver(); + initializeTestScript(); + } + + // launch browser and wait until page load. + @BeforeMethod(alwaysRun = true) + public void testMethodSetup() { + if (driver == null) + initializeDriver(); + launchURl(); + waitForPageLoad(1000); + } + + private void initializeDriver() { + driver = WebDriverFactory.getDriver(); + driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); + driver.manage().window().maximize(); + } + + private void initializeTestScript() { + this.scriptHelper = new ScriptHelper(driver); + } + + public void launchURl() { + // properties = getPropInstance(); + String uriStr = "https://www.cleartrip.com/"; + driver.get(uriStr); + } + + public void waitForPageLoad(int durationInMilliSeconds) { + try { + Thread.sleep(durationInMilliSeconds); + } catch (InterruptedException e) { + e.printStackTrace(); // To change body of catch statement use File | Settings | File Templates. + } + } + + public Properties getPropInstance() { + + Properties properties = new Properties(); + try { + properties.load(new FileInputStream(globalSettingsPath)); + + } catch (FileNotFoundException e) { + e.printStackTrace(); + System.out.println("FileNotFoundException while loading the Settings file"); + } catch (IOException e) { + System.out.println("IOException while loading the Global Settings file"); + } + + return properties; + } + + @AfterClass(alwaysRun = true) + public void testClassTearDown() { + driver.quit(); + } + +} diff --git a/src/test/java/supportlibraries/WebDriverFactory.java b/src/test/java/supportlibraries/WebDriverFactory.java new file mode 100644 index 00000000..9cbbcfb5 --- /dev/null +++ b/src/test/java/supportlibraries/WebDriverFactory.java @@ -0,0 +1,76 @@ +package supportlibraries; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Arrays; +import java.util.Properties; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.remote.DesiredCapabilities; + +public class WebDriverFactory { + + private static Properties properties; + private static String driverPath = System.getProperty("user.dir") + + "\\src\\test\\resources\\"; + private static String globalSettingsPath = "settings.gradle"; + + + public static WebDriver getDriver() { + WebDriver driver = null; + + // properties = getOSInstance(); + // String osName = System.getProperty("os.name").toLowerCase(); + // set default platform as windows + String osName = "windows"; + + String folderName = new String(); + + DesiredCapabilities chromeCapabilities = DesiredCapabilities + .chrome(); + + if (osName.contains("windows")) { + System.setProperty("webdriver.chrome.driver", driverPath + + "chromedriver.exe"); + } + if (osName.contains("mac")) { + chromeCapabilities.setCapability("chrome.switches", + Arrays.asList("--verbose")); + System.setProperty("webdriver.chrome.driver", driverPath + + "chromedriver"); + } + if (osName.contains("linux")) { + System.setProperty("webdriver.chrome.driver", driverPath + + "chromedriver_linux"); + } + + + driver = new ChromeDriver(chromeCapabilities); + + return driver; + } + + + + public static Properties getOSInstance() { + + Properties properties = new Properties(); + try { + properties.load(new FileInputStream(globalSettingsPath)); + + } catch (FileNotFoundException e) { + e.printStackTrace(); + System.out.println("FileNotFoundException while loading the Settings file"); + } catch (IOException e) { + System.out.println("IOException while loading the Global Settings file"); + } + + return properties; + + } + + + +} diff --git a/chromedriver b/src/test/resources/chromedriver old mode 100755 new mode 100644 similarity index 100% rename from chromedriver rename to src/test/resources/chromedriver diff --git a/chromedriver.exe b/src/test/resources/chromedriver.exe old mode 100755 new mode 100644 similarity index 53% rename from chromedriver.exe rename to src/test/resources/chromedriver.exe index 4bfff1ef..0eedbe0f Binary files a/chromedriver.exe and b/src/test/resources/chromedriver.exe differ diff --git a/chromedriver_linux b/src/test/resources/chromedriver_linux old mode 100755 new mode 100644 similarity index 100% rename from chromedriver_linux rename to src/test/resources/chromedriver_linux