From 2d37d4ce87d6c279d6a5ec0f662efa04df82161f Mon Sep 17 00:00:00 2001 From: Jonathan Cook Date: Mon, 29 Jun 2020 12:05:57 +0200 Subject: [PATCH] BAEL-4198 - Fix Selenium Live Tests --- testing-modules/selenium-junit-testng/pom.xml | 37 +++++-------- .../baeldung/selenium/SeleniumExample.java | 53 +++++++++++-------- .../selenium/config/SeleniumConfig.java | 22 ++++---- .../selenium/models/BaeldungAbout.java | 6 +-- .../selenium/pages/BaeldungAboutPage.java | 2 +- .../selenium/pages/BaeldungHomePage.java | 6 +-- .../selenium/pages/StartHerePage.java | 4 +- ...a => SeleniumJavaScriptClickLiveTest.java} | 22 ++++---- .../SeleniumCookiesJUnitLiveTest.java | 32 ++++++++--- .../junit/SeleniumWithJUnitLiveTest.java | 25 +++++---- .../SeleniumPageObjectLiveTest.java | 10 ++-- .../testng/SeleniumWithTestNGLiveTest.java | 13 +++-- 12 files changed, 129 insertions(+), 103 deletions(-) rename testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/clickusingjavascript/{SeleniumJavaScriptClickTest.java => SeleniumJavaScriptClickLiveTest.java} (74%) rename testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/{junit => cookies}/SeleniumCookiesJUnitLiveTest.java (80%) rename testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/{junit => pages}/SeleniumPageObjectLiveTest.java (79%) diff --git a/testing-modules/selenium-junit-testng/pom.xml b/testing-modules/selenium-junit-testng/pom.xml index 3734bf72c9..44af047bdd 100644 --- a/testing-modules/selenium-junit-testng/pom.xml +++ b/testing-modules/selenium-junit-testng/pom.xml @@ -1,6 +1,7 @@ - + 4.0.0 selenium-junit-testng 0.0.1-SNAPSHOT @@ -43,32 +44,22 @@ - src + + + src/main/resources + true + + + src/test/resources + true + + - - - live - - - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - - **/*LiveTest.java - - - - - - - - 6.10 3.4.0 + 1.5.4 diff --git a/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/SeleniumExample.java b/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/SeleniumExample.java index c5ad5182ff..b32e51d250 100644 --- a/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/SeleniumExample.java +++ b/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/SeleniumExample.java @@ -1,14 +1,12 @@ -package main.java.com.baeldung.selenium; - -import main.java.com.baeldung.selenium.config.SeleniumConfig; -import org.openqa.selenium.By; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.WebElement; -import org.openqa.selenium.firefox.FirefoxDriver; -import org.openqa.selenium.interactions.Actions; +package com.baeldung.selenium; import java.util.List; -import java.util.concurrent.TimeUnit; + +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.interactions.Actions; + +import com.baeldung.selenium.config.SeleniumConfig; public class SeleniumExample { @@ -17,15 +15,18 @@ public class SeleniumExample { public SeleniumExample() { config = new SeleniumConfig(); - config.getDriver().get(url); + config.getDriver() + .get(url); } public void closeWindow() { - this.config.getDriver().close(); + this.config.getDriver() + .close(); } public String getTitle() { - return this.config.getDriver().getTitle(); + return this.config.getDriver() + .getTitle(); } public void getAboutBaeldungPage() { @@ -35,29 +36,35 @@ public class SeleniumExample { } private void closeOverlay() { - List webElementList = this.config.getDriver().findElements(By.tagName("a")); + List webElementList = this.config.getDriver() + .findElements(By.tagName("a")); if (webElementList != null) { webElementList.stream() - .filter(webElement -> "Close".equalsIgnoreCase(webElement.getAttribute("title"))) - .filter(WebElement::isDisplayed) - .findAny() - .ifPresent(WebElement::click); + .filter(webElement -> "Close".equalsIgnoreCase(webElement.getAttribute("title"))) + .filter(WebElement::isDisplayed) + .findAny() + .ifPresent(WebElement::click); } } private void clickAboutLink() { - this.config.getDriver().findElement(By.partialLinkText("About")).click(); + Actions actions = new Actions(config.getDriver()); + WebElement aboutElement = this.config.getDriver() + .findElement(By.id("menu-item-6138")); + + actions.moveToElement(aboutElement).perform(); } private void clickAboutUsLink() { - Actions builder = new Actions(config.getDriver()); - WebElement element = this.config.getDriver().findElement(By.partialLinkText("About Baeldung.")); - builder.moveToElement(element).build().perform(); + WebElement element = this.config.getDriver() + .findElement(By.partialLinkText("About Baeldung.")); + element.click(); } public boolean isAuthorInformationAvailable() { return this.config.getDriver() - .findElement(By.cssSelector("article > .row > div")) - .isDisplayed(); + .getPageSource() + .contains("Hey ! I'm Eugen"); } + } diff --git a/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/config/SeleniumConfig.java b/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/config/SeleniumConfig.java index c84283b76b..b1b7754648 100644 --- a/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/config/SeleniumConfig.java +++ b/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/config/SeleniumConfig.java @@ -1,17 +1,14 @@ -package main.java.com.baeldung.selenium.config; +package com.baeldung.selenium.config; + +import java.io.File; +import java.util.concurrent.TimeUnit; import org.openqa.selenium.Capabilities; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; -import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.remote.DesiredCapabilities; -import java.io.File; -import java.util.concurrent.TimeUnit; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - public class SeleniumConfig { private WebDriver driver; @@ -19,15 +16,17 @@ public class SeleniumConfig { public SeleniumConfig() { Capabilities capabilities = DesiredCapabilities.firefox(); driver = new FirefoxDriver(capabilities); - driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); + driver.manage() + .timeouts() + .implicitlyWait(5, TimeUnit.SECONDS); } static { System.setProperty("webdriver.gecko.driver", findFile("geckodriver.mac")); } - static private String findFile(String filename) { - String paths[] = {"", "bin/", "target/classes"}; // if you have chromedriver somewhere else on the path, then put it here. + private static String findFile(String filename) { + String[] paths = { "", "bin/", "target/classes" }; // if you have chromedriver somewhere else on the path, then put it here. for (String path : paths) { if (new File(path + filename).exists()) return path + filename; @@ -40,7 +39,8 @@ public class SeleniumConfig { } public void navigateTo(String url) { - driver.navigate().to(url); + driver.navigate() + .to(url); } public void clickElement(WebElement element) { diff --git a/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/models/BaeldungAbout.java b/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/models/BaeldungAbout.java index 838beb5326..580cbd1f2b 100644 --- a/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/models/BaeldungAbout.java +++ b/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/models/BaeldungAbout.java @@ -1,7 +1,7 @@ -package main.java.com.baeldung.selenium.models; +package com.baeldung.selenium.models; -import main.java.com.baeldung.selenium.config.SeleniumConfig; -import main.java.com.baeldung.selenium.pages.BaeldungAboutPage; +import com.baeldung.selenium.config.SeleniumConfig; +import com.baeldung.selenium.pages.BaeldungAboutPage; import org.openqa.selenium.support.PageFactory; public class BaeldungAbout { diff --git a/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/pages/BaeldungAboutPage.java b/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/pages/BaeldungAboutPage.java index d33cb76398..064462fc10 100644 --- a/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/pages/BaeldungAboutPage.java +++ b/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/pages/BaeldungAboutPage.java @@ -1,4 +1,4 @@ -package main.java.com.baeldung.selenium.pages; +package com.baeldung.selenium.pages; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; diff --git a/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/pages/BaeldungHomePage.java b/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/pages/BaeldungHomePage.java index 55a8044375..d901bf3c34 100644 --- a/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/pages/BaeldungHomePage.java +++ b/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/pages/BaeldungHomePage.java @@ -1,6 +1,6 @@ -package main.java.com.baeldung.selenium.pages; +package com.baeldung.selenium.pages; -import main.java.com.baeldung.selenium.config.SeleniumConfig; +import com.baeldung.selenium.config.SeleniumConfig; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; @@ -8,7 +8,7 @@ import org.openqa.selenium.support.PageFactory; public class BaeldungHomePage { private SeleniumConfig config; - @FindBy(css=".header--menu > a") + @FindBy(css = ".nav--logo_mobile") private WebElement title; @FindBy(css = ".menu-start-here > a") private WebElement startHere; diff --git a/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/pages/StartHerePage.java b/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/pages/StartHerePage.java index 4f0ee9edcd..ccc166c351 100644 --- a/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/pages/StartHerePage.java +++ b/testing-modules/selenium-junit-testng/src/main/java/com/baeldung/selenium/pages/StartHerePage.java @@ -1,6 +1,6 @@ -package main.java.com.baeldung.selenium.pages; +package com.baeldung.selenium.pages; -import main.java.com.baeldung.selenium.config.SeleniumConfig; +import com.baeldung.selenium.config.SeleniumConfig; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; diff --git a/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/clickusingjavascript/SeleniumJavaScriptClickTest.java b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/clickusingjavascript/SeleniumJavaScriptClickLiveTest.java similarity index 74% rename from testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/clickusingjavascript/SeleniumJavaScriptClickTest.java rename to testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/clickusingjavascript/SeleniumJavaScriptClickLiveTest.java index 6d2ab8ef1f..de519a44fc 100644 --- a/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/clickusingjavascript/SeleniumJavaScriptClickTest.java +++ b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/clickusingjavascript/SeleniumJavaScriptClickLiveTest.java @@ -1,4 +1,4 @@ -package java.com.baeldung.selenium.clickusingjavascript; +package com.baeldung.selenium.clickusingjavascript; import org.junit.After; import org.junit.Before; @@ -14,16 +14,18 @@ import org.openqa.selenium.support.ui.WebDriverWait; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -public class SeleniumJavaScriptClickTest { +import java.io.File; + +public class SeleniumJavaScriptClickLiveTest { private WebDriver driver; private WebDriverWait wait; @Before public void setUp() { - System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); + System.setProperty("webdriver.chrome.driver", new File("src/main/resources/chromedriver.mac").getAbsolutePath()); driver = new ChromeDriver(); - wait = new WebDriverWait(driver, 5000); + wait = new WebDriverWait(driver, 20); } @After @@ -37,19 +39,21 @@ public class SeleniumJavaScriptClickTest { String title = driver.getTitle(); assertEquals("Baeldung | Java, Spring and Web Development tutorials", title); - wait.until(ExpectedConditions.elementToBeClickable(By.className("menu-search"))); - WebElement searchButton = driver.findElement(By.className("menu-search")); + wait.until(ExpectedConditions.elementToBeClickable(By.className("nav--menu_item_anchor"))); + WebElement searchButton = driver.findElement(By.className("nav--menu_item_anchor")); clickElement(searchButton); wait.until(ExpectedConditions.elementToBeClickable(By.id("search"))); WebElement searchInput = driver.findElement(By.id("search")); searchInput.sendKeys("Selenium"); - wait.until(ExpectedConditions.elementToBeClickable(By.className("btn-search"))); - WebElement seeSearchResultsButton = driver.findElement(By.className("btn-search")); + wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".btn-search"))); + WebElement seeSearchResultsButton = driver.findElement(By.cssSelector(".btn-search")); clickElement(seeSearchResultsButton); - int seleniumPostsCount = driver.findElements(By.className("post")).size(); + wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("post"))); + int seleniumPostsCount = driver.findElements(By.className("post")) + .size(); assertTrue(seleniumPostsCount > 0); } diff --git a/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumCookiesJUnitLiveTest.java b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/cookies/SeleniumCookiesJUnitLiveTest.java similarity index 80% rename from testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumCookiesJUnitLiveTest.java rename to testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/cookies/SeleniumCookiesJUnitLiveTest.java index 0cbbf52454..337e3b85fb 100644 --- a/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumCookiesJUnitLiveTest.java +++ b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/cookies/SeleniumCookiesJUnitLiveTest.java @@ -1,4 +1,16 @@ -package test.java.com.baeldung.selenium.junit; +package com.baeldung.selenium.cookies; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; + +import java.io.File; +import java.util.Set; +import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; @@ -9,12 +21,6 @@ import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.remote.DesiredCapabilities; -import java.util.Set; -import java.util.concurrent.TimeUnit; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.*; - public class SeleniumCookiesJUnitLiveTest { private WebDriver driver; @@ -22,11 +28,21 @@ public class SeleniumCookiesJUnitLiveTest { @Before public void setUp() { + System.setProperty("webdriver.gecko.driver", findFile("geckodriver.mac")); + Capabilities capabilities = DesiredCapabilities.firefox(); driver = new FirefoxDriver(capabilities); navUrl = "https://baeldung.com"; driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); - System.setProperty("webdriver.gecko.driver", "geckodriver.exe"); + } + + private static String findFile(String filename) { + String[] paths = { "", "bin/", "target/classes" }; // if you have chromedriver somewhere else on the path, then put it here. + for (String path : paths) { + if (new File(path + filename).exists()) + return path + filename; + } + return ""; } @After diff --git a/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumWithJUnitLiveTest.java b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumWithJUnitLiveTest.java index b1a4149358..1b1035cc6c 100644 --- a/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumWithJUnitLiveTest.java +++ b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumWithJUnitLiveTest.java @@ -1,16 +1,21 @@ -package test.java.com.baeldung.selenium.junit; +package com.baeldung.selenium.junit; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; -import main.java.com.baeldung.selenium.SeleniumExample; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; -import static org.testng.Assert.*; +import com.baeldung.selenium.SeleniumExample; public class SeleniumWithJUnitLiveTest { private static SeleniumExample seleniumExample; - private String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials"; + private String expectedTitle = "About Baeldung | Baeldung"; @BeforeClass public static void setUp() { @@ -18,17 +23,17 @@ public class SeleniumWithJUnitLiveTest { } @AfterClass - public static void tearDown() { + public static void tearDown() throws IOException { seleniumExample.closeWindow(); } @Test public void whenAboutBaeldungIsLoaded_thenAboutEugenIsMentionedOnPage() { - seleniumExample.getAboutBaeldungPage(); - String actualTitle = seleniumExample.getTitle(); - assertNotNull(actualTitle); - assertEquals(expectedTitle, actualTitle); - assertTrue(seleniumExample.isAuthorInformationAvailable()); + seleniumExample.getAboutBaeldungPage(); + String actualTitle = seleniumExample.getTitle(); + assertNotNull(actualTitle); + assertEquals(expectedTitle, actualTitle); + assertTrue(seleniumExample.isAuthorInformationAvailable()); } } diff --git a/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumPageObjectLiveTest.java b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/pages/SeleniumPageObjectLiveTest.java similarity index 79% rename from testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumPageObjectLiveTest.java rename to testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/pages/SeleniumPageObjectLiveTest.java index 8493122414..96772821a9 100644 --- a/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumPageObjectLiveTest.java +++ b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/pages/SeleniumPageObjectLiveTest.java @@ -1,9 +1,9 @@ -package test.java.com.baeldung.selenium.junit; +package com.baeldung.selenium.pages; -import main.java.com.baeldung.selenium.config.SeleniumConfig; -import main.java.com.baeldung.selenium.models.BaeldungAbout; -import main.java.com.baeldung.selenium.pages.BaeldungHomePage; -import main.java.com.baeldung.selenium.pages.StartHerePage; +import com.baeldung.selenium.config.SeleniumConfig; +import com.baeldung.selenium.models.BaeldungAbout; +import com.baeldung.selenium.pages.BaeldungHomePage; +import com.baeldung.selenium.pages.StartHerePage; import org.junit.After; import org.junit.Before; import org.junit.Test; diff --git a/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/testng/SeleniumWithTestNGLiveTest.java b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/testng/SeleniumWithTestNGLiveTest.java index 94426edc6d..85fa00c0ab 100644 --- a/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/testng/SeleniumWithTestNGLiveTest.java +++ b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/testng/SeleniumWithTestNGLiveTest.java @@ -1,16 +1,19 @@ -package test.java.com.baeldung.selenium.testng; +package com.baeldung.selenium.testng; + +import com.baeldung.selenium.SeleniumExample; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; -import main.java.com.baeldung.selenium.SeleniumExample; import org.testng.annotations.AfterSuite; import org.testng.annotations.BeforeSuite; import org.testng.annotations.Test; -import static org.testng.Assert.*; - public class SeleniumWithTestNGLiveTest { private SeleniumExample seleniumExample; - private String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials"; + private String expectedTitle = "About Baeldung | Baeldung"; @BeforeSuite public void setUp() {