PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/atlassian-aui-pageobjects/src/main/java/com/atlassian/pageobjects/aui/component/AuiDatePickerCalendar.java

https://bitbucket.org/versionzero/aui
Java | 249 lines | 163 code | 42 blank | 44 comment | 7 complexity | a5f5d0b6e8d90da37fab0157026931d3 MD5 | raw file
  1. package com.atlassian.pageobjects.aui.component;
  2. import com.atlassian.pageobjects.PageBinder;
  3. import com.atlassian.pageobjects.binder.Init;
  4. import com.atlassian.pageobjects.elements.PageElement;
  5. import com.atlassian.pageobjects.elements.PageElementFinder;
  6. import com.atlassian.webdriver.utils.by.ByJquery;
  7. import com.atlassian.webdriver.waiter.Waiter;
  8. import com.atlassian.webdriver.waiter.webdriver.function.ConditionFunction;
  9. import com.google.common.base.Preconditions;
  10. import org.openqa.selenium.By;
  11. import org.openqa.selenium.WebDriver;
  12. import org.openqa.selenium.WebElement;
  13. import org.openqa.selenium.interactions.Actions;
  14. import javax.inject.Inject;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. /**
  18. * WARNING: don't instantiate directly. Get this via the {@link AuiDatePicker}
  19. *
  20. * @since 3.7
  21. *
  22. * @deprecated Previously published API deprecated as of AUI 5.0. Do not use outside AUI. Will be refactored out eventually.
  23. */
  24. @Deprecated
  25. public class AuiDatePickerCalendar
  26. {
  27. @Inject
  28. PageElementFinder elementFinder;
  29. @Inject
  30. PageBinder binder;
  31. @Inject
  32. Waiter waiter;
  33. @Inject
  34. WebDriver driver;
  35. private final AuiDatePicker datePicker;
  36. private final String uuid;
  37. private PageElement calendar;
  38. public AuiDatePickerCalendar(AuiDatePicker datePicker, String uuid)
  39. {
  40. this.datePicker = datePicker;
  41. this.uuid = uuid;
  42. }
  43. @Init
  44. private void init()
  45. {
  46. this.calendar = elementFinder.find(By.cssSelector("div[data-aui-dp-popup-uuid='" + uuid + "']"));
  47. calendar.timed().isVisible().now();
  48. }
  49. /**
  50. * This de-focuses the date picker field
  51. * @return an instance of the {@link AuiDatePicker}
  52. */
  53. public AuiDatePicker close()
  54. {
  55. if (isOpen())
  56. {
  57. Actions action = new Actions(driver);
  58. WebElement el = driver.findElement(ByJquery.$("*[data-aui-dp-uuid=" + uuid + "]").parent());
  59. action.click(el).perform();
  60. waitForCalendarToClose();
  61. }
  62. return datePicker;
  63. }
  64. /**
  65. * Checks if the calendar is still open.
  66. */
  67. public boolean isOpen()
  68. {
  69. return calendar.isPresent() && calendar.isVisible();
  70. }
  71. /**
  72. * @return the current month that the date picker popup is set to.
  73. */
  74. public String getMonth()
  75. {
  76. Preconditions.checkState(isOpen(), "The calendar must be open to be able to get the month");
  77. return calendar.find(By.className("ui-datepicker-month")).getText();
  78. }
  79. /**
  80. * @return the current year that the date picker popup is set to.
  81. */
  82. public int getYear()
  83. {
  84. Preconditions.checkState(isOpen(), "The calendar must be open to be able to get the year");
  85. return Integer.parseInt(calendar.find(By.className("ui-datepicker-year")).getText());
  86. }
  87. /**
  88. * @return the current day the date picker popup is set to. -1 if there isn't one.
  89. */
  90. public int getDay()
  91. {
  92. Preconditions.checkState(isOpen(), "The calendar must be open to be able to get the day");
  93. PageElement el = calendar.find(By.className("ui-datepicker-current-day"));
  94. if (el.isPresent())
  95. {
  96. return Integer.parseInt(el.getText());
  97. }
  98. return -1;
  99. }
  100. /**
  101. * The date picker will change to the previous month.
  102. * @return an instance of the {@link AuiDatePickerCalendar}
  103. */
  104. public AuiDatePickerCalendar previousMonth()
  105. {
  106. Preconditions.checkState(isOpen(), "The calendar must be open to be able to change to the prev month");
  107. String currentMonth = getMonth();
  108. calendar.find(By.className("ui-datepicker-prev")).click();
  109. waitForMonthChange(currentMonth);
  110. return this;
  111. }
  112. public boolean canChangeToPreviousMonth()
  113. {
  114. return !calendar.find(By.className("ui-datepicker-prev")).hasClass("ui-state-disabled");
  115. }
  116. public boolean canChangeToNextMonth()
  117. {
  118. return !calendar.find(By.className("ui-datepicker-prev")).hasClass("ui-state-disabled");
  119. }
  120. /**
  121. * The date picker will change to the next month.
  122. * @return an instance of the {@link AuiDatePickerCalendar}
  123. */
  124. public AuiDatePickerCalendar nextMonth()
  125. {
  126. Preconditions.checkState(isOpen(), "The calendar must be open to be able to change to the next month");
  127. String currentMonth = getMonth();
  128. calendar.find(By.className("ui-datepicker-next")).click();
  129. waitForMonthChange(currentMonth);
  130. return this;
  131. }
  132. private void waitForMonthChange(final String currentMonth)
  133. {
  134. waiter.until().function(new ConditionFunction()
  135. {
  136. public Boolean apply(final WebDriver webDriver)
  137. {
  138. return currentMonth != getMonth();
  139. }
  140. }).isTrue().execute();
  141. }
  142. private void waitForCalendarToClose()
  143. {
  144. waiter.until().function(new ConditionFunction()
  145. {
  146. public Boolean apply(final WebDriver webDriver)
  147. {
  148. return !isOpen();
  149. }
  150. }).isTrue().execute();
  151. }
  152. /**
  153. * The date picker will select the day on the calendar that matches the value
  154. * passed in.
  155. * @param day the day to select on the calendar.
  156. * @return an instance of the {@link AuiDatePicker}
  157. */
  158. public AuiDatePicker setDay(int day)
  159. {
  160. Preconditions.checkState(isOpen(), "The calendar must be open to be able to set the day");
  161. PageElement el = calendar.find(
  162. ByJquery.$(".ui-datepicker-calendar td *:contains(" + day + ")").filter("function() { return ATLWD.$(this).text() == " + day + "}"));
  163. Preconditions.checkState(el.isPresent(),
  164. "The day: %d cannot be found for this date: %s/%s", day, getMonth(), getYear());
  165. Preconditions.checkState(!el.hasClass("ui-state-disabled"),
  166. "This day cannot be selected as it is disabled.");
  167. el.click();
  168. waitForCalendarToClose();
  169. return datePicker;
  170. }
  171. /**
  172. * Checks whether the supplied day can be set on the calendar.
  173. * This checks that both the day is present on the calendar and that
  174. * it is not disabled.
  175. * @param day the day to check
  176. * @return
  177. */
  178. public boolean canSetDay(int day)
  179. {
  180. Preconditions.checkState(isOpen(), "The calendar must be open.");
  181. PageElement el = calendar.find(
  182. ByJquery.$(".ui-datepicker-calendar td *:contains(" + day + ")").filter("function() { return ATLWD.$(this).text() == " + day + "}").parent());
  183. return el.isPresent() && !el.hasClass("ui-state-disabled");
  184. }
  185. public List<String> getDaysOfWeekShortNames()
  186. {
  187. Preconditions.checkState(isOpen(), "The calendar must be open to get the days of the week.");
  188. List<String> days = new ArrayList<String> ();
  189. List<PageElement> dayElements = calendar.findAll(By.cssSelector(".ui-datepicker-calendar thead th span"));
  190. for(PageElement dayEl : dayElements)
  191. {
  192. days.add(dayEl.getText());
  193. }
  194. return days;
  195. }
  196. public List<String> getDaysOfWeekLongNames()
  197. {
  198. Preconditions.checkState(isOpen(), "The calendar must be open to get the days of the week.");
  199. List<String> days = new ArrayList<String> ();
  200. List<PageElement> dayElements = calendar.findAll(By.cssSelector(".ui-datepicker-calendar thead th span"));
  201. for(PageElement dayEl : dayElements)
  202. {
  203. days.add(dayEl.getAttribute("title"));
  204. }
  205. return days;
  206. }
  207. }