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.

GridClientDataSourcesTest.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.grid.basicfeatures;
  17. import static org.junit.Assert.assertNotNull;
  18. import static org.junit.Assert.assertNull;
  19. import org.junit.Before;
  20. import org.junit.Test;
  21. import org.openqa.selenium.By;
  22. import org.openqa.selenium.Dimension;
  23. import org.openqa.selenium.JavascriptExecutor;
  24. import org.openqa.selenium.NoSuchElementException;
  25. import org.openqa.selenium.WebDriver;
  26. import org.openqa.selenium.WebElement;
  27. import org.openqa.selenium.interactions.Actions;
  28. import com.vaadin.tests.tb3.MultiBrowserTest;
  29. public class GridClientDataSourcesTest extends MultiBrowserTest {
  30. @Before
  31. public void before() {
  32. openTestURL();
  33. }
  34. @Test
  35. public void normalRestishDatasource() throws Exception {
  36. selectMenuPath("DataSources", "RESTish", "Use");
  37. assertCellPresent("cell 0 #0");
  38. scrollToBottom();
  39. assertCellPresent("cell 99 #0");
  40. assertCellNotPresent("cell 100 #0");
  41. }
  42. @Test
  43. public void growOnRequestRestishDatasource() throws Exception {
  44. selectMenuPath("DataSources", "RESTish", "Use");
  45. selectMenuPath("DataSources", "RESTish", "Next request +10");
  46. scrollToBottom();
  47. /* second scroll needed because of scrollsize change after scrolling */
  48. scrollToBottom();
  49. assertCellPresent("cell 109 #1");
  50. assertCellNotPresent("cell 110 #1");
  51. }
  52. @Test
  53. public void shrinkOnRequestRestishDatasource() throws Exception {
  54. selectMenuPath("DataSources", "RESTish", "Use");
  55. scrollToBottom();
  56. selectMenuPath("DataSources", "RESTish", "Next request -10");
  57. scrollToTop();
  58. assertCellPresent("cell 0 #1");
  59. }
  60. @Test
  61. public void pushChangeRestishDatasource() throws Exception {
  62. selectMenuPath("DataSources", "RESTish", "Use");
  63. selectMenuPath("DataSources", "RESTish", "Push data change");
  64. assertCellPresent("cell 0 #1");
  65. assertCellNotPresent("cell 0 #0");
  66. }
  67. @Test
  68. public void growOnPushRestishDatasource() throws Exception {
  69. selectMenuPath("DataSources", "RESTish", "Use");
  70. selectMenuPath("DataSources", "RESTish", "Push data change +10");
  71. assertCellPresent("cell 0 #1");
  72. assertCellNotPresent("cell 0 #0");
  73. scrollToBottom();
  74. assertCellPresent("cell 109 #1");
  75. }
  76. @Test
  77. public void shrinkOnPushRestishDatasource() throws Exception {
  78. selectMenuPath("DataSources", "RESTish", "Use");
  79. scrollToBottom();
  80. selectMenuPath("DataSources", "RESTish", "Push data change -10");
  81. assertCellPresent("cell 89 #1");
  82. assertCellNotPresent("cell 89 #0");
  83. assertCellNotPresent("cell 99 #1");
  84. assertCellNotPresent("cell 99 #0");
  85. }
  86. private void assertCellPresent(String content) {
  87. assertNotNull("A cell with content \"" + content
  88. + "\" should've been found", findByXPath("//td[text()='"
  89. + content + "']"));
  90. }
  91. private void assertCellNotPresent(String content) {
  92. assertNull("A cell with content \"" + content
  93. + "\" should've not been found", findByXPath("//td[text()='"
  94. + content + "']"));
  95. }
  96. private void scrollToTop() {
  97. scrollVerticallyTo(0);
  98. }
  99. private void scrollToBottom() {
  100. scrollVerticallyTo(9999);
  101. }
  102. private WebElement findByXPath(String string) {
  103. try {
  104. return findElement(By.xpath(string));
  105. } catch (NoSuchElementException e) {
  106. return null;
  107. }
  108. }
  109. private void scrollVerticallyTo(int px) {
  110. executeScript("arguments[0].scrollTop = " + px, findVerticalScrollbar());
  111. }
  112. private Object executeScript(String script, Object args) {
  113. final WebDriver driver = getDriver();
  114. if (driver instanceof JavascriptExecutor) {
  115. final JavascriptExecutor je = (JavascriptExecutor) driver;
  116. return je.executeScript(script, args);
  117. } else {
  118. throw new IllegalStateException("current driver "
  119. + getDriver().getClass().getName() + " is not a "
  120. + JavascriptExecutor.class.getSimpleName());
  121. }
  122. }
  123. private WebElement findVerticalScrollbar() {
  124. return getDriver().findElement(
  125. By.xpath("//div[contains(@class, "
  126. + "\"v-grid-scroller-vertical\")]"));
  127. }
  128. private void selectMenu(String menuCaption) {
  129. WebElement menuElement = getMenuElement(menuCaption);
  130. Dimension size = menuElement.getSize();
  131. new Actions(getDriver()).moveToElement(menuElement, size.width - 10,
  132. size.height / 2).perform();
  133. }
  134. private WebElement getMenuElement(String menuCaption) {
  135. return getDriver().findElement(
  136. By.xpath("//td[text() = '" + menuCaption + "']"));
  137. }
  138. private void selectMenuPath(String... menuCaptions) {
  139. new Actions(getDriver()).moveToElement(getMenuElement(menuCaptions[0]))
  140. .click().perform();
  141. for (int i = 1; i < menuCaptions.length - 1; ++i) {
  142. selectMenu(menuCaptions[i]);
  143. new Actions(getDriver()).moveByOffset(20, 0).perform();
  144. }
  145. new Actions(getDriver())
  146. .moveToElement(
  147. getMenuElement(menuCaptions[menuCaptions.length - 1]))
  148. .click().perform();
  149. }
  150. }