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

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