You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TableNavigationPageDownTest.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package com.vaadin.tests.components.table;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.fail;
  4. import java.util.List;
  5. import org.junit.Test;
  6. import org.openqa.selenium.By;
  7. import org.openqa.selenium.Keys;
  8. import org.openqa.selenium.WebDriver;
  9. import org.openqa.selenium.WebElement;
  10. import org.openqa.selenium.interactions.Actions;
  11. import org.openqa.selenium.remote.DesiredCapabilities;
  12. import org.openqa.selenium.support.ui.ExpectedCondition;
  13. import com.vaadin.testbench.elements.TableElement;
  14. import com.vaadin.testbench.parallel.Browser;
  15. import com.vaadin.tests.tb3.MultiBrowserTest;
  16. /**
  17. * Tests that navigation with PageDown/PageUp/Home/End in Table works
  18. *
  19. * @author Vaadin Ltd
  20. */
  21. public class TableNavigationPageDownTest extends MultiBrowserTest {
  22. private static final int ROW_NUMBER = 50;
  23. private int lowerWrapperY = -1;
  24. private int pageHeight = -1;
  25. private int rowHeight = -1;
  26. private WebElement wrapper;
  27. @Override
  28. public List<DesiredCapabilities> getBrowsersToTest() {
  29. // Sending PageDown has no effect on PhantomJS. On IE focus
  30. // in Table is often lost, so default scrolling happens on PageDown.
  31. return getBrowserCapabilities(Browser.FIREFOX, Browser.CHROME);
  32. }
  33. @Override
  34. public void setup() throws Exception {
  35. super.setup();
  36. openTestURL();
  37. TableElement table = $(TableElement.class).first();
  38. rowHeight = table.getCell(1, 0).getLocation().getY()
  39. - table.getCell(0, 0).getLocation().getY();
  40. wrapper = findElement(By.className("v-table-body-wrapper"));
  41. pageHeight = wrapper.getSize().getHeight();
  42. lowerWrapperY = wrapper.getLocation().getY() + pageHeight;
  43. }
  44. private void sendKeyUntilEndIsReached(Keys key) {
  45. while (true) {
  46. int lastVisibleRowNumber = getLastVisibleRowNumber();
  47. sendKey(key);
  48. if (!waitUntilLastRowHasChanged(lastVisibleRowNumber)) {
  49. break;
  50. }
  51. }
  52. }
  53. private void sendKey(Keys key) {
  54. new Actions(driver).sendKeys(key).build().perform();
  55. }
  56. private boolean waitUntilLastRowHasChanged(final int row) {
  57. try {
  58. waitUntil(input -> row != getLastVisibleRowNumber(), 1);
  59. return true;
  60. } catch (Exception e) {
  61. return false;
  62. }
  63. }
  64. private int getLastVisibleRowNumber() {
  65. return getRowNumber(getLastVisibleRow());
  66. }
  67. private void sendPageDownUntilBottomIsReached() {
  68. sendKeyUntilEndIsReached(Keys.PAGE_DOWN);
  69. }
  70. private void sendPageUpUntilTopIsReached() {
  71. sendKeyUntilEndIsReached(Keys.PAGE_UP);
  72. }
  73. @Test
  74. public void navigatePageDown() {
  75. // Scroll to a point where you can reach the bottom with a couple of
  76. // page downs.
  77. // Can't use v-table-body height because lower rows haven't been
  78. // fetched yet.
  79. testBenchElement(wrapper)
  80. .scroll(ROW_NUMBER * rowHeight - (int) (2.8 * pageHeight));
  81. waitForScrollToFinish();
  82. getLastVisibleRow().click();
  83. sendPageDownUntilBottomIsReached();
  84. assertEquals("Last table row should be visible", ROW_NUMBER - 1,
  85. getLastVisibleRowNumber());
  86. }
  87. @Test
  88. public void navigatePageUp() {
  89. // Scroll to a point where you can reach the top with a couple of page
  90. // ups.
  91. testBenchElement(wrapper).scroll((int) (2.8 * pageHeight));
  92. waitForScrollToFinish();
  93. getFirstVisibleRow().click();
  94. sendPageUpUntilTopIsReached();
  95. assertEquals("First table row should be visible", 0,
  96. getRowNumber(getFirstVisibleRow()));
  97. }
  98. @Test
  99. public void navigateEndAndHome() {
  100. getLastVisibleRow().click();
  101. new Actions(driver).sendKeys(Keys.END).build().perform();
  102. waitForScrollToFinish();
  103. assertEquals("Last table row should be visible", ROW_NUMBER - 1,
  104. getRowNumber(getLastVisibleRow()));
  105. new Actions(driver).sendKeys(Keys.HOME).build().perform();
  106. waitForScrollToFinish();
  107. assertEquals("First table row should be visible", 0,
  108. getRowNumber(getFirstVisibleRow()));
  109. }
  110. /**
  111. * Waits until the scroll position indicator goes away, signifying that all
  112. * the required rows have been fetched.
  113. */
  114. private void waitForScrollToFinish() {
  115. waitUntil(new ExpectedCondition<Boolean>() {
  116. @Override
  117. public Boolean apply(WebDriver input) {
  118. List<WebElement> elements = findElements(
  119. By.className("v-table-scrollposition"));
  120. return elements.isEmpty() || !elements.get(0).isDisplayed();
  121. }
  122. @Override
  123. public String toString() {
  124. // Timed out after 10 seconds waiting for ...
  125. return "scroll position indicator to vanish";
  126. }
  127. });
  128. }
  129. /**
  130. * Returns row number from its first cell
  131. */
  132. private int getRowNumber(WebElement row) {
  133. return Integer.valueOf(row
  134. .findElement(By.className("v-table-cell-wrapper")).getText());
  135. }
  136. /**
  137. * Returns the first fully visible row
  138. */
  139. private WebElement getFirstVisibleRow() {
  140. List<WebElement> allFetchedRows = wrapper
  141. .findElements(By.tagName("tr"));
  142. int wrapperY = wrapper.getLocation().getY();
  143. for (WebElement row : allFetchedRows) {
  144. int rowY = row.getLocation().getY();
  145. if ((rowY >= wrapperY) && (rowY - rowHeight <= wrapperY)) {
  146. return row;
  147. }
  148. }
  149. fail("Could not find first visible row");
  150. return null;
  151. }
  152. /**
  153. * Returns the last fully visible row
  154. */
  155. private WebElement getLastVisibleRow() {
  156. List<WebElement> allFetchedRows = wrapper
  157. .findElements(By.tagName("tr"));
  158. for (WebElement row : allFetchedRows) {
  159. int lowerY = row.getLocation().getY() + rowHeight;
  160. if ((lowerY <= lowerWrapperY)
  161. && (lowerY + rowHeight >= lowerWrapperY)) {
  162. return row;
  163. }
  164. }
  165. fail("Could not find last visible row");
  166. return null;
  167. }
  168. }