From c21739bfc627b1aad3de8edbec7e4ad7d5d8a0dc Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Wed, 22 May 2019 15:12:45 +0400 Subject: [PATCH] #120 parse kias page --- src/test/java/IndexKiasTest.java | 67 +++++++++++++++++++++++++++++++ src/test/java/grant/KiasPage.java | 28 ++++++++++++- 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 src/test/java/IndexKiasTest.java diff --git a/src/test/java/IndexKiasTest.java b/src/test/java/IndexKiasTest.java new file mode 100644 index 0000000..73dc646 --- /dev/null +++ b/src/test/java/IndexKiasTest.java @@ -0,0 +1,67 @@ +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> navigationHolder = ImmutableMap.of( + new KiasPage(), Arrays.asList("Поиск по конкурсам", + String.format(BASE_URL, CONTEST_STATUS_ID, CONTEST_TYPE, CONTEST_YEAR)) + ); + + @Autowired + UserRepository userRepository; + + public List getNewGrantsDto() throws ParseException { + Map.Entry> page = Iterables.get(navigationHolder.entrySet(), 0); + getContext().goTo(page.getValue().get(1)); + KiasPage kiasPage = (KiasPage) getContext().initPage(page.getKey()); + List kiasGrants = new ArrayList<>(); + List 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; + } +} diff --git a/src/test/java/grant/KiasPage.java b/src/test/java/grant/KiasPage.java index 49b40d9..7723b01 100644 --- a/src/test/java/grant/KiasPage.java +++ b/src/test/java/grant/KiasPage.java @@ -4,7 +4,9 @@ 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 @@ -13,9 +15,31 @@ public class KiasPage extends PageObject { } public List getGrants() { - WebElement listContest = driver.findElement(By.tagName("tBody")); - return listContest.findElements(By.cssSelector("tr.tr")); + List grants = new ArrayList<>(); + do { + grants.addAll(getPageOfGrants()); + } + while (checkPagination()); + return grants; + } + + public List getPageOfGrants() { + WebElement listContest = driver.findElement(By.tagName("tBody")); + List 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) {