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 7.0KB

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