renamed back to testing-modules, pulled together testing-modules-2 modules into single module
This commit is contained in:
3
testing-modules/selenium-junit-testng/README.md
Normal file
3
testing-modules/selenium-junit-testng/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
- [Guide to Selenium with JUnit / TestNG](http://www.baeldung.com/java-selenium-with-junit-and-testng)
|
||||
- [Testing with Selenium/WebDriver and the Page Object Pattern](http://www.baeldung.com/selenium-webdriver-page-object)
|
||||
BIN
testing-modules/selenium-junit-testng/geckodriver.mac
Normal file
BIN
testing-modules/selenium-junit-testng/geckodriver.mac
Normal file
Binary file not shown.
73
testing-modules/selenium-junit-testng/pom.xml
Normal file
73
testing-modules/selenium-junit-testng/pom.xml
Normal file
@@ -0,0 +1,73 @@
|
||||
<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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>selenium-junit-testng</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>selenium-junit-testng</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.seleniumhq.selenium</groupId>
|
||||
<artifactId>selenium-java</artifactId>
|
||||
<version>${selenium-java.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testng</groupId>
|
||||
<artifactId>testng</artifactId>
|
||||
<version>${testng.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-all</artifactId>
|
||||
<version>${org.hamcrest.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<sourceDirectory>src</sourceDirectory>
|
||||
</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>
|
||||
<testng.version>6.10</testng.version>
|
||||
<selenium-java.version>3.4.0</selenium-java.version>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -0,0 +1,63 @@
|
||||
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;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class SeleniumExample {
|
||||
|
||||
private SeleniumConfig config;
|
||||
private String url = "http://www.baeldung.com/";
|
||||
|
||||
public SeleniumExample() {
|
||||
config = new SeleniumConfig();
|
||||
config.getDriver().get(url);
|
||||
}
|
||||
|
||||
public void closeWindow() {
|
||||
this.config.getDriver().close();
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return this.config.getDriver().getTitle();
|
||||
}
|
||||
|
||||
public void getAboutBaeldungPage() {
|
||||
closeOverlay();
|
||||
clickAboutLink();
|
||||
clickAboutUsLink();
|
||||
}
|
||||
|
||||
private void closeOverlay() {
|
||||
List<WebElement> 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);
|
||||
}
|
||||
}
|
||||
|
||||
private void clickAboutLink() {
|
||||
this.config.getDriver().findElement(By.partialLinkText("About")).click();
|
||||
}
|
||||
|
||||
private void clickAboutUsLink() {
|
||||
Actions builder = new Actions(config.getDriver());
|
||||
WebElement element = this.config.getDriver().findElement(By.partialLinkText("About Baeldung."));
|
||||
builder.moveToElement(element).build().perform();
|
||||
}
|
||||
|
||||
public boolean isAuthorInformationAvailable() {
|
||||
return this.config.getDriver()
|
||||
.findElement(By.cssSelector("article > .row > div"))
|
||||
.isDisplayed();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package main.java.com.baeldung.selenium.config;
|
||||
|
||||
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;
|
||||
|
||||
public SeleniumConfig() {
|
||||
Capabilities capabilities = DesiredCapabilities.firefox();
|
||||
driver = new FirefoxDriver(capabilities);
|
||||
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.
|
||||
for (String path : paths) {
|
||||
if (new File(path + filename).exists())
|
||||
return path + filename;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public void close() {
|
||||
driver.close();
|
||||
}
|
||||
|
||||
public void navigateTo(String url) {
|
||||
driver.navigate().to(url);
|
||||
}
|
||||
|
||||
public void clickElement(WebElement element) {
|
||||
element.click();
|
||||
}
|
||||
|
||||
public WebDriver getDriver() {
|
||||
return driver;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package main.java.com.baeldung.selenium.models;
|
||||
|
||||
import main.java.com.baeldung.selenium.config.SeleniumConfig;
|
||||
import main.java.com.baeldung.selenium.pages.BaeldungAboutPage;
|
||||
import org.openqa.selenium.support.PageFactory;
|
||||
|
||||
public class BaeldungAbout {
|
||||
|
||||
private SeleniumConfig config;
|
||||
|
||||
public BaeldungAbout(SeleniumConfig config) {
|
||||
this.config = config;
|
||||
PageFactory.initElements(config.getDriver(), BaeldungAboutPage.class);
|
||||
}
|
||||
|
||||
public void navigateTo() {
|
||||
config.navigateTo("http://www.baeldung.com/about/");
|
||||
}
|
||||
|
||||
public String getPageTitle() {
|
||||
return BaeldungAboutPage.title.getText();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package main.java.com.baeldung.selenium.pages;
|
||||
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.support.FindBy;
|
||||
|
||||
public class BaeldungAboutPage {
|
||||
|
||||
@FindBy(css = ".page-header > h1")
|
||||
public static WebElement title;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package main.java.com.baeldung.selenium.pages;
|
||||
|
||||
import main.java.com.baeldung.selenium.config.SeleniumConfig;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.support.FindBy;
|
||||
import org.openqa.selenium.support.PageFactory;
|
||||
|
||||
public class BaeldungHomePage {
|
||||
|
||||
private SeleniumConfig config;
|
||||
@FindBy(css=".header--menu > a")
|
||||
private WebElement title;
|
||||
@FindBy(css = ".menu-start-here > a")
|
||||
private WebElement startHere;
|
||||
|
||||
public BaeldungHomePage(SeleniumConfig config) {
|
||||
this.config = config;
|
||||
PageFactory.initElements(this.config.getDriver(), this);
|
||||
}
|
||||
|
||||
public void navigate() {
|
||||
this.config.navigateTo("http://www.baeldung.com/");
|
||||
}
|
||||
|
||||
public String getPageTitle() {
|
||||
return title.getAttribute("title");
|
||||
}
|
||||
|
||||
public StartHerePage clickOnStartHere() {
|
||||
config.clickElement(startHere);
|
||||
|
||||
StartHerePage startHerePage = new StartHerePage(config);
|
||||
PageFactory.initElements(config.getDriver(), startHerePage);
|
||||
|
||||
return startHerePage;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package main.java.com.baeldung.selenium.pages;
|
||||
|
||||
import main.java.com.baeldung.selenium.config.SeleniumConfig;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.support.FindBy;
|
||||
|
||||
public class StartHerePage {
|
||||
|
||||
private SeleniumConfig config;
|
||||
|
||||
@FindBy(css = ".page-title")
|
||||
private WebElement title;
|
||||
|
||||
public StartHerePage(SeleniumConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public String getPageTitle() {
|
||||
return title.getText();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,50 @@
|
||||
package test.java.com.baeldung.selenium.junit;
|
||||
|
||||
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 org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
public class SeleniumPageObjectLiveTest {
|
||||
|
||||
private SeleniumConfig config;
|
||||
private BaeldungHomePage homePage;
|
||||
private BaeldungAbout about;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
config = new SeleniumConfig();
|
||||
homePage = new BaeldungHomePage(config);
|
||||
about = new BaeldungAbout(config);
|
||||
}
|
||||
|
||||
@After
|
||||
public void teardown() {
|
||||
config.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHomePage_whenNavigate_thenTitleMatch() {
|
||||
homePage.navigate();
|
||||
assertThat(homePage.getPageTitle(), is("Baeldung"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHomePage_whenNavigate_thenShouldBeInStartHere() {
|
||||
homePage.navigate();
|
||||
StartHerePage startHerePage = homePage.clickOnStartHere();
|
||||
assertThat(startHerePage.getPageTitle(), is("Start Here"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAboutPage_whenNavigate_thenTitleMatch() {
|
||||
about.navigateTo();
|
||||
assertThat(about.getPageTitle(), is("About Baeldung"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package test.java.com.baeldung.selenium.junit;
|
||||
|
||||
import main.java.com.baeldung.selenium.SeleniumExample;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class SeleniumWithJUnitLiveTest {
|
||||
|
||||
private static SeleniumExample seleniumExample;
|
||||
private String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials";
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() {
|
||||
seleniumExample = new SeleniumExample();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDown() {
|
||||
seleniumExample.closeWindow();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAboutBaeldungIsLoaded_thenAboutEugenIsMentionedOnPage() {
|
||||
seleniumExample.getAboutBaeldungPage();
|
||||
String actualTitle = seleniumExample.getTitle();
|
||||
assertNotNull(actualTitle);
|
||||
assertEquals(expectedTitle, actualTitle);
|
||||
assertTrue(seleniumExample.isAuthorInformationAvailable());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package test.java.com.baeldung.selenium.testng;
|
||||
|
||||
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";
|
||||
|
||||
@BeforeSuite
|
||||
public void setUp() {
|
||||
seleniumExample = new SeleniumExample();
|
||||
}
|
||||
|
||||
@AfterSuite
|
||||
public void tearDown() {
|
||||
seleniumExample.closeWindow();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAboutBaeldungIsLoaded_thenAboutEugenIsMentionedOnPage() {
|
||||
seleniumExample.getAboutBaeldungPage();
|
||||
String actualTitle = seleniumExample.getTitle();
|
||||
assertNotNull(actualTitle);
|
||||
assertEquals(expectedTitle, actualTitle);
|
||||
assertTrue(seleniumExample.isAuthorInformationAvailable());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user