From a630e00321fe66ca387254094201fd2fffe5dd0e Mon Sep 17 00:00:00 2001 From: Jitendra2021 <35825478+Jitendra2021@users.noreply.github.com> Date: Mon, 8 Jul 2019 17:17:19 +0530 Subject: [PATCH 1/2] Create new class Page and moved re-useable functions in it. Other classes will extend Page class now --- .classpath | 28 +++++++++++++++ .gitignore | 3 +- .project | 23 ++++++++++++ .settings/org.eclipse.jdt.core.prefs | 14 ++++++++ src/main/java/FlightBookingTest.java | 35 ++++--------------- src/main/java/HotelBookingTest.java | 19 +++------- src/main/java/SignInTest.java | 29 ++++----------- src/main/java/codingRound/Utilities/Page.java | 28 +++++++++++++++ 8 files changed, 114 insertions(+), 65 deletions(-) create mode 100644 .classpath create mode 100644 .project create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 src/main/java/codingRound/Utilities/Page.java diff --git a/.classpath b/.classpath new file mode 100644 index 00000000..226c7f7a --- /dev/null +++ b/.classpath @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore index 0275ff56..98a6bc15 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ build .idea -.gradle \ No newline at end of file +.gradle +/target/ diff --git a/.project b/.project new file mode 100644 index 00000000..98fd637a --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + codingRound + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000..91ca62e2 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/src/main/java/FlightBookingTest.java b/src/main/java/FlightBookingTest.java index 19d98ddf..54d82a22 100644 --- a/src/main/java/FlightBookingTest.java +++ b/src/main/java/FlightBookingTest.java @@ -1,16 +1,16 @@ -import com.sun.javafx.PlatformUtil; +import codingRound.Utilities.Page; + 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 { +public class FlightBookingTest extends Page { WebDriver driver = new ChromeDriver(); @@ -18,9 +18,9 @@ public class FlightBookingTest { @Test public void testThatResultsAppearForAOneWayJourney() { - setDriverPath(); + Page.setDriverPath(); driver.get("https://www.cleartrip.com/"); - waitFor(2000); + Page.waitFor(2000); driver.findElement(By.id("OneWay")).click(); driver.findElement(By.id("FromTag")).clear(); @@ -47,7 +47,7 @@ public void testThatResultsAppearForAOneWayJourney() { //all fields filled in. Now click on search driver.findElement(By.id("SearchBtn")).click(); - waitFor(5000); + Page.waitFor(5000); //verify that result appears for the provided journey search Assert.assertTrue(isElementPresent(By.className("searchSummary"))); @@ -55,17 +55,7 @@ public void testThatResultsAppearForAOneWayJourney() { 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); @@ -75,15 +65,4 @@ private boolean isElementPresent(By by) { } } - 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 index 2be026bb..00a80e3b 100644 --- a/src/main/java/HotelBookingTest.java +++ b/src/main/java/HotelBookingTest.java @@ -1,4 +1,5 @@ -import com.sun.javafx.PlatformUtil; +import codingRound.Utilities.Page; + import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; @@ -6,7 +7,7 @@ import org.openqa.selenium.support.ui.Select; import org.testng.annotations.Test; -public class HotelBookingTest { +public class HotelBookingTest extends Page{ WebDriver driver = new ChromeDriver(); @@ -24,7 +25,7 @@ public class HotelBookingTest { @Test public void shouldBeAbleToSearchForHotels() { - setDriverPath(); + Page.setDriverPath(); driver.get("https://www.cleartrip.com/"); hotelLink.click(); @@ -38,16 +39,6 @@ public void shouldBeAbleToSearchForHotels() { } - 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 index 2c109950..765630d4 100644 --- a/src/main/java/SignInTest.java +++ b/src/main/java/SignInTest.java @@ -1,21 +1,22 @@ -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 { +import codingRound.Utilities.Page; + +public class SignInTest extends Page{ WebDriver driver = new ChromeDriver(); @Test public void shouldThrowAnErrorIfSignInDetailsAreMissing() { - setDriverPath(); + Page.setDriverPath(); driver.get("https://www.cleartrip.com/"); - waitFor(2000); + Page.waitFor(2000); driver.findElement(By.linkText("Your trips")).click(); driver.findElement(By.id("SignIn")).click(); @@ -27,25 +28,9 @@ public void shouldThrowAnErrorIfSignInDetailsAreMissing() { 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/main/java/codingRound/Utilities/Page.java b/src/main/java/codingRound/Utilities/Page.java new file mode 100644 index 00000000..4dd1f3d7 --- /dev/null +++ b/src/main/java/codingRound/Utilities/Page.java @@ -0,0 +1,28 @@ +package codingRound.Utilities; + +import com.sun.javafx.PlatformUtil; + +public class Page { + + + public static void waitFor(int durationInMilliSeconds) { + try { + Thread.sleep(durationInMilliSeconds); + } catch (InterruptedException e) { + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + } + + + protected static 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"); + } + } +} From 9c93cb9c421f05c66a34ab8c7cad3e4e9bd19994 Mon Sep 17 00:00:00 2001 From: Jitendra2021 <35825478+Jitendra2021@users.noreply.github.com> Date: Mon, 8 Jul 2019 22:26:47 +0530 Subject: [PATCH 2/2] Created Page class and updated Test class --- src/main/java/FlightBookingTest.java | 74 +++++-------------- src/main/java/HotelBookingTest.java | 46 ++++-------- src/main/java/SignInTest.java | 35 ++++----- src/main/java/codingRound/Utilities/Page.java | 32 +++++++- 4 files changed, 77 insertions(+), 110 deletions(-) diff --git a/src/main/java/FlightBookingTest.java b/src/main/java/FlightBookingTest.java index 54d82a22..94bde1f3 100644 --- a/src/main/java/FlightBookingTest.java +++ b/src/main/java/FlightBookingTest.java @@ -1,68 +1,34 @@ -import codingRound.Utilities.Page; - -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.testng.Assert; +import org.testng.annotations.AfterClass; import org.testng.annotations.Test; -import java.util.List; +import codingRound.Pages.FlightBooking_Page; +import codingRound.Utilities.Page; public class FlightBookingTest extends Page { - WebDriver driver = new ChromeDriver(); - - @Test - public void testThatResultsAppearForAOneWayJourney() { - - Page.setDriverPath(); - driver.get("https://www.cleartrip.com/"); - Page.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(); - + public void testThatResultsAppearForAOneWayJourney() { + + new Page().homePage(); + + new Page().waitFor(2000); + new FlightBooking_Page().selectJourneyType(); //all fields filled in. Now click on search - driver.findElement(By.id("SearchBtn")).click(); - - Page.waitFor(5000); + new FlightBooking_Page().selectOrigin(); + new FlightBooking_Page().selectDestination(); + new FlightBooking_Page().searchFlight(); + new Page().waitFor(5000); //verify that result appears for the provided journey search - Assert.assertTrue(isElementPresent(By.className("searchSummary"))); - - //close the browser - driver.quit(); + + new FlightBooking_Page().verifySummary(); } - private boolean isElementPresent(By by) { - try { - driver.findElement(by); - return true; - } catch (NoSuchElementException e) { - return false; - } + + @AfterClass + public void endTest() { + driver.quit(); } + } diff --git a/src/main/java/HotelBookingTest.java b/src/main/java/HotelBookingTest.java index 00a80e3b..743e9308 100644 --- a/src/main/java/HotelBookingTest.java +++ b/src/main/java/HotelBookingTest.java @@ -1,44 +1,24 @@ -import codingRound.Utilities.Page; - -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.AfterClass; import org.testng.annotations.Test; -public class HotelBookingTest extends Page{ - - WebDriver driver = new ChromeDriver(); - @FindBy(linkText = "Hotels") - private WebElement hotelLink; - - @FindBy(id = "Tags") - private WebElement localityTextBox; - - @FindBy(id = "SearchHotelsButton") - private WebElement searchButton; +import codingRound.Pages.HotelBooking_Page; +import codingRound.Utilities.Page; - @FindBy(id = "travellersOnhome") - private WebElement travellerSelection; +public class HotelBookingTest extends Page{ @Test public void shouldBeAbleToSearchForHotels() { - Page.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(); - + + new Page().homePage(); + new HotelBooking_Page().searchHotel(); + + } + + @AfterClass + public void endTest() { + driver.quit(); } - } diff --git a/src/main/java/SignInTest.java b/src/main/java/SignInTest.java index 765630d4..5d8685dd 100644 --- a/src/main/java/SignInTest.java +++ b/src/main/java/SignInTest.java @@ -1,34 +1,27 @@ -import org.openqa.selenium.By; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.chrome.ChromeDriver; -import org.testng.Assert; +import org.testng.annotations.AfterClass; import org.testng.annotations.Test; +import codingRound.Pages.SignIn_Page; import codingRound.Utilities.Page; public class SignInTest extends Page{ - WebDriver driver = new ChromeDriver(); - + @Test public void shouldThrowAnErrorIfSignInDetailsAreMissing() { - - Page.setDriverPath(); - - driver.get("https://www.cleartrip.com/"); - Page.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(); + new Page().homePage(); + + + new SignIn_Page().signIn(); + + new SignIn_Page().verifySubmission(); + } - + @AfterClass + public void endTest() { + driver.quit(); + } diff --git a/src/main/java/codingRound/Utilities/Page.java b/src/main/java/codingRound/Utilities/Page.java index 4dd1f3d7..852ef683 100644 --- a/src/main/java/codingRound/Utilities/Page.java +++ b/src/main/java/codingRound/Utilities/Page.java @@ -1,11 +1,25 @@ package codingRound.Utilities; +import org.openqa.selenium.By; +import org.openqa.selenium.NoSuchElementException; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.chrome.ChromeDriver; + import com.sun.javafx.PlatformUtil; public class Page { + public static WebDriver driver; + Page (WebDriver driver) { + this.driver = new ChromeDriver(); + setDriverPath(); + } - public static void waitFor(int durationInMilliSeconds) { + public Page() { + // TODO Auto-generated constructor stub + } + + public void waitFor(int durationInMilliSeconds) { try { Thread.sleep(durationInMilliSeconds); } catch (InterruptedException e) { @@ -14,7 +28,7 @@ public static void waitFor(int durationInMilliSeconds) { } - protected static void setDriverPath() { + public void setDriverPath() { if (PlatformUtil.isMac()) { System.setProperty("webdriver.chrome.driver", "chromedriver"); } @@ -25,4 +39,18 @@ protected static void setDriverPath() { System.setProperty("webdriver.chrome.driver", "chromedriver_linux"); } } + + protected boolean isElementPresent(By by) { + try { + driver.findElement(by); + return true; + } catch (NoSuchElementException e) { + return false; + } + } + + public void homePage() { + driver.get("https://www.cleartrip.com/"); + + } }