diff --git a/src/test/java/es/uvigo/esei/daa/util/AdditionalConditions.java b/src/test/java/es/uvigo/esei/daa/util/AdditionalConditions.java deleted file mode 100644 index 68bb9225e2f3d972c3f93428ea588d0f78ebdde2..0000000000000000000000000000000000000000 --- a/src/test/java/es/uvigo/esei/daa/util/AdditionalConditions.java +++ /dev/null @@ -1,14 +0,0 @@ -package es.uvigo.esei.daa.util; - -import org.openqa.selenium.JavascriptExecutor; -import org.openqa.selenium.support.ui.ExpectedCondition; - -/* - * Implementation based on https://stackoverflow.com/questions/33348600/selenium-wait-for-ajax-content-to-load-universal-approach - */ -public class AdditionalConditions { - public static ExpectedCondition jQueryAjaxCallsHaveCompleted() { - return driver -> - (Boolean) ((JavascriptExecutor) driver).executeScript("return (window.jQuery !== null) && (jQuery.active === 0)"); - } -} diff --git a/src/test/java/es/uvigo/esei/daa/web/PeopleWebTest.java b/src/test/java/es/uvigo/esei/daa/web/PeopleWebTest.java deleted file mode 100644 index 81313c8bdab5129e4a6f26e11bd69c78d520fd4f..0000000000000000000000000000000000000000 --- a/src/test/java/es/uvigo/esei/daa/web/PeopleWebTest.java +++ /dev/null @@ -1,134 +0,0 @@ -package es.uvigo.esei.daa.web; - -import static es.uvigo.esei.daa.dataset.PeopleDataset.existentId; -import static es.uvigo.esei.daa.dataset.PeopleDataset.existentPerson; -import static es.uvigo.esei.daa.dataset.PeopleDataset.newName; -import static es.uvigo.esei.daa.dataset.PeopleDataset.newPerson; -import static es.uvigo.esei.daa.dataset.PeopleDataset.newSurname; -import static es.uvigo.esei.daa.dataset.PeopleDataset.people; -import static es.uvigo.esei.daa.matchers.IsEqualToPerson.containsPeopleInAnyOrder; -import static es.uvigo.esei.daa.matchers.IsEqualToPerson.equalsToPerson; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; - -import java.util.concurrent.TimeUnit; - -import javax.sql.DataSource; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.firefox.FirefoxDriver; -import org.openqa.selenium.firefox.FirefoxOptions; -import org.openqa.selenium.firefox.FirefoxProfile; -import org.openqa.selenium.html5.LocalStorage; -import org.openqa.selenium.remote.DesiredCapabilities; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.TestExecutionListeners; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import com.github.springtestdbunit.DbUnitTestExecutionListener; -import com.github.springtestdbunit.annotation.DatabaseSetup; -import com.github.springtestdbunit.annotation.ExpectedDatabase; - -import es.uvigo.esei.daa.entities.Person; -import es.uvigo.esei.daa.listeners.ApplicationContextBinding; -import es.uvigo.esei.daa.listeners.ApplicationContextJndiBindingTestExecutionListener; -import es.uvigo.esei.daa.listeners.DbManagement; -import es.uvigo.esei.daa.listeners.DbManagementTestExecutionListener; -import es.uvigo.esei.daa.web.pages.MainPage; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration("classpath:contexts/hsql-context.xml") -@TestExecutionListeners({ - DbUnitTestExecutionListener.class, - DbManagementTestExecutionListener.class, - ApplicationContextJndiBindingTestExecutionListener.class -}) -@ApplicationContextBinding( - jndiUrl = "java:/comp/env/jdbc/daaexample", - type = DataSource.class -) -@DbManagement( - create = "classpath:db/hsqldb.sql", - drop = "classpath:db/hsqldb-drop.sql" -) -@DatabaseSetup("/datasets/dataset.xml") -@ExpectedDatabase("/datasets/dataset.xml") -public class PeopleWebTest { - private static final int DEFAULT_WAIT_TIME = 1; - - private WebDriver driver; - private MainPage mainPage; - - @Before - public void setUp() throws Exception { - final String baseUrl = "http://localhost:9080/DAAExample/"; - - final FirefoxProfile profile = new FirefoxProfile(); - profile.setPreference("browser.privatebrowsing.autostart", true); - - final FirefoxOptions options = new FirefoxOptions(DesiredCapabilities.firefox()); - options.setProfile(profile); - - final FirefoxDriver firefoxDriver; - driver = firefoxDriver = new FirefoxDriver(); - driver.get(baseUrl); - - // Driver will wait DEFAULT_WAIT_TIME if it doesn't find and element. - driver.manage().timeouts().implicitlyWait(DEFAULT_WAIT_TIME, TimeUnit.SECONDS); - driver.manage().window().maximize(); - - // Login as "admin:adminpass" - final LocalStorage localStorage = firefoxDriver.getLocalStorage(); - localStorage.setItem("authorization-token", "YWRtaW46YWRtaW5wYXNz"); - - mainPage = new MainPage(driver, baseUrl); - mainPage.navigateTo(); - } - - @After - public void tearDown() throws Exception { - driver.quit(); - driver = null; - mainPage = null; - } - - @Test - public void testList() throws Exception { - assertThat(mainPage.listPeople(), containsPeopleInAnyOrder(people())); - } - - @Test - @ExpectedDatabase("/datasets/dataset-add.xml") - public void testAdd() throws Exception { - final Person newPerson = mainPage.addPerson(newName(), newSurname()); - - assertThat(newPerson, is(equalsToPerson(newPerson()))); - } - - @Test - @ExpectedDatabase("/datasets/dataset-modify.xml") - public void testEdit() throws Exception { - final Person person = existentPerson(); - person.setName(newName()); - person.setSurname(newSurname()); - - mainPage.editPerson(person); - - final Person webPerson = mainPage.getPerson(person.getId()); - - assertThat(webPerson, is(equalsToPerson(person))); - } - - @Test - @ExpectedDatabase("/datasets/dataset-delete.xml") - public void testDelete() throws Exception { - mainPage.deletePerson(existentId()); - - assertFalse(mainPage.hasPerson(existentId())); - } -} diff --git a/src/test/java/es/uvigo/esei/daa/web/pages/MainPage.java b/src/test/java/es/uvigo/esei/daa/web/pages/MainPage.java deleted file mode 100644 index 97b6a1a230a6f8135a33e1beae89b06a0d14504d..0000000000000000000000000000000000000000 --- a/src/test/java/es/uvigo/esei/daa/web/pages/MainPage.java +++ /dev/null @@ -1,242 +0,0 @@ -package es.uvigo.esei.daa.web.pages; - -import static es.uvigo.esei.daa.util.AdditionalConditions.jQueryAjaxCallsHaveCompleted; -import static java.util.stream.Collectors.toList; -import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated; -import static org.openqa.selenium.support.ui.ExpectedConditions.textToBePresentInElement; - -import java.util.List; - -import org.openqa.selenium.By; -import org.openqa.selenium.NoSuchElementException; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.WebElement; -import org.openqa.selenium.support.ui.WebDriverWait; - -import es.uvigo.esei.daa.entities.Person; - -public class MainPage { - private static final String TABLE_ID = "people-list"; - private static final String FORM_ID = "people-form"; - - private static final String ID_PREFIX = "person-"; - - private final WebDriver driver; - - private final WebDriverWait wait; - - private final String baseUrl; - - public MainPage(WebDriver driver, String baseUrl) { - this.driver = driver; - this.baseUrl = baseUrl; - - this.wait = new WebDriverWait(driver, 1); - } - - public void navigateTo() { - this.driver.get(this.baseUrl + "main.html"); - - this.wait.until(presenceOfElementLocated(By.id("people-list"))); - } - - public int countPeople() { - return new PeopleTable(this.driver).countPeople(); - } - - public List listPeople() { - return new PeopleTable(this.driver).listPeople(); - } - - public Person getLastPerson() { - return new PeopleTable(this.driver).getPersonInLastRow(); - } - - public Person getPerson(int id) { - return new PeopleTable(this.driver).getPersonById(id); - } - - public boolean hasPerson(int id) { - return new PeopleTable(this.driver).hasPerson(id); - } - - public Person addPerson(String name, String surname) { - final PersonForm form = new PersonForm(this.driver); - - form.clear(); - form.setName(name); - form.setSurname(surname); - form.submit(); - - final PeopleTable table = new PeopleTable(driver); - return table.getPerson(name, surname); - } - - public void editPerson(Person person) { - final PeopleTable table = new PeopleTable(this.driver); - table.editPerson(person.getId()); - - final PersonForm form = new PersonForm(this.driver); - form.setName(person.getName()); - form.setSurname(person.getSurname()); - form.submit(); - } - - public void deletePerson(int id) { - final PeopleTable table = new PeopleTable(this.driver); - - table.deletePerson(id); - - wait.until(jQueryAjaxCallsHaveCompleted()); - } - - private final static class PeopleTable { - private final WebDriver driver; - - private final WebElement table; - - public PeopleTable(WebDriver driver) { - this.driver = driver; - - this.table = this.driver.findElement(By.id(TABLE_ID)); - } - - public boolean hasPerson(int id) { - try { - return this.getPersonRow(id) != null; - } catch (NoSuchElementException nsee) { - return false; - } - } - - public void editPerson(int id) { - final WebElement personRow = this.getPersonRow(id); - - personRow.findElement(By.className("edit")).click(); - } - - public void deletePerson(int id) { - final WebElement personRow = this.getPersonRow(id); - - personRow.findElement(By.className("delete")).click(); - - this.acceptDialog(); - } - - public Person getPersonById(int id) { - return rowToPerson(getPersonRow(id)); - } - - public Person getPerson(String name, String surname) { - return rowToPerson(getPersonRow(name, surname)); - } - - public Person getPersonInLastRow() { - final WebElement row = this.table.findElement(By.cssSelector("tbody > tr:last-child")); - - return rowToPerson(row); - } - - private WebElement getPersonRow(int id) { - return this.table.findElement(By.id(ID_PREFIX + id)); - } - - public WebElement getPersonRow(String name, String surname) { - final List rows = table.findElements(By.cssSelector("tbody > tr")); - - for (WebElement row : rows) { - final String rowName = row.findElement(By.className("name")).getText(); - final String rowSurname = row.findElement(By.className("surname")).getText(); - - if (rowName.equals(name) && rowSurname.equals(surname)) { - return row; - } - } - - throw new IllegalArgumentException(String.format("No row found with name '%s' and surname '%s'", name, surname)); - } - - public int countPeople() { - return getRows().size(); - } - - public List listPeople() { - return getRows().stream() - .map(this::rowToPerson) - .collect(toList()); - } - - private List getRows() { - final String xpathQuery = "//tbody/tr[starts-with(@id, '" + ID_PREFIX + "')]"; - - return this.table.findElements(By.xpath(xpathQuery)); - } - - private Person rowToPerson(WebElement row) { - return new Person( - Integer.parseInt(row.getAttribute("id").substring(ID_PREFIX.length())), - row.findElement(By.className("name")).getText(), - row.findElement(By.className("surname")).getText() - ); - } - - private void acceptDialog() { - driver.switchTo().alert().accept(); - } - } - - public final static class PersonForm { - private final WebDriverWait wait; - - private final WebElement fieldName; - private final WebElement fieldSurname; - private final WebElement buttonClear; - private final WebElement buttonSubmit; - - public PersonForm(WebDriver driver) { - this.wait = new WebDriverWait(driver, 1); - - final WebElement form = driver.findElement(By.id(FORM_ID)); - - this.fieldName = form.findElement(By.name("name")); - this.fieldSurname = form.findElement(By.name("surname")); - this.buttonClear = form.findElement(By.id("btnClear")); - this.buttonSubmit = form.findElement(By.id("btnSubmit")); - } - - public void submit() { - this.buttonSubmit.click(); - - this.waitForCleanFields(); - } - - public void clear() { - this.buttonClear.click(); - - this.waitForCleanFields(); - } - - public void setName(String name) { - this.fieldName.clear(); - this.fieldName.sendKeys(name); - } - - public void setSurname(String surname) { - this.fieldSurname.clear(); - this.fieldSurname.sendKeys(surname); - } - - public String getName() { - return this.fieldName.getText(); - } - - public String getSurname() { - return this.fieldSurname.getText(); - } - - private void waitForCleanFields() { - wait.until(textToBePresentInElement(fieldName, "")); - wait.until(textToBePresentInElement(fieldSurname, "")); - } - } -}