Merge branch 'BAEL-4198-fix-selenium-testing-module'

This commit is contained in:
Jonathan Cook 2020-06-29 12:06:36 +02:00
commit a768e5e8f0
12 changed files with 129 additions and 103 deletions

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>selenium-junit-testng</artifactId> <artifactId>selenium-junit-testng</artifactId>
@ -43,32 +44,22 @@
</dependencies> </dependencies>
<build> <build>
<sourceDirectory>src</sourceDirectory> <resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build> </build>
<profiles>
<profile>
<id>live</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<includes>
<include>**/*LiveTest.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<properties> <properties>
<testng.version>6.10</testng.version> <testng.version>6.10</testng.version>
<selenium-java.version>3.4.0</selenium-java.version> <selenium-java.version>3.4.0</selenium-java.version>
<ashot.version>1.5.4</ashot.version>
</properties> </properties>
</project> </project>

View File

@ -1,14 +1,12 @@
package main.java.com.baeldung.selenium; package 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;
import java.util.List; 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 { public class SeleniumExample {
@ -17,15 +15,18 @@ public class SeleniumExample {
public SeleniumExample() { public SeleniumExample() {
config = new SeleniumConfig(); config = new SeleniumConfig();
config.getDriver().get(url); config.getDriver()
.get(url);
} }
public void closeWindow() { public void closeWindow() {
this.config.getDriver().close(); this.config.getDriver()
.close();
} }
public String getTitle() { public String getTitle() {
return this.config.getDriver().getTitle(); return this.config.getDriver()
.getTitle();
} }
public void getAboutBaeldungPage() { public void getAboutBaeldungPage() {
@ -35,7 +36,8 @@ public class SeleniumExample {
} }
private void closeOverlay() { private void closeOverlay() {
List<WebElement> webElementList = this.config.getDriver().findElements(By.tagName("a")); List<WebElement> webElementList = this.config.getDriver()
.findElements(By.tagName("a"));
if (webElementList != null) { if (webElementList != null) {
webElementList.stream() webElementList.stream()
.filter(webElement -> "Close".equalsIgnoreCase(webElement.getAttribute("title"))) .filter(webElement -> "Close".equalsIgnoreCase(webElement.getAttribute("title")))
@ -46,18 +48,23 @@ public class SeleniumExample {
} }
private void clickAboutLink() { 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() { private void clickAboutUsLink() {
Actions builder = new Actions(config.getDriver()); WebElement element = this.config.getDriver()
WebElement element = this.config.getDriver().findElement(By.partialLinkText("About Baeldung.")); .findElement(By.partialLinkText("About Baeldung."));
builder.moveToElement(element).build().perform(); element.click();
} }
public boolean isAuthorInformationAvailable() { public boolean isAuthorInformationAvailable() {
return this.config.getDriver() return this.config.getDriver()
.findElement(By.cssSelector("article > .row > div")) .getPageSource()
.isDisplayed(); .contains("Hey ! I'm Eugen");
} }
} }

View File

@ -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.Capabilities;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement; import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities; 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 { public class SeleniumConfig {
private WebDriver driver; private WebDriver driver;
@ -19,15 +16,17 @@ public class SeleniumConfig {
public SeleniumConfig() { public SeleniumConfig() {
Capabilities capabilities = DesiredCapabilities.firefox(); Capabilities capabilities = DesiredCapabilities.firefox();
driver = new FirefoxDriver(capabilities); driver = new FirefoxDriver(capabilities);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.manage()
.timeouts()
.implicitlyWait(5, TimeUnit.SECONDS);
} }
static { static {
System.setProperty("webdriver.gecko.driver", findFile("geckodriver.mac")); System.setProperty("webdriver.gecko.driver", findFile("geckodriver.mac"));
} }
static private String findFile(String filename) { private static String findFile(String filename) {
String paths[] = {"", "bin/", "target/classes"}; // if you have chromedriver somewhere else on the path, then put it here. String[] paths = { "", "bin/", "target/classes" }; // if you have chromedriver somewhere else on the path, then put it here.
for (String path : paths) { for (String path : paths) {
if (new File(path + filename).exists()) if (new File(path + filename).exists())
return path + filename; return path + filename;
@ -40,7 +39,8 @@ public class SeleniumConfig {
} }
public void navigateTo(String url) { public void navigateTo(String url) {
driver.navigate().to(url); driver.navigate()
.to(url);
} }
public void clickElement(WebElement element) { public void clickElement(WebElement element) {

View File

@ -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 com.baeldung.selenium.config.SeleniumConfig;
import main.java.com.baeldung.selenium.pages.BaeldungAboutPage; import com.baeldung.selenium.pages.BaeldungAboutPage;
import org.openqa.selenium.support.PageFactory; import org.openqa.selenium.support.PageFactory;
public class BaeldungAbout { public class BaeldungAbout {

View File

@ -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.WebElement;
import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.FindBy;

View File

@ -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.WebElement;
import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory; import org.openqa.selenium.support.PageFactory;
@ -8,7 +8,7 @@ import org.openqa.selenium.support.PageFactory;
public class BaeldungHomePage { public class BaeldungHomePage {
private SeleniumConfig config; private SeleniumConfig config;
@FindBy(css=".header--menu > a") @FindBy(css = ".nav--logo_mobile")
private WebElement title; private WebElement title;
@FindBy(css = ".menu-start-here > a") @FindBy(css = ".menu-start-here > a")
private WebElement startHere; private WebElement startHere;

View File

@ -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.WebElement;
import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.FindBy;

View File

@ -1,4 +1,4 @@
package java.com.baeldung.selenium.clickusingjavascript; package com.baeldung.selenium.clickusingjavascript;
import org.junit.After; import org.junit.After;
import org.junit.Before; 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.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class SeleniumJavaScriptClickTest { import java.io.File;
public class SeleniumJavaScriptClickLiveTest {
private WebDriver driver; private WebDriver driver;
private WebDriverWait wait; private WebDriverWait wait;
@Before @Before
public void setUp() { 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(); driver = new ChromeDriver();
wait = new WebDriverWait(driver, 5000); wait = new WebDriverWait(driver, 20);
} }
@After @After
@ -37,19 +39,21 @@ public class SeleniumJavaScriptClickTest {
String title = driver.getTitle(); String title = driver.getTitle();
assertEquals("Baeldung | Java, Spring and Web Development tutorials", title); assertEquals("Baeldung | Java, Spring and Web Development tutorials", title);
wait.until(ExpectedConditions.elementToBeClickable(By.className("menu-search"))); wait.until(ExpectedConditions.elementToBeClickable(By.className("nav--menu_item_anchor")));
WebElement searchButton = driver.findElement(By.className("menu-search")); WebElement searchButton = driver.findElement(By.className("nav--menu_item_anchor"));
clickElement(searchButton); clickElement(searchButton);
wait.until(ExpectedConditions.elementToBeClickable(By.id("search"))); wait.until(ExpectedConditions.elementToBeClickable(By.id("search")));
WebElement searchInput = driver.findElement(By.id("search")); WebElement searchInput = driver.findElement(By.id("search"));
searchInput.sendKeys("Selenium"); searchInput.sendKeys("Selenium");
wait.until(ExpectedConditions.elementToBeClickable(By.className("btn-search"))); wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".btn-search")));
WebElement seeSearchResultsButton = driver.findElement(By.className("btn-search")); WebElement seeSearchResultsButton = driver.findElement(By.cssSelector(".btn-search"));
clickElement(seeSearchResultsButton); 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); assertTrue(seleniumPostsCount > 0);
} }

View File

@ -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.After;
import org.junit.Before; import org.junit.Before;
@ -9,12 +21,6 @@ import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities; 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 { public class SeleniumCookiesJUnitLiveTest {
private WebDriver driver; private WebDriver driver;
@ -22,11 +28,21 @@ public class SeleniumCookiesJUnitLiveTest {
@Before @Before
public void setUp() { public void setUp() {
System.setProperty("webdriver.gecko.driver", findFile("geckodriver.mac"));
Capabilities capabilities = DesiredCapabilities.firefox(); Capabilities capabilities = DesiredCapabilities.firefox();
driver = new FirefoxDriver(capabilities); driver = new FirefoxDriver(capabilities);
navUrl = "https://baeldung.com"; navUrl = "https://baeldung.com";
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 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 @After

View File

@ -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.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import static org.testng.Assert.*; import com.baeldung.selenium.SeleniumExample;
public class SeleniumWithJUnitLiveTest { public class SeleniumWithJUnitLiveTest {
private static SeleniumExample seleniumExample; private static SeleniumExample seleniumExample;
private String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials"; private String expectedTitle = "About Baeldung | Baeldung";
@BeforeClass @BeforeClass
public static void setUp() { public static void setUp() {
@ -18,7 +23,7 @@ public class SeleniumWithJUnitLiveTest {
} }
@AfterClass @AfterClass
public static void tearDown() { public static void tearDown() throws IOException {
seleniumExample.closeWindow(); seleniumExample.closeWindow();
} }

View File

@ -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 com.baeldung.selenium.config.SeleniumConfig;
import main.java.com.baeldung.selenium.models.BaeldungAbout; import com.baeldung.selenium.models.BaeldungAbout;
import main.java.com.baeldung.selenium.pages.BaeldungHomePage; import com.baeldung.selenium.pages.BaeldungHomePage;
import main.java.com.baeldung.selenium.pages.StartHerePage; import com.baeldung.selenium.pages.StartHerePage;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;

View File

@ -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.AfterSuite;
import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import static org.testng.Assert.*;
public class SeleniumWithTestNGLiveTest { public class SeleniumWithTestNGLiveTest {
private SeleniumExample seleniumExample; private SeleniumExample seleniumExample;
private String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials"; private String expectedTitle = "About Baeldung | Baeldung";
@BeforeSuite @BeforeSuite
public void setUp() { public void setUp() {