PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/it/net/customware/confluence/plugin/google/calendar/pageobjects/GoogleCalendarViewPage.java

https://bitbucket.org/servicerocket/google-calendar-plugin
Java | 142 lines | 111 code | 28 blank | 3 comment | 0 complexity | b0b3f9f3dd9262a633143d20a385541b MD5 | raw file
  1. package it.net.customware.confluence.plugin.google.calendar.pageobjects;
  2. import com.atlassian.confluence.api.model.content.Content;
  3. import com.atlassian.confluence.webdriver.pageobjects.page.content.ViewPage;
  4. import com.atlassian.pageobjects.elements.ElementBy;
  5. import com.atlassian.pageobjects.elements.PageElement;
  6. import com.atlassian.pageobjects.elements.WebDriverElement;
  7. import org.openqa.selenium.*;
  8. import java.util.List;
  9. import java.util.Optional;
  10. import java.util.Set;
  11. import java.util.stream.Collectors;
  12. import static com.atlassian.pageobjects.elements.query.Poller.by;
  13. import static com.atlassian.pageobjects.elements.query.Poller.waitUntil;
  14. import static java.util.concurrent.TimeUnit.SECONDS;
  15. import static org.apache.commons.lang3.StringUtils.EMPTY;
  16. import static org.hamcrest.core.Is.is;
  17. public class GoogleCalendarViewPage extends ViewPage {
  18. private static final String CALENDAR_EVENTS_SELECTOR = "div.rb-ni";
  19. private static final String CALENDAR_EVENTS_WRAPPER_SELECTOR = "div.rb-n";
  20. private static final String CALENDAR_FRAME_SELECTOR = "iframe[data-macro-name=google-calendar]";
  21. private static final String CALENDAR_WEEK_DAYS_SELECTOR = "span.wk-daylink";
  22. private JavascriptExecutor javascriptExecutor;
  23. private WebDriver webDriver;
  24. @ElementBy(id = "calendarTitle")
  25. private PageElement calendarTitle;
  26. @ElementBy(id = "currentDate1")
  27. private PageElement currentDate;
  28. @ElementBy(cssSelector = CALENDAR_WEEK_DAYS_SELECTOR)
  29. private PageElement firstCalendarDay;
  30. @ElementBy(cssSelector = CALENDAR_EVENTS_SELECTOR)
  31. private PageElement firstCalendarEvent;
  32. @ElementBy(cssSelector = CALENDAR_EVENTS_WRAPPER_SELECTOR)
  33. private PageElement firstCalendarEventWrapper;
  34. @ElementBy(cssSelector = CALENDAR_FRAME_SELECTOR)
  35. private WebDriverElement googleCalendarFrame;
  36. @ElementBy(tagName = "body")
  37. private PageElement googleCalendarFrameBody;
  38. @ElementBy(id = "todayButton1")
  39. private PageElement todayButton;
  40. public GoogleCalendarViewPage(Content content, WebDriver webDriver) {
  41. super(content);
  42. this.webDriver = webDriver;
  43. this.javascriptExecutor = (JavascriptExecutor) webDriver;
  44. }
  45. private static String getFirstStyleProperty(WebElement element) {
  46. Optional<String> stringOptional = Optional.ofNullable(element.getAttribute("style"));
  47. return stringOptional.orElse(EMPTY).split(";")[0];
  48. }
  49. private static String getFirstStyleProperty(PageElement element) {
  50. Optional<String> stringOptional = Optional.ofNullable(element.getAttribute("style"));
  51. return stringOptional.orElse(EMPTY).split(";")[0];
  52. }
  53. public String getCurrentDate() {
  54. switchToGoogleCalendarFrame();
  55. return currentDate.getText();
  56. }
  57. public void setCurrentDate(String date) {
  58. switchToMainWindow();
  59. // In order to assert with the same events and dates even as time passes, a JS executor is used to add a query param
  60. // and force Google Calendar frame to load on the required date. Using the date picker UI is less consistent as
  61. // it would involve many clicks and change with time.
  62. javascriptExecutor.executeScript(
  63. "document.querySelector('" + CALENDAR_FRAME_SELECTOR + "').src += '&dates=" + date + "';");
  64. }
  65. public Dimension getFrameDimensions() {
  66. switchToMainWindow();
  67. return googleCalendarFrame.getSize();
  68. }
  69. public String getCalendarTitle() {
  70. switchToGoogleCalendarFrame();
  71. return calendarTitle.getText();
  72. }
  73. public String getFirstCalendarDay() {
  74. switchToGoogleCalendarFrame();
  75. return firstCalendarDay.getText();
  76. }
  77. public String getBodyBackgroundColor() {
  78. switchToGoogleCalendarFrame();
  79. return getFirstStyleProperty(googleCalendarFrameBody);
  80. }
  81. public String getTodayButtonText() {
  82. switchToGoogleCalendarFrame();
  83. return todayButton.getText();
  84. }
  85. public List<String> getCalendarDays() {
  86. switchToGoogleCalendarFrame();
  87. waitUntil(firstCalendarDay.timed().isPresent(), is(true), by(10, SECONDS));
  88. return webDriver.findElements(By.cssSelector(CALENDAR_WEEK_DAYS_SELECTOR)).stream()
  89. .map(WebElement::getText)
  90. .collect(Collectors.toList());
  91. }
  92. public List<String> getCalendarEvents() {
  93. switchToGoogleCalendarFrame();
  94. waitUntil(firstCalendarEvent.timed().isPresent(), is(true), by(10, SECONDS));
  95. return webDriver.findElements(By.cssSelector(CALENDAR_EVENTS_SELECTOR)).stream()
  96. .map(WebElement::getText)
  97. .collect(Collectors.toList());
  98. }
  99. public Set<String> getCalendarEventsColors() {
  100. switchToGoogleCalendarFrame();
  101. waitUntil(firstCalendarEventWrapper.timed().isPresent(), is(true), by(10, SECONDS));
  102. return webDriver.findElements(By.cssSelector(CALENDAR_EVENTS_WRAPPER_SELECTOR)).stream()
  103. .map(GoogleCalendarViewPage::getFirstStyleProperty)
  104. .collect(Collectors.toSet());
  105. }
  106. private void switchToGoogleCalendarFrame() {
  107. switchToMainWindow();
  108. waitUntil(googleCalendarFrame.timed().isPresent(), is(true), by(20, SECONDS));
  109. webDriver.switchTo().frame(googleCalendarFrame.asWebElement());
  110. }
  111. private void switchToMainWindow() {
  112. webDriver.switchTo().window("");
  113. }
  114. }