#120 move to service

This commit is contained in:
Anton Romanov 2019-05-25 15:48:32 +04:00
parent 59d6edb818
commit 477a9fd6ff
2 changed files with 26 additions and 26 deletions

View File

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

View File

@ -1,6 +1,7 @@
package ru.ulstu.grant.service; package ru.ulstu.grant.service;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -37,13 +38,33 @@ public class KiasService {
List<GrantDto> grants = new ArrayList<>(); List<GrantDto> grants = new ArrayList<>();
for (Integer year : generateGrantYears()) { for (Integer year : generateGrantYears()) {
webDriver.get(String.format(BASE_URL, CONTEST_STATUS_ID, CONTEST_TYPE, year)); webDriver.get(String.format(BASE_URL, CONTEST_STATUS_ID, CONTEST_TYPE, year));
grants.addAll(new KiasPage(webDriver).getKiasGrants()); grants.addAll(getKiasGrants(webDriver));
} }
grants.forEach(grantDto -> grantDto.setLeaderId(leaderId)); grants.forEach(grantDto -> grantDto.setLeaderId(leaderId));
webDriver.quit(); webDriver.quit();
return grants; return grants;
} }
public List<GrantDto> getKiasGrants(WebDriver webDriver) throws ParseException {
List<GrantDto> newGrants = new ArrayList<>();
KiasPage kiasPage = new KiasPage(webDriver);
do {
newGrants.addAll(getGrantsFromPage(kiasPage));
} while (kiasPage.goToNextPage()); //проверка существования следующей страницы с грантами
return newGrants;
}
private List<GrantDto> getGrantsFromPage(KiasPage kiasPage) throws ParseException {
List<GrantDto> grants = new ArrayList<>();
for (WebElement grantElement : kiasPage.getPageOfGrants()) {
GrantDto grantDto = new GrantDto(
kiasPage.getGrantTitle(grantElement),
kiasPage.parseDeadLineDate(grantElement));
grants.add(grantDto);
}
return grants;
}
private List<Integer> generateGrantYears() { private List<Integer> generateGrantYears() {
return Arrays.asList(Calendar.getInstance().get(Calendar.YEAR), return Arrays.asList(Calendar.getInstance().get(Calendar.YEAR),
Calendar.getInstance().get(Calendar.YEAR) + 1); Calendar.getInstance().get(Calendar.YEAR) + 1);