Feature/selenium page object pattern (#1993)

* Add project for hibernate immutable article
Add Event entity
Add hibernate configuration file
Add hibernateutil for configuration
Add test to match snippets from article

* Update master

* Selenium Page Object Pattern
Add Chromedrivers
Include hamcrest dependency for matchers on tests
Add PageObjects for Start Here and Home Page
Create config class for selenium

* Update Readme file
Add About page and model for additional example
Create new test for new impl
This commit is contained in:
Walter Gómez 2017-06-06 11:35:31 -06:00 committed by Grzegorz Piwowarek
parent a48ded75c0
commit 3338f51cae
11 changed files with 207 additions and 0 deletions

View File

@ -1,2 +1,3 @@
### Relevant Articles:
- [Guide to Selenium with JUnit / TestNG](http://www.baeldung.com/java-selenium-with-junit-and-testng)
- [Testing a Site with Selenium / Webdriver](http://www.baeldung.com)

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -57,6 +57,11 @@
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
<properties>

View File

@ -0,0 +1,60 @@
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.remote.DesiredCapabilities;
import java.io.File;
public class SeleniumConfig {
private WebDriver driver;
public SeleniumConfig() {
Capabilities capabilities = DesiredCapabilities.chrome();
driver = new ChromeDriver(capabilities);
}
static {
if (getOsName("os.name").toLowerCase().contains("mac")) {
System.setProperty("webdriver.chrome.driver", findFile("chromedriver.mac"));
} else if (getOsName("os.name").toLowerCase().contains("nix") ||
getOsName("os.name").toLowerCase().contains("nux") ||
getOsName("os.name").toLowerCase().contains("aix")) {
System.setProperty("webdriver.chrome.driver", findFile("chromedriver.linux"));
} else if (getOsName("os.name").toLowerCase().contains("win")) {
System.setProperty("webdriver.chrome.driver", findFile("chromedriver.exe"));
}
}
private static String getOsName(String prop) {
return (System.getProperty(prop));
}
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.quit();
}
public void navigateTo(String url) {
driver.navigate().to(url);
}
public void clickElement(WebElement element) {
element.click();
}
public WebDriver getDriver() {
return driver;
}
}

View File

@ -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();
}
}

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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 SeleniumPageObjectTest {
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"));
}
}