#103 create tests for create, update and delete functions

This commit is contained in:
T-Midnight 2019-05-22 23:15:04 +04:00
parent 16885cc94f
commit 3e9bbb3584
3 changed files with 160 additions and 2 deletions

View File

@ -0,0 +1,92 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import core.PageObject;
import core.TestTemplate;
import grant.GrantPage;
import grant.GrantsDashboardPage;
import grant.GrantsPage;
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 ru.ulstu.NgTrackerApplication;
import ru.ulstu.configuration.ApplicationProperties;
import java.util.Arrays;
import java.util.Date;
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 IndexGrantTest extends TestTemplate {
private final Map<PageObject, List<String>> navigationHolder = ImmutableMap.of(
new GrantsPage(), Arrays.asList("ГРАНТЫ", "/grants/grants"),
new GrantPage(), Arrays.asList("РЕДАКТИРОВАНИЕ ГРАНТА", "/grants/grant?id=0"),
new GrantsDashboardPage(), Arrays.asList("Гранты", "/grants/dashboard")
);
@Autowired
private ApplicationProperties applicationProperties;
@Test
public void createNewGrant() {
Map.Entry<PageObject, List<String>> page = Iterables.get(navigationHolder.entrySet(), 1);
getContext().goTo(applicationProperties.getBaseUrl() + page.getValue().get(1));
GrantPage grantPage = (GrantPage) getContext().initPage(page.getKey());
GrantsPage grantsPage = (GrantsPage) getContext().initPage(Iterables.get(navigationHolder.entrySet(), 0).getKey());
String newGrantName = "test grant" + (new Date());
grantPage.setTitle(newGrantName);
String deadlineDate = new Date().toString();
String deadlineDescription = "test deadline description";
grantPage.setDeadline(deadlineDate, 0, deadlineDescription);
grantPage.setLeader();
grantPage.saveGrant();
Assert.assertTrue(grantsPage.findGrantByTitle(newGrantName));
}
@Test
public void createBlankGrant() {
Map.Entry<PageObject, List<String>> page = Iterables.get(navigationHolder.entrySet(), 1);
getContext().goTo(applicationProperties.getBaseUrl() + page.getValue().get(1));
GrantPage grantPage = (GrantPage) getContext().initPage(page.getKey());
grantPage.saveGrant();
Assert.assertTrue(grantPage.checkBlankFields());
}
@Test
public void updateGrantTitle() {
Map.Entry<PageObject, List<String>> 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());
grantsPage.getFirstGrant();
String newGrantTitle = "test " + (new Date());
grantPage.setTitle(newGrantTitle);
grantPage.saveGrant();
Assert.assertTrue(grantsPage.findGrantByTitle(newGrantTitle));
}
@Test
public void deleteGrant() throws InterruptedException {
Map.Entry<PageObject, List<String>> 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());
}
}

View File

@ -1,10 +1,48 @@
package grant;
import core.PageObject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
public class GrantPage extends PageObject {
@Override
public String getSubTitle() {
return null;
return driver.findElement(By.tagName("h2")).getText();
}
public String getId() {
return driver.findElement(By.id("id")).getAttribute("value");
}
public void setTitle(String name) {
driver.findElement(By.id("title")).clear();
driver.findElement(By.id("title")).sendKeys(name);
}
public String getTitle() {
return driver.findElement(By.id("title")).getAttribute("value");
}
public void setDeadline(String date, Integer i, String description) {
driver.findElement(By.id(String.format("deadlines%d.date", i))).sendKeys(date);
driver.findElement(By.id(String.format("deadlines%d.description", i))).sendKeys(description);
}
public void setLeader() {
WebElement webElement = driver.findElement(By.id("leaderId"));
Select selectLeader = new Select(webElement);
selectLeader.selectByVisibleText("Романов");
}
public void saveGrant() {
driver.findElement(By.id("sendMessageButton")).click();
}
public boolean checkBlankFields() {
if (driver.findElements(By.className("alert-danger")).size() > 0) {
return true;
}
return false;
}
}

View File

@ -1,10 +1,38 @@
package grant;
import core.PageObject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import java.util.List;
public class GrantsPage extends PageObject {
@Override
public String getSubTitle() {
return null;
return driver.findElement(By.tagName("h2")).getText();
}
public List<WebElement> getGrantsList() {
return driver.findElements(By.cssSelector("span.h6"));
}
public boolean findGrantByTitle(String grantTitle) {
return getGrantsList()
.stream()
.anyMatch(webElement -> webElement.getText().equals(grantTitle));
}
public void deleteFirst() throws InterruptedException {
WebElement findDeleteButton = driver.findElement(By.xpath("//*[@id=\"grants\"]/div/div[2]/div[1]/div[1]/div"));
findDeleteButton.click();
Thread.sleep(3000);
WebElement grant = driver.findElement(By.xpath("//*[@id=\"grants\"]/div/div[2]/div[1]/div[1]/div/a[2]"));
grant.click();
WebElement ok = driver.findElement(By.id("dataConfirmOK"));
ok.click();
}
public void getFirstGrant() {
driver.findElement(By.xpath("//*[@id=\"grants\"]/div/div[2]/div[1]/div[1]/div/a[1]")).click();
}
}