Merge branch '103-paper-integr-tests' into 'dev'

Resolve "Реализовать интеграционные автотесты для проверки модулей"

See merge request romanov73/ng-tracker!111
This commit is contained in:
Anton Romanov 2019-06-05 04:57:33 +00:00
commit 44824e53b2
8 changed files with 537 additions and 6 deletions

View File

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

View File

@ -10,7 +10,8 @@
<span th:replace="papers/fragments/paperStatusFragment :: paperStatus(paperStatus=${paper.status})"/>
</div>
<div class="col col-10 text-right">
<p th:if="${paper.url!=null and paper.url!=''}"><a target="_blank" th:href="${paper.url}"><i
<p th:if="${paper.url!=null and paper.url!=''}"><a target="_blank" class="externalLink"
th:href="${paper.url}"><i
class="fa fa-external-link fa-1x"
aria-hidden="true"></i></a></p>
<p th:unless="${paper.url!=null and paper.url!=''}"><i class="fa fa-fw fa-2x" aria-hidden="true"></i></p>

View File

@ -87,14 +87,16 @@
<div class="form-group">
<label>Дедлайны:</label>
<div class="row" th:each="deadline, rowStat : *{deadlines}">
<div class="row deadline" th:each="deadline, rowStat : *{deadlines}">
<input type="hidden" th:field="*{deadlines[__${rowStat.index}__].id}"/>
<div class="col-6">
<input type="date" class="form-control" name="deadline"
<input type="date" class="form-control deadline-date"
name="deadline"
th:field="*{deadlines[__${rowStat.index}__].date}"/>
</div>
<div class="col-4">
<input class="form-control" type="text" placeholder="Описание"
<input class="form-control deadline-desc" type="text"
placeholder="Описание"
th:field="*{deadlines[__${rowStat.index}__].description}"/>
</div>
<div class="col-2">
@ -234,7 +236,8 @@
</div>
<div class="form-group col-12">
<label class="col-4">Год издания:</label>
<input type="number" class="form-control col-7 "
<input type="number"
class="form-control col-7 publicationYear"
name="publicationYear"
th:field="*{references[__${rowStat.index}__].publicationYear}"/>
</div>
@ -247,7 +250,8 @@
</div>
<div class="form-group col-12">
<label class="col-4">Страницы:</label>
<input type="text" class="form-control col-7" name="pages"
<input type="text" class="form-control col-7 pages"
name="pages"
th:field="*{references[__${rowStat.index}__].pages}"/>
</div>
<div class="form-group col-12">

View File

@ -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<PageObject, List<String>> 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("не может быть пусто"));
}
}

View File

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

View File

@ -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<WebElement> deadlines;
@FindBy(className = "deadline-date")
private List<WebElement> deadlineDates;
@FindBy(className = "deadline-desc")
private List<WebElement> deadlineDescs;
@FindBy(css = ".alert.alert-danger")
private List<WebElement> 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<WebElement> 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);
}
}

View File

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

View File

@ -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<WebElement> paperTitles;
@FindBy(className = "paper-row")
private List<WebElement> 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();
}
}