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.

GridComponentsTest.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package com.vaadin.tests.components.grid;
  2. import java.util.stream.IntStream;
  3. import java.util.stream.Stream;
  4. import org.junit.Assert;
  5. import org.junit.Test;
  6. import org.openqa.selenium.WebElement;
  7. import com.vaadin.testbench.By;
  8. import com.vaadin.testbench.elements.ButtonElement;
  9. import com.vaadin.testbench.elements.GridElement;
  10. import com.vaadin.testbench.elements.GridElement.GridCellElement;
  11. import com.vaadin.testbench.elements.GridElement.GridRowElement;
  12. import com.vaadin.testbench.elements.LabelElement;
  13. import com.vaadin.testbench.elements.NotificationElement;
  14. import com.vaadin.tests.tb3.MultiBrowserTest;
  15. public class GridComponentsTest extends MultiBrowserTest {
  16. @Test
  17. public void testReuseTextFieldOnScroll() {
  18. openTestURL();
  19. GridElement grid = $(GridElement.class).first();
  20. editTextFieldInCell(grid, 0, 1);
  21. // Scroll out of view port
  22. grid.getRow(900);
  23. // Scroll back
  24. grid.getRow(0);
  25. WebElement textField = grid.getCell(0, 1)
  26. .findElement(By.tagName("input"));
  27. Assert.assertEquals("TextField value was reset", "Foo",
  28. textField.getAttribute("value"));
  29. Assert.assertTrue("No mention in the log",
  30. logContainsText("1. Reusing old text field for: Row 0"));
  31. }
  32. @Test
  33. public void testReuseTextFieldOnSelect() {
  34. openTestURL();
  35. GridElement grid = $(GridElement.class).first();
  36. editTextFieldInCell(grid, 1, 1);
  37. // Select row
  38. grid.getCell(1, 1).click(1, 1);
  39. WebElement textField = grid.getCell(1, 1)
  40. .findElement(By.tagName("input"));
  41. Assert.assertEquals("TextField value was reset", "Foo",
  42. textField.getAttribute("value"));
  43. Assert.assertTrue("No mention in the log",
  44. logContainsText("1. Reusing old text field for: Row 1"));
  45. }
  46. @Test
  47. public void testReplaceData() {
  48. openTestURL();
  49. assertRowExists(5, "Row 5");
  50. $(ButtonElement.class).caption("Reset data").first().click();
  51. assertRowExists(5, "Row 1005");
  52. }
  53. @Test
  54. public void testTextFieldSize() {
  55. openTestURL();
  56. GridCellElement cell = $(GridElement.class).first().getCell(0, 1);
  57. int cellWidth = cell.getSize().getWidth();
  58. int fieldWidth = cell.findElement(By.tagName("input")).getSize()
  59. .getWidth();
  60. // padding left and right, +1 to fix sub pixel issues
  61. int padding = 18 * 2 + 1;
  62. int extraSpace = Math.abs(fieldWidth - cellWidth);
  63. Assert.assertTrue("Too much unused space in cell. Expected: " + padding
  64. + " Actual: " + extraSpace, extraSpace <= padding);
  65. }
  66. private void editTextFieldInCell(GridElement grid, int row, int col) {
  67. WebElement textField = grid.getCell(row, col)
  68. .findElement(By.tagName("input"));
  69. textField.clear();
  70. textField.sendKeys("Foo");
  71. }
  72. @Test
  73. public void testRow5() {
  74. openTestURL();
  75. assertRowExists(5, "Row 5");
  76. }
  77. @Test
  78. public void testRow0() {
  79. openTestURL();
  80. assertRowExists(0, "Row 0");
  81. Assert.assertEquals("Grid row height is not what it should be", 40,
  82. $(GridElement.class).first().getRow(0).getSize().getHeight());
  83. }
  84. @Test
  85. public void testRow999() {
  86. openTestURL();
  87. assertRowExists(999, "Row 999");
  88. }
  89. @Test
  90. public void testRow30() {
  91. openTestURL();
  92. Stream.of(30, 130, 230, 330).forEach(this::assertNoButton);
  93. IntStream.range(300, 310).forEach(this::assertNoButton);
  94. }
  95. @Test(expected = AssertionError.class)
  96. public void testRow31() {
  97. openTestURL();
  98. // There is a button on row 31. This should fail.
  99. assertNoButton(31);
  100. }
  101. @Test
  102. public void testHeaders() {
  103. openTestURL();
  104. GridElement grid = $(GridElement.class).first();
  105. GridCellElement headerCell = grid.getHeaderCell(0, 0);
  106. Assert.assertTrue("First header should contain a Label",
  107. headerCell.isElementPresent(LabelElement.class));
  108. Assert.assertEquals("Label",
  109. headerCell.$(LabelElement.class).first().getText());
  110. Assert.assertFalse("Second header should not contain a component",
  111. grid.getHeaderCell(0, 1).isElementPresent(LabelElement.class));
  112. Assert.assertEquals("Other Components",
  113. grid.getHeaderCell(0, 1).getText());
  114. }
  115. private void assertRowExists(int i, String string) {
  116. GridRowElement row = $(GridElement.class).first().getRow(i);
  117. Assert.assertEquals("Label text did not match", string,
  118. row.getCell(0).getText());
  119. row.findElement(By.id(string.replace(' ', '_').toLowerCase())).click();
  120. // IE 11 is slow, need to wait for the notification.
  121. waitUntil(driver -> isElementPresent(NotificationElement.class), 10);
  122. Assert.assertTrue("Notification should contain given text",
  123. $(NotificationElement.class).first().getText()
  124. .contains(string));
  125. }
  126. private void assertNoButton(int i) {
  127. GridRowElement row = $(GridElement.class).first().getRow(i);
  128. Assert.assertFalse("Row " + i + " should not have a button",
  129. row.getCell(2).isElementPresent(ButtonElement.class));
  130. }
  131. }