diff --git a/build.gradle b/build.gradle index 71c9977..e57e620 100644 --- a/build.gradle +++ b/build.gradle @@ -127,5 +127,7 @@ dependencies { testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test' compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.3.1' + testCompile group: 'org.seleniumhq.selenium', name: 'selenium-support', version: '3.3.1' + testCompile group: 'com.google.guava', name: 'guava', version: '21.0' } \ No newline at end of file diff --git a/src/main/resources/templates/papers/fragments/paperDashboardFragment.html b/src/main/resources/templates/papers/fragments/paperDashboardFragment.html index d0ec0c5..62d2466 100644 --- a/src/main/resources/templates/papers/fragments/paperDashboardFragment.html +++ b/src/main/resources/templates/papers/fragments/paperDashboardFragment.html @@ -10,7 +10,8 @@
-

diff --git a/src/main/resources/templates/papers/paper.html b/src/main/resources/templates/papers/paper.html index 2755aba..2fbfa3a 100644 --- a/src/main/resources/templates/papers/paper.html +++ b/src/main/resources/templates/papers/paper.html @@ -87,14 +87,16 @@
-
+
-
-
@@ -234,7 +236,8 @@
-
@@ -247,7 +250,8 @@
-
diff --git a/src/test/java/PaperTest.java b/src/test/java/PaperTest.java new file mode 100644 index 0000000..a08a1d8 --- /dev/null +++ b/src/test/java/PaperTest.java @@ -0,0 +1,247 @@ +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Iterables; +import core.PageObject; +import core.TestTemplate; +import org.junit.Assert; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import paper.PaperPage; +import paper.PapersDashboardPage; +import paper.PapersPage; +import ru.ulstu.NgTrackerApplication; +import ru.ulstu.configuration.ApplicationProperties; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +@RunWith(SpringRunner.class) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@SpringBootTest(classes = NgTrackerApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +public class PaperTest extends TestTemplate { + private final Map> navigationHolder = ImmutableMap.of( + new PapersPage(), Arrays.asList("СТАТЬИ", "/papers/papers"), + new PaperPage(), Arrays.asList("РЕДАКТИРОВАНИЕ СТАТЬИ", "/papers/paper?id=0"), + new PapersDashboardPage(), Arrays.asList("СТАТЬИ", "/papers/dashboard") + ); + + @Autowired + private ApplicationProperties applicationProperties; + + private String getPaperPageUrl() { + return Iterables.get(navigationHolder.entrySet(), 1).getValue().get(1); + } + + private PaperPage getPaperPage() { + PaperPage paperPage = (PaperPage) getContext().initPage(Iterables.get(navigationHolder.entrySet(), 1).getKey()); + paperPage.initElements(); + return paperPage; + } + + private String getPapersPageUrl() { + return Iterables.get(navigationHolder.entrySet(), 0).getValue().get(1); + } + + private PapersPage getPapersPage() { + PapersPage papersPage = (PapersPage) getContext().initPage(Iterables.get(navigationHolder.entrySet(), 0).getKey()); + papersPage.initElements(); + return papersPage; + } + + private String getPapersDashboardPageUrl() { + return Iterables.get(navigationHolder.entrySet(), 2).getValue().get(1); + } + + private PapersDashboardPage getPapersDashboardPage() { + PapersDashboardPage papersDashboardPage = (PapersDashboardPage) getContext().initPage(Iterables.get(navigationHolder.entrySet(), 2).getKey()); + papersDashboardPage.initElements(); + return papersDashboardPage; + } + + @Test + public void createNewPaperTest() { + getContext().goTo(applicationProperties.getBaseUrl() + getPaperPageUrl()); + PaperPage paperPage = getPaperPage(); + + String testTitle = "test " + (String.valueOf(System.currentTimeMillis())); + fillRequiredFields(paperPage, testTitle); + paperPage.clickSaveBtn(); + + PapersPage papersPage = getPapersPage(); + + Assert.assertTrue(papersPage.havePaperWithTitle(testTitle)); + } + + @Test + public void editPaperTest() { + createNewPaper(); + getContext().goTo(applicationProperties.getBaseUrl() + getPapersPageUrl()); + PapersPage papersPage = getPapersPage(); + papersPage.clickFirstPaper(); + + PaperPage paperPage = getPaperPage(); + String testTitle = "test " + (String.valueOf(System.currentTimeMillis())); + paperPage.setTitle(testTitle); + paperPage.clickSaveBtn(); + + Assert.assertTrue(papersPage.havePaperWithTitle(testTitle)); + } + + private void createNewPaper() { + getContext().goTo(applicationProperties.getBaseUrl() + getPaperPageUrl()); + PaperPage paperPage = getPaperPage(); + String testTitle = "test " + (String.valueOf(System.currentTimeMillis())); + fillRequiredFields(paperPage, testTitle); + paperPage.clickSaveBtn(); + } + + @Test + public void addDeadlineTest() { + createNewPaper(); + getContext().goTo(applicationProperties.getBaseUrl() + getPapersPageUrl()); + PapersPage papersPage = getPapersPage(); + papersPage.clickFirstPaper(); + + PaperPage paperPage = getPaperPage(); + papersPage.clickAddDeadline(); + String testDate = "01.01.2019"; + String testDateResult = "2019-01-01"; + String testDesc = "desc"; + Integer deadlineNumber = 2; + paperPage.setDeadlineDate(deadlineNumber, testDate); + paperPage.setDeadlineDescription(deadlineNumber, testDesc); + String paperId = paperPage.getId(); + paperPage.clickSaveBtn(); + + getContext().goTo(applicationProperties.getBaseUrl() + String.format("/papers/paper?id=%s", paperId)); + + Assert.assertTrue(paperPage.deadlineExist(testDesc, testDateResult)); + } + + @Test + public void noDeadlinesValidationTest() { + getContext().goTo(applicationProperties.getBaseUrl() + getPaperPageUrl()); + PaperPage paperPage = getPaperPage(); + + String testTitle = "test " + (String.valueOf(System.currentTimeMillis())); + paperPage.setTitle(testTitle); + paperPage.clickSaveBtn(); + + Assert.assertTrue(paperPage.hasAlert("Не может быть пустым")); + } + + private void fillRequiredFields(PaperPage paperPage, String title) { + paperPage.setTitle(title); + String testDate = "01.01.2019"; + String testDesc = "desc"; + Integer deadlineNumber = 1; + paperPage.setDeadlineDate(deadlineNumber, testDate); + paperPage.setDeadlineDescription(deadlineNumber, testDesc); + } + + @Test + public void addReferenceTest() { + createNewPaper(); + getContext().goTo(applicationProperties.getBaseUrl() + getPapersPageUrl()); + PapersPage papersPage = getPapersPage(); + papersPage.clickFirstPaper(); + + PaperPage paperPage = getPaperPage(); + fillRequiredFields(paperPage, "test " + (String.valueOf(System.currentTimeMillis()))); + paperPage.clickReferenceTab(); + paperPage.clickAddReferenceButton(); + + paperPage.clickReferenceTab(); + paperPage.showFirstReference(); + String authors = "testAuthors"; + paperPage.setFirstReferenceAuthors(authors); + + String paperId = paperPage.getId(); + paperPage.clickSaveBtn(); + + getContext().goTo(applicationProperties.getBaseUrl() + String.format("/papers/paper?id=%s", paperId)); + + Assert.assertTrue(paperPage.authorsExists(authors)); + } + + @Test + public void referencesFormatTest() { + getContext().goTo(applicationProperties.getBaseUrl() + getPaperPageUrl()); + + PaperPage paperPage = getPaperPage(); + paperPage.setTitle("test"); + paperPage.clickReferenceTab(); + paperPage.clickAddReferenceButton(); + + paperPage.clickReferenceTab(); + paperPage.showFirstReference(); + paperPage.setFirstReferenceAuthors("authors"); + paperPage.setFirstReferencePublicationTitle("title"); + paperPage.setFirstReferencePublicationYear("2010"); + paperPage.setFirstReferencePublisher("publisher"); + paperPage.setFirstReferencePages("200"); + paperPage.setFirstReferenceJournalOrCollectionTitle("journal"); + paperPage.setFormatStandardSpringer(); + paperPage.clickFormatButton(); + + Assert.assertEquals("authors (2010) title. journal, publisher, pp 200", paperPage.getFormatString()); + } + + @Test + public void dashboardLinkTest() { + getContext().goTo(applicationProperties.getBaseUrl() + getPaperPageUrl()); + PaperPage paperPage = getPaperPage(); + + fillRequiredFields(paperPage, "test " + (String.valueOf(System.currentTimeMillis()))); + String testLink = "http://test.com/"; + paperPage.setUrl(testLink); + paperPage.clickSaveBtn(); + + getContext().goTo(applicationProperties.getBaseUrl() + getPapersDashboardPageUrl()); + PapersDashboardPage papersDashboardPage = getPapersDashboardPage(); + + Assert.assertTrue(papersDashboardPage.externalLinkExists(testLink)); + } + + @Test + public void deletePaperTest() { + createNewPaper(); + getContext().goTo(applicationProperties.getBaseUrl() + getPapersPageUrl()); + PapersPage papersPage = getPapersPage(); + + int size = papersPage.getPapersCount(); + papersPage.clickRemoveFirstPaperButton(); + papersPage.clickConfirmDeleteButton(); + + Assert.assertEquals(size - 1, papersPage.getPapersCount()); + } + + @Test + public void latexValidationTest() { + getContext().goTo(applicationProperties.getBaseUrl() + getPaperPageUrl()); + + PaperPage paperPage = getPaperPage(); + paperPage.setTitle("test"); + paperPage.clickLatexTab(); + paperPage.setLatexText("test"); + paperPage.clickPdfButton(); + + Assert.assertTrue(paperPage.dangerMessageExist("Ошибка при создании PDF")); + } + + @Test + public void titleValidationTest() { + getContext().goTo(applicationProperties.getBaseUrl() + getPaperPageUrl()); + PaperPage paperPage = getPaperPage(); + + paperPage.clickSaveBtn(); + + Assert.assertTrue(paperPage.hasAlert("не может быть пусто")); + } + +} diff --git a/src/test/java/core/PageObject.java b/src/test/java/core/PageObject.java index f3e5cb8..e8eae6e 100644 --- a/src/test/java/core/PageObject.java +++ b/src/test/java/core/PageObject.java @@ -2,16 +2,24 @@ package core; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.PageFactory; +import org.openqa.selenium.support.ui.WebDriverWait; public abstract class PageObject { protected WebDriver driver; protected JavascriptExecutor js; + protected WebDriverWait waiter; public abstract String getSubTitle(); public PageObject setDriver(WebDriver driver) { this.driver = driver; js = (JavascriptExecutor) driver; + waiter = new WebDriverWait(driver, 10); return this; } + + public void initElements() { + PageFactory.initElements(driver, this); + } } diff --git a/src/test/java/paper/PaperPage.java b/src/test/java/paper/PaperPage.java index 9c3c357..908f8a1 100644 --- a/src/test/java/paper/PaperPage.java +++ b/src/test/java/paper/PaperPage.java @@ -2,10 +2,216 @@ package paper; import core.PageObject; import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.Select; + +import java.util.List; public class PaperPage extends PageObject { + @FindBy(id = "title") + private WebElement titleInput; + + @FindBy(id = "sendMessageButton") + private WebElement sendMessageButton; + + @FindBy(id = "id") + private WebElement idInput; + + @FindBy(css = "#messages .alert-danger span") + private WebElement dangerMessage; + + @FindBy(className = "deadline") + private List deadlines; + + @FindBy(className = "deadline-date") + private List deadlineDates; + + @FindBy(className = "deadline-desc") + private List deadlineDescs; + + @FindBy(css = ".alert.alert-danger") + private List dangerAlerts; + + @FindBy(className = "collapse-heading") + private WebElement firstCollapsedLink; + + @FindBy(id = "nav-references-tab") + private WebElement referenceTab; + + @FindBy(id = "nav-latex-tab") + private WebElement latexTab; + + @FindBy(id = "latex-text") + private WebElement latexTextarea; + + @FindBy(id = "addReference") + private WebElement addReferenceButton; + + @FindBy(css = "input.author ") + private WebElement firstAuthorInput; + + @FindBy(css = "input.publicationTitle") + private WebElement firstPublicationTitleInput; + + @FindBy(css = "input.publicationYear") + private WebElement firstPublicationYearInput; + + @FindBy(css = "input.publisher") + private WebElement firstPublisherInput; + + @FindBy(css = "input.pages") + private WebElement firstPagesInput; + + @FindBy(css = "input.journalOrCollectionTitle") + private WebElement firstJournalOrCollectionTitleInput; + + @FindBy(id = "formatBtn") + private WebElement formatButton; + + @FindBy(id = "formattedReferencesArea") + private WebElement formatArea; + + @FindBy(id = "url") + private WebElement urlInput; + + @FindBy(id = "pdfBtn") + private WebElement pdfButton; + + @FindBy(css = "input.author ") + private List authorInputs; + public String getSubTitle() { return driver.findElement(By.tagName("h2")).getText(); } + + public void clickReferenceTab() { + js.executeScript("document.getElementById('nav-references-tab').scrollIntoView(false);"); + referenceTab.click(); + } + + public void clickLatexTab() { + latexTab.click(); + } + + public void showFirstReference() { + waiter.until(ExpectedConditions.elementToBeClickable(firstCollapsedLink)); + firstCollapsedLink.click(); + } + + public void clickAddReferenceButton() { + js.executeScript("arguments[0].click()", addReferenceButton); + } + + public void clickFormatButton() { + formatButton.click(); + } + + public void clickPdfButton() { + pdfButton.click(); + } + + public void setTitle(String title) { + titleInput.clear(); + titleInput.sendKeys(title); + } + + public void setLatexText(String text) { + waiter.until(ExpectedConditions.visibilityOf(latexTextarea)); + latexTextarea.clear(); + latexTextarea.sendKeys(text); + } + + public void setFirstReferenceAuthors(String authors) { + waiter.until(ExpectedConditions.visibilityOf(firstAuthorInput)); + + firstAuthorInput.clear(); + firstAuthorInput.sendKeys(authors); + } + + public void setFirstReferencePublicationTitle(String title) { + firstPublicationTitleInput.clear(); + firstPublicationTitleInput.sendKeys(title); + } + + public void setFirstReferencePublicationYear(String year) { + firstPublicationYearInput.clear(); + firstPublicationYearInput.sendKeys(year); + } + + public void setFirstReferencePublisher(String publisher) { + firstPublisherInput.clear(); + firstPublisherInput.sendKeys(publisher); + } + + public void setFirstReferencePages(String pages) { + firstPagesInput.clear(); + firstPagesInput.sendKeys(pages); + } + + public void setFirstReferenceJournalOrCollectionTitle(String journal) { + firstJournalOrCollectionTitleInput.clear(); + firstJournalOrCollectionTitleInput.sendKeys(journal); + } + + public void setUrl(String url) { + urlInput.clear(); + urlInput.sendKeys(url); + } + + public void setFormatStandardSpringer() { + Select standards = new Select(driver.findElement(By.id("formatStandard"))); + standards.selectByValue("SPRINGER"); + } + + public void setDeadlineDate(Integer deadlineNumber, String date) { + deadlineDates.get(deadlineNumber - 1).sendKeys(date); + } + + public void setDeadlineDescription(Integer deadlineNumber, String desc) { + deadlineDescs.get(deadlineNumber - 1).clear(); + deadlineDescs.get(deadlineNumber - 1).sendKeys(desc); + } + + public boolean hasAlert(String alertMessage) { + return dangerAlerts + .stream() + .anyMatch( + webElement -> webElement.getText().contains(alertMessage)); + } + + public void clickSaveBtn() { + sendMessageButton.click(); + } + + public String getId() { + return idInput.getAttribute("value"); + } + + public String getFormatString() { + waiter.until(ExpectedConditions.attributeToBeNotEmpty(formatArea, "value")); + return formatArea.getAttribute("value"); + } + + public boolean deadlineExist(String desc, String date) { + return deadlines + .stream() + .anyMatch( + webElement -> webElement.findElement(By.className("deadline-desc")).getAttribute("value").equals(desc) + && webElement.findElement(By.className("deadline-date")).getAttribute("value").equals(date)); + } + + public boolean authorsExists(String authors) { + return authorInputs + .stream() + .anyMatch( + webElement -> webElement.getAttribute("value").equals(authors)); + } + + public boolean dangerMessageExist(String message) { + waiter.until(ExpectedConditions.visibilityOf(dangerMessage)); + return dangerMessage.getText().equals(message); + } } diff --git a/src/test/java/paper/PapersDashboardPage.java b/src/test/java/paper/PapersDashboardPage.java index 51d7cb8..4567490 100644 --- a/src/test/java/paper/PapersDashboardPage.java +++ b/src/test/java/paper/PapersDashboardPage.java @@ -2,10 +2,23 @@ package paper; import core.PageObject; import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; + +import java.util.List; public class PapersDashboardPage extends PageObject { + @FindBy(className = "externalLink") + private List externalLinks; public String getSubTitle() { return driver.findElement(By.tagName("h2")).getText(); } + + public boolean externalLinkExists(String link) { + return externalLinks + .stream() + .anyMatch( + webElement -> webElement.getAttribute("href").equals(link)); + } } diff --git a/src/test/java/paper/PapersPage.java b/src/test/java/paper/PapersPage.java index f191d9b..620d0c0 100644 --- a/src/test/java/paper/PapersPage.java +++ b/src/test/java/paper/PapersPage.java @@ -2,10 +2,60 @@ package paper; import core.PageObject; import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.ui.ExpectedConditions; + +import java.util.List; public class PapersPage extends PageObject { + @FindBy(css = ".paper-row .h6") + private List paperTitles; + + @FindBy(className = "paper-row") + private List paperItems; + + @FindBy(className = "remove-paper") + private WebElement removeFirstPaperButton; + + @FindBy(id = "dataConfirmOK") + private WebElement confirmDeleteButton; + + @FindBy(id = "addDeadline") + private WebElement addDeadlineButton; + + @FindBy(css = ".paper-row a:nth-child(2)") + private WebElement firstPaper; public String getSubTitle() { return driver.findElement(By.tagName("h2")).getText(); } + + public void clickFirstPaper() { + firstPaper.click(); + } + + public void clickAddDeadline() { + addDeadlineButton.click(); + } + + public void clickRemoveFirstPaperButton() { + js.executeScript("arguments[0].click()", removeFirstPaperButton); + } + + public void clickConfirmDeleteButton() { + waiter.until(ExpectedConditions.visibilityOf(confirmDeleteButton)); + confirmDeleteButton.click(); + } + + public boolean havePaperWithTitle(String title) { + return paperTitles + .stream() + .anyMatch(webElement -> webElement.getText().equals(title)); + } + + public int getPapersCount() { + return paperItems.size(); + } + }