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.

GridBasicFeaturesTest.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 java.util.ArrayList;
  18. import java.util.List;
  19. import org.openqa.selenium.By;
  20. import org.openqa.selenium.Dimension;
  21. import org.openqa.selenium.JavascriptExecutor;
  22. import org.openqa.selenium.WebDriver;
  23. import org.openqa.selenium.WebElement;
  24. import org.openqa.selenium.interactions.Actions;
  25. import com.vaadin.testbench.TestBenchElement;
  26. import com.vaadin.testbench.elements.GridElement;
  27. import com.vaadin.testbench.parallel.TestCategory;
  28. import com.vaadin.tests.tb3.MultiBrowserTest;
  29. @TestCategory("grid")
  30. public abstract class GridBasicFeaturesTest extends MultiBrowserTest {
  31. @Override
  32. protected boolean requireWindowFocusForIE() {
  33. return true;
  34. }
  35. @Override
  36. protected Class<?> getUIClass() {
  37. return GridBasicFeatures.class;
  38. }
  39. protected void selectMenu(String menuCaption) {
  40. selectMenu(menuCaption, true);
  41. }
  42. protected void selectMenu(String menuCaption, boolean click) {
  43. WebElement menuElement = getMenuElement(menuCaption);
  44. Dimension size = menuElement.getSize();
  45. new Actions(getDriver()).moveToElement(menuElement, size.width - 10,
  46. size.height / 2).perform();
  47. if (click) {
  48. new Actions(getDriver()).click().perform();
  49. }
  50. }
  51. protected WebElement getMenuElement(String menuCaption) {
  52. return getDriver().findElement(
  53. By.xpath("//span[text() = '" + menuCaption + "']"));
  54. }
  55. protected void selectMenuPath(String... menuCaptions) {
  56. selectMenu(menuCaptions[0], true);
  57. for (int i = 1; i < menuCaptions.length - 1; i++) {
  58. selectMenu(menuCaptions[i]);
  59. new Actions(getDriver()).moveByOffset(40, 0).build().perform();
  60. }
  61. selectMenu(menuCaptions[menuCaptions.length - 1], true);
  62. }
  63. protected GridElement getGridElement() {
  64. return ((TestBenchElement) findElement(By.id("testComponent")))
  65. .wrap(GridElement.class);
  66. }
  67. protected void scrollGridVerticallyTo(double px) {
  68. executeScript("arguments[0].scrollTop = " + px,
  69. getGridVerticalScrollbar());
  70. }
  71. protected int getGridVerticalScrollPos() {
  72. return ((Number) executeScript("return arguments[0].scrollTop",
  73. getGridVerticalScrollbar())).intValue();
  74. }
  75. protected List<TestBenchElement> getGridHeaderRowCells() {
  76. List<TestBenchElement> headerCells = new ArrayList<TestBenchElement>();
  77. for (int i = 0; i < getGridElement().getHeaderCount(); ++i) {
  78. headerCells.addAll(getGridElement().getHeaderCells(i));
  79. }
  80. return headerCells;
  81. }
  82. protected List<TestBenchElement> getGridFooterRowCells() {
  83. List<TestBenchElement> footerCells = new ArrayList<TestBenchElement>();
  84. for (int i = 0; i < getGridElement().getFooterCount(); ++i) {
  85. footerCells.addAll(getGridElement().getFooterCells(i));
  86. }
  87. return footerCells;
  88. }
  89. protected WebElement getEditor() {
  90. List<WebElement> elems = getGridElement().findElements(
  91. By.className("v-grid-editor"));
  92. assertLessThanOrEqual("number of editors", elems.size(), 1);
  93. return elems.isEmpty() ? null : elems.get(0);
  94. }
  95. private Object executeScript(String script, WebElement element) {
  96. final WebDriver driver = getDriver();
  97. if (driver instanceof JavascriptExecutor) {
  98. final JavascriptExecutor je = (JavascriptExecutor) driver;
  99. return je.executeScript(script, element);
  100. } else {
  101. throw new IllegalStateException("current driver "
  102. + getDriver().getClass().getName() + " is not a "
  103. + JavascriptExecutor.class.getSimpleName());
  104. }
  105. }
  106. protected WebElement getGridVerticalScrollbar() {
  107. return getDriver()
  108. .findElement(
  109. By.xpath("//div[contains(@class, \"v-grid-scroller-vertical\")]"));
  110. }
  111. /**
  112. * Reloads the page without restartApplication. This occasionally breaks
  113. * stuff.
  114. */
  115. protected void reopenTestURL() {
  116. String testUrl = getTestUrl();
  117. testUrl = testUrl.replace("?restartApplication", "?");
  118. testUrl = testUrl.replace("?&", "?");
  119. driver.get(testUrl);
  120. }
  121. }