diff --git a/src/test/java/IndexGrantTest.java b/src/test/java/GrantTest.java similarity index 68% rename from src/test/java/IndexGrantTest.java rename to src/test/java/GrantTest.java index e1560a9..b824852 100644 --- a/src/test/java/IndexGrantTest.java +++ b/src/test/java/GrantTest.java @@ -10,6 +10,7 @@ import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; +import org.openqa.selenium.WebElement; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @@ -24,7 +25,7 @@ import java.util.Map; @RunWith(SpringRunner.class) @FixMethodOrder(MethodSorters.NAME_ASCENDING) @SpringBootTest(classes = NgTrackerApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) -public class IndexGrantTest extends TestTemplate { +public class GrantTest extends TestTemplate { private final Map> navigationHolder = ImmutableMap.of( new GrantsPage(), Arrays.asList("ГРАНТЫ", "/grants/grants"), new GrantPage(), Arrays.asList("РЕДАКТИРОВАНИЕ ГРАНТА", "/grants/grant?id=0"), @@ -35,7 +36,7 @@ public class IndexGrantTest extends TestTemplate { private ApplicationProperties applicationProperties; @Test - public void createNewGrant() { + public void aCreateNewGrant() { Map.Entry> page = Iterables.get(navigationHolder.entrySet(), 1); getContext().goTo(applicationProperties.getBaseUrl() + page.getValue().get(1)); GrantPage grantPage = (GrantPage) getContext().initPage(page.getKey()); @@ -53,7 +54,7 @@ public class IndexGrantTest extends TestTemplate { } @Test - public void createBlankGrant() { + public void bCreateBlankGrant() { Map.Entry> page = Iterables.get(navigationHolder.entrySet(), 1); getContext().goTo(applicationProperties.getBaseUrl() + page.getValue().get(1)); GrantPage grantPage = (GrantPage) getContext().initPage(page.getKey()); @@ -64,7 +65,7 @@ public class IndexGrantTest extends TestTemplate { } @Test - public void updateGrantTitle() { + public void cUpdateGrantTitle() { Map.Entry> page = Iterables.get(navigationHolder.entrySet(), 0); getContext().goTo(applicationProperties.getBaseUrl() + page.getValue().get(1)); GrantsPage grantsPage = (GrantsPage) getContext().initPage(page.getKey()); @@ -79,18 +80,7 @@ public class IndexGrantTest extends TestTemplate { } @Test - public void deleteGrant() throws InterruptedException { - Map.Entry> page = Iterables.get(navigationHolder.entrySet(), 0); - getContext().goTo(applicationProperties.getBaseUrl() + page.getValue().get(1)); - GrantsPage grantsPage = (GrantsPage) getContext().initPage(page.getKey()); - - Integer size = grantsPage.getGrantsList().size(); - grantsPage.deleteFirst(); - Assert.assertEquals(size - 1, grantsPage.getGrantsList().size()); - } - - @Test - public void attachPaper() { + public void dAttachPaper() { Map.Entry> page = Iterables.get(navigationHolder.entrySet(), 0); getContext().goTo(applicationProperties.getBaseUrl() + page.getValue().get(1)); GrantsPage grantsPage = (GrantsPage) getContext().initPage(page.getKey()); @@ -103,7 +93,7 @@ public class IndexGrantTest extends TestTemplate { } @Test - public void deletePaper() { + public void eDeletePaper() { Map.Entry> page = Iterables.get(navigationHolder.entrySet(), 0); getContext().goTo(applicationProperties.getBaseUrl() + page.getValue().get(1)); GrantsPage grantsPage = (GrantsPage) getContext().initPage(page.getKey()); @@ -119,7 +109,7 @@ public class IndexGrantTest extends TestTemplate { } @Test - public void addDeadline() { + public void fAddDeadline() { Map.Entry> page = Iterables.get(navigationHolder.entrySet(), 0); getContext().goTo(applicationProperties.getBaseUrl() + page.getValue().get(1)); GrantsPage grantsPage = (GrantsPage) getContext().initPage(page.getKey()); @@ -130,8 +120,8 @@ public class IndexGrantTest extends TestTemplate { Integer deadlineCount = grantPage.getDeadlineCount(); String description = "deadline test"; - String date = "09.09.2019"; - String dateValue = "2019-09-09"; + String date = "08.08.2019"; + String dateValue = "2019-08-08"; grantPage.addDeadline(); grantPage.setDeadline(date, deadlineCount, description); grantPage.saveGrant(); @@ -142,7 +132,7 @@ public class IndexGrantTest extends TestTemplate { } @Test - public void deleteDeadline() { + public void gDeleteDeadline() { Map.Entry> page = Iterables.get(navigationHolder.entrySet(), 0); getContext().goTo(applicationProperties.getBaseUrl() + page.getValue().get(1)); GrantsPage grantsPage = (GrantsPage) getContext().initPage(page.getKey()); @@ -152,16 +142,6 @@ public class IndexGrantTest extends TestTemplate { String grantId = grantPage.getId(); Integer deadlineCount = grantPage.getDeadlineCount(); - String description = "deadline test"; - String date = "09.09.2019"; - String dateValue = "2019-09-09"; - grantPage.addDeadline(); - grantPage.setDeadline(date, deadlineCount, description); - grantPage.saveGrant(); - - getContext().goTo(applicationProperties.getBaseUrl() + String.format("/grants/grant?id=%s", grantId)); - deadlineCount = grantPage.getDeadlineCount(); - grantPage.deleteDeadline(); grantPage.saveGrant(); @@ -169,4 +149,77 @@ public class IndexGrantTest extends TestTemplate { Integer newDeadlineCount = grantPage.getDeadlineCount(); Assert.assertEquals(deadlineCount - 1, (int) newDeadlineCount); } + + @Test + public void hAddAuthor() { + Map.Entry> page = Iterables.get(navigationHolder.entrySet(), 0); + getContext().goTo(applicationProperties.getBaseUrl() + page.getValue().get(1)); + GrantsPage grantsPage = (GrantsPage) getContext().initPage(page.getKey()); + GrantPage grantPage = (GrantPage) getContext().initPage(Iterables.get(navigationHolder.entrySet(), 1).getKey()); + + WebElement grant = grantsPage.getFirstGrantWithoutClick(); + String grantTitle = grantsPage.getGrantTitle(grant); + Integer authorsCount = grantsPage.getAuthorsCount(grant); + + grantsPage.getFirstGrant(); + grantPage.addAuthor(); + grantPage.saveGrant(); + + grant = grantsPage.getGrantByTitle(grantTitle); + Integer newAuthorsCount = grantsPage.getAuthorsCount(grant); + + Assert.assertEquals(authorsCount + 1, (int) newAuthorsCount); + } + + @Test + public void iDeleteAuthor() { + Map.Entry> page = Iterables.get(navigationHolder.entrySet(), 0); + getContext().goTo(applicationProperties.getBaseUrl() + page.getValue().get(1)); + GrantsPage grantsPage = (GrantsPage) getContext().initPage(page.getKey()); + GrantPage grantPage = (GrantPage) getContext().initPage(Iterables.get(navigationHolder.entrySet(), 1).getKey()); + + WebElement grant = grantsPage.getFirstGrantWithoutClick(); + String grantTitle = grantsPage.getGrantTitle(grant); + Integer authorsCount = grantsPage.getAuthorsCount(grant); + + grantsPage.getFirstGrant(); + grantPage.deleteAuthor(); + grantPage.saveGrant(); + + grant = grantsPage.getGrantByTitle(grantTitle); + Integer newAuthorsCount = grantsPage.getAuthorsCount(grant); + + authorsCount = (authorsCount == 0) ? 0 : authorsCount - 1; + + Assert.assertEquals((int) authorsCount, (int) newAuthorsCount); + } + + @Test + public void jUpdateGrantDescription() { + Map.Entry> page = Iterables.get(navigationHolder.entrySet(), 0); + getContext().goTo(applicationProperties.getBaseUrl() + page.getValue().get(1)); + GrantsPage grantsPage = (GrantsPage) getContext().initPage(page.getKey()); + GrantPage grantPage = (GrantPage) getContext().initPage(Iterables.get(navigationHolder.entrySet(), 1).getKey()); + + String description = "newDescriptionForGrant"; + grantsPage.getFirstGrant(); + String grantId = grantPage.getId(); + grantPage.setDescription(description); + grantPage.saveGrant(); + + getContext().goTo(applicationProperties.getBaseUrl() + String.format("/grants/grant?id=%s", grantId)); + + Assert.assertTrue(description.equals(grantPage.getDescription())); + } + + @Test + public void kDeleteGrant() throws InterruptedException { + Map.Entry> page = Iterables.get(navigationHolder.entrySet(), 0); + getContext().goTo(applicationProperties.getBaseUrl() + page.getValue().get(1)); + GrantsPage grantsPage = (GrantsPage) getContext().initPage(page.getKey()); + + Integer size = grantsPage.getGrantsList().size(); + grantsPage.deleteFirst(); + Assert.assertEquals(size - 1, grantsPage.getGrantsList().size()); + } } diff --git a/src/test/java/grant/GrantPage.java b/src/test/java/grant/GrantPage.java index 9fd1986..fe13956 100644 --- a/src/test/java/grant/GrantPage.java +++ b/src/test/java/grant/GrantPage.java @@ -42,10 +42,7 @@ public class GrantPage extends PageObject { } public boolean checkBlankFields() { - if (driver.findElements(By.className("alert-danger")).size() > 0) { - return true; - } - return false; + return driver.findElements(By.className("alert-danger")).size() > 0; } public List getAttachedPapers() { @@ -109,4 +106,44 @@ public class GrantPage extends PageObject { public void deleteDeadline() { driver.findElements(By.className("btn-delete-deadline")).get(0).click(); } + + public List addAuthor() { + WebElement selectAuthors = driver.findElement(By.id("authors")); + Select select = new Select(selectAuthors); + List selectedOptions = select.getAllSelectedOptions(); + List allOptions = select.getOptions(); + int i = 0; + while (i < selectedOptions.size()) { + if (!allOptions.get(i).equals(selectedOptions.get(i))) { + select.selectByVisibleText(allOptions.get(i).getText()); + selectedOptions.add(allOptions.get(i)); + return selectedOptions; + } else { + i++; + } + } + if (selectedOptions.size() != allOptions.size()) { + select.selectByVisibleText(allOptions.get(i).getText()); + selectedOptions.add(allOptions.get(i)); + } + return selectedOptions; + } + + public void deleteAuthor() { + WebElement selectAuthors = driver.findElement(By.id("authors")); + Select select = new Select(selectAuthors); + List selectedOptions = select.getAllSelectedOptions(); + if (selectedOptions.size() != 0) { + select.deselectByVisibleText(selectedOptions.get(0).getText()); + } + } + + public void setDescription(String description) { + driver.findElement(By.id("comment")).clear(); + driver.findElement(By.id("comment")).sendKeys(description); + } + + public String getDescription() { + return driver.findElement(By.id("comment")).getText(); + } } diff --git a/src/test/java/grant/GrantsPage.java b/src/test/java/grant/GrantsPage.java index 68f50db..33c388d 100644 --- a/src/test/java/grant/GrantsPage.java +++ b/src/test/java/grant/GrantsPage.java @@ -13,12 +13,13 @@ public class GrantsPage extends PageObject { } public List getGrantsList() { - return driver.findElements(By.cssSelector("span.h6")); + return driver.findElements(By.className("grant-row")); } public boolean findGrantByTitle(String grantTitle) { return getGrantsList() .stream() + .map(el -> el.findElement(By.cssSelector("span.h6"))) .anyMatch(webElement -> webElement.getText().equals(grantTitle)); } @@ -35,4 +36,31 @@ public class GrantsPage extends PageObject { public void getFirstGrant() { driver.findElement(By.xpath("//*[@id=\"grants\"]/div/div[2]/div[1]/div[1]/div/a[1]")).click(); } + + public WebElement getFirstGrantWithoutClick() { + return driver.findElement(By.xpath("//*[@id=\"grants\"]/div/div[2]/div[1]/div[1]")); + } + + public String getGrantTitle(WebElement webElement) { + return webElement.findElement(By.cssSelector("span.h6")).getText(); + } + + public WebElement getGrantByTitle(String title) { + List list = getGrantsList(); + for (int i = 0; i < list.size(); i++) { + if (getGrantTitle(list.get(i)).equals(title)) { + return list.get(i); + } + } + return null; + } + + public Integer getAuthorsCount(WebElement webElement) { + String authors = webElement.findElement(By.className("text-muted")).getText(); + if (!authors.equals("")) { + String[] mas = authors.split(","); + return mas.length; + } + return 0; + } }