This commit is contained in:
Anton Romanov 2019-05-25 15:42:08 +04:00
parent 837a9f465c
commit 59d6edb818
5 changed files with 63 additions and 144 deletions

View File

@ -12,6 +12,7 @@ import ru.ulstu.project.model.ProjectDto;
import ru.ulstu.user.model.UserDto;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@ -96,6 +97,12 @@ public class GrantDto extends NameContainer {
this.papers = convert(grant.getPapers(), PaperDto::new);
}
public GrantDto(String grantTitle, Date deadLineDate) {
this.title = grantTitle;
deadlines.add(new Deadline(deadLineDate, "Окончание приёма заявок"));
status = Grant.GrantStatus.LOADED_FROM_KIAS;
}
public Integer getId() {
return id;
}

View File

@ -3,34 +3,31 @@ package ru.ulstu.grant.page;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import ru.ulstu.grant.model.GrantDto;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.NoSuchElementException;
public class KiasPage {
private WebDriver driver;
public KiasPage(WebDriver webDriver) {
this.driver = webDriver;
}
public List<WebElement> getGrants() {
List<WebElement> grants = new ArrayList<>();
public List<GrantDto> getKiasGrants() throws ParseException {
List<GrantDto> newGrants = new ArrayList<>();
do {
grants.addAll(getPageOfGrants());
}
while (checkPagination());
return grants;
newGrants.addAll(getGrantsFromPage());
} while (goToNextPage()); //проверка существования следующей страницы с грантами
return newGrants;
}
public List<WebElement> getPageOfGrants() {
WebElement listContest = driver.findElement(By.tagName("tBody"));
List<WebElement> grants = listContest.findElements(By.cssSelector("tr.tr"));
return grants;
}
public boolean checkPagination() {
private boolean goToNextPage() {
try {
if (driver.findElements(By.id("js-ctrlNext")).size() > 0) {
driver.findElement(By.id("js-ctrlNext")).click();
@ -42,11 +39,34 @@ public class KiasPage {
return false;
}
public String getGrantTitle(WebElement grant) {
private List<WebElement> getPageOfGrants() {
WebElement listContest = driver.findElement(By.tagName("tBody"));
List<WebElement> grants = listContest.findElements(By.cssSelector("tr.tr"));
return grants;
}
private String getGrantTitle(WebElement grant) {
return grant.findElement(By.cssSelector("td.tertiary")).findElement(By.tagName("a")).getText();
}
public String getFirstDeadline(WebElement grant) {
return grant.findElement(By.xpath("./td[5]")).getText();
private List<GrantDto> getGrantsFromPage() throws ParseException {
List<GrantDto> grants = new ArrayList<>();
for (WebElement grantElement : getPageOfGrants()) {
GrantDto grantDto = new GrantDto(
getGrantTitle(grantElement),
parseDeadLineDate(grantElement));
grants.add(grantDto);
}
return grants;
}
private Date parseDeadLineDate(WebElement grantElement) throws ParseException {
String deadlineDate = getFirstDeadline(grantElement); //10.06.2019 23:59
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm");
return formatter.parse(deadlineDate);
}
private String getFirstDeadline(WebElement grantElement) {
return grantElement.findElement(By.xpath("./td[5]")).getText();
}
}

View File

@ -1,22 +1,17 @@
package ru.ulstu.grant.service;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.stereotype.Service;
import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.grant.model.Grant;
import ru.ulstu.grant.model.GrantDto;
import ru.ulstu.grant.page.KiasPage;
import ru.ulstu.user.service.UserService;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Service
@ -24,7 +19,6 @@ public class KiasService {
private final static String BASE_URL = "https://www.rfbr.ru/rffi/ru/contest_search?CONTEST_STATUS_ID=%s&CONTEST_TYPE=%s&CONTEST_YEAR=%s";
private final static String CONTEST_STATUS_ID = "1";
private final static String CONTEST_TYPE = "-1";
private final static int CONTEST_YEAR = Calendar.getInstance().get(Calendar.YEAR);
private final static String DRIVER_LOCATION = "drivers/%s";
private final static String WINDOWS_DRIVER = "chromedriver.exe";
@ -38,33 +32,28 @@ public class KiasService {
}
public List<GrantDto> getNewGrantsDto() throws ParseException {
WebDriver webDriver = getDriver();
Integer leaderId = userService.findOneByLoginIgnoreCase("admin").getId();
List<GrantDto> grants = new ArrayList<>();
for (Integer year : generateGrantYears()) {
webDriver.get(String.format(BASE_URL, CONTEST_STATUS_ID, CONTEST_TYPE, year));
grants.addAll(new KiasPage(webDriver).getKiasGrants());
}
grants.forEach(grantDto -> grantDto.setLeaderId(leaderId));
webDriver.quit();
return grants;
}
private List<Integer> generateGrantYears() {
return Arrays.asList(Calendar.getInstance().get(Calendar.YEAR),
Calendar.getInstance().get(Calendar.YEAR) + 1);
}
private WebDriver getDriver() {
System.setProperty(DRIVER_TYPE, getDriverExecutablePath());
final ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
WebDriver webDriver = new ChromeDriver(chromeOptions);
webDriver.get(String.format(BASE_URL, CONTEST_STATUS_ID, CONTEST_TYPE, CONTEST_YEAR));
KiasPage kiasPage = new KiasPage(webDriver);
List<WebElement> kiasGrants = new ArrayList<>();
List<GrantDto> newGrants = new ArrayList<>();
do {
kiasGrants.addAll(kiasPage.getPageOfGrants());
for (WebElement grant : kiasGrants) {
GrantDto grantDto = new GrantDto();
grantDto.setTitle(kiasPage.getGrantTitle(grant));
String deadlineDate = kiasPage.getFirstDeadline(grant); //10.06.2019 23:59
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm");
Date date = formatter.parse(deadlineDate);
Deadline deadline = new Deadline(date, "Окончание приёма заявок");
grantDto.setDeadlines(Arrays.asList(deadline));
grantDto.setLeaderId(userService.findOneByLoginIgnoreCase("admin").getId());
grantDto.setStatus(Grant.GrantStatus.LOADED_FROM_KIAS);
newGrants.add(grantDto);
}
kiasGrants.clear();
}
while (kiasPage.checkPagination()); //проверка существования следующей страницы с грантами
webDriver.quit();
return newGrants;
return new ChromeDriver(chromeOptions);
}
private String getDriverExecutablePath() {

View File

@ -1,67 +1,11 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import core.PageObject;
import core.TestTemplate;
import grant.KiasPage;
import org.junit.runner.RunWith;
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;
import ru.ulstu.NgTrackerApplication;
import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.grant.model.Grant;
import ru.ulstu.grant.model.GrantDto;
import ru.ulstu.user.repository.UserRepository;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = NgTrackerApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class IndexKiasTest extends TestTemplate {
private final static String BASE_URL = "https://www.rfbr.ru/rffi/ru/contest_search?CONTEST_STATUS_ID=%s&CONTEST_TYPE=%s&CONTEST_YEAR=%s";
private final static String CONTEST_STATUS_ID = "1";
private final static String CONTEST_TYPE = "-1";
private final static String CONTEST_YEAR = "2019";
private final Map<PageObject, List<String>> navigationHolder = ImmutableMap.of(
new KiasPage(), Arrays.asList("Поиск по конкурсам",
String.format(BASE_URL, CONTEST_STATUS_ID, CONTEST_TYPE, CONTEST_YEAR))
);
@Autowired
UserRepository userRepository;
public List<GrantDto> getNewGrantsDto() throws ParseException {
Map.Entry<PageObject, List<String>> page = Iterables.get(navigationHolder.entrySet(), 0);
getContext().goTo(page.getValue().get(1));
KiasPage kiasPage = (KiasPage) getContext().initPage(page.getKey());
List<WebElement> kiasGrants = new ArrayList<>();
List<GrantDto> newGrants = new ArrayList<>();
do {
kiasGrants.addAll(kiasPage.getPageOfGrants());
for (WebElement grant : kiasGrants) {
GrantDto grantDto = new GrantDto();
grantDto.setTitle(kiasPage.getGrantTitle(grant));
String deadlineDate = kiasPage.getFirstDeadline(grant); //10.06.2019 23:59
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm");
Date date = formatter.parse(deadlineDate);
Deadline deadline = new Deadline(date, "Окончание приёма заявок");
grantDto.setDeadlines(Arrays.asList(deadline));
grantDto.setLeaderId(userRepository.findOneByLoginIgnoreCase("admin").getId());
grantDto.setStatus(Grant.GrantStatus.LOADED_FROM_KIAS);
newGrants.add(grantDto);
}
kiasGrants.clear();
}
while (kiasPage.checkPagination()); //проверка существования следующей страницы с грантами
return newGrants;
}
}

View File

@ -2,51 +2,10 @@ package grant;
import core.PageObject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
public class KiasPage extends PageObject {
@Override
public String getSubTitle() {
return driver.findElement(By.tagName("h1")).getText();
}
public List<WebElement> getGrants() {
List<WebElement> grants = new ArrayList<>();
do {
grants.addAll(getPageOfGrants());
}
while (checkPagination());
return grants;
}
public List<WebElement> getPageOfGrants() {
WebElement listContest = driver.findElement(By.tagName("tBody"));
List<WebElement> grants = listContest.findElements(By.cssSelector("tr.tr"));
return grants;
}
public boolean checkPagination() {
try {
if (driver.findElements(By.id("js-ctrlNext")).size() > 0) {
driver.findElement(By.id("js-ctrlNext")).click();
return true;
}
} catch (NoSuchElementException e) {
return false;
}
return false;
}
public String getGrantTitle(WebElement grant) {
return grant.findElement(By.cssSelector("td.tertiary")).findElement(By.tagName("a")).getText();
}
public String getFirstDeadline(WebElement grant) {
return grant.findElement(By.xpath("./td[5]")).getText();
}
}