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.

TableMatchesMouseDownMouseUpElementTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package com.vaadin.tests.components.table;
  2. import static com.vaadin.tests.components.table.TableMatchesMouseDownMouseUpElement.CLEAR_BUTTON_ID;
  3. import static org.junit.Assert.assertEquals;
  4. import java.util.List;
  5. import org.junit.Test;
  6. import org.openqa.selenium.By;
  7. import org.openqa.selenium.WebElement;
  8. import org.openqa.selenium.interactions.Actions;
  9. import com.vaadin.testbench.elements.TableElement;
  10. import com.vaadin.tests.tb3.MultiBrowserTest;
  11. /**
  12. * Regular click cases already covered by @LabelEmbeddedClickThroughForTableTest
  13. * Testing cases when mouse down and mouse up positions are different
  14. *
  15. * @author Vaadin Ltd
  16. */
  17. public class TableMatchesMouseDownMouseUpElementTest extends MultiBrowserTest {
  18. TableElement table;
  19. @Test
  20. public void testClick() {
  21. openTestURL();
  22. table = $(TableElement.class).first();
  23. testMoveOut(getBoldTag(0, 2));
  24. testMoveIn(getBoldTag(0, 2));
  25. testMoveOut(getLabel(0, 1));
  26. testMoveIn(getLabel(0, 1));
  27. testClickOnDifferentRows();
  28. }
  29. /**
  30. * MouseDown on element and mouseUp outside element but on same cell
  31. */
  32. private void testMoveOut(WebElement element) {
  33. clearSelection();
  34. clickAndMove(element, 5, 5, 0, 50);
  35. checkSelectedRowCount(1);
  36. checkRowSelected(0);
  37. }
  38. /**
  39. * MouseDown outside element but on same cell and mouseUp on element
  40. */
  41. private void testMoveIn(WebElement element) {
  42. clearSelection();
  43. clickAndMove(element, 5, 55, 0, -50);
  44. checkSelectedRowCount(1);
  45. checkRowSelected(0);
  46. }
  47. /**
  48. * Mouse down in cell of row1 holds and mouse up in cell of row 2
  49. */
  50. public void testClickOnDifferentRows() {
  51. clearSelection();
  52. WebElement elementFrom = getCell(0, 1);
  53. WebElement elementTo = getCell(0, 2);
  54. clickAndMove(elementFrom, elementTo);
  55. checkSelectedRowCount(0);
  56. }
  57. private WebElement getBoldTag(int row, int column) {
  58. return table.getCell(row, column).findElement(By.className("v-label"))
  59. .findElement(By.tagName("b"));
  60. }
  61. private WebElement getLabel(int row, int column) {
  62. return table.getCell(row, column).findElement(By.className("v-label"));
  63. }
  64. private WebElement getCell(int row, int column) {
  65. return table.getCell(row, column);
  66. }
  67. private void clearSelection() {
  68. WebElement clearButton = vaadinElementById(CLEAR_BUTTON_ID);
  69. clearButton.click();
  70. }
  71. /**
  72. * Mouse down on element + initial offset -> Moves the "move offset" ->
  73. * Mouse up
  74. */
  75. private void clickAndMove(WebElement element, int initialX, int initialY,
  76. int moveX, int moveY) {
  77. new Actions(driver).moveToElement(element, initialX, initialY)
  78. .clickAndHold().perform();
  79. new Actions(driver).moveByOffset(moveX, moveY).perform();
  80. new Actions(driver).release().perform();
  81. }
  82. /**
  83. * Mouse down on elementFrom -> Moves to elementTo -> Mouse up
  84. */
  85. private void clickAndMove(WebElement elementFrom, WebElement elementTo) {
  86. new Actions(driver).moveToElement(elementFrom, 5, 5).clickAndHold()
  87. .perform();
  88. new Actions(driver).moveToElement(elementTo, 5, 5).perform();
  89. new Actions(driver).release().perform();
  90. }
  91. private void checkRowSelected(int rowIndex) {
  92. assertEquals(
  93. "contents of the selected row don't match contents of the row #"
  94. + rowIndex,
  95. table.getCell(rowIndex, 0).getText(),
  96. getSelectedRows().get(0)
  97. .findElement(By.className("v-table-cell-wrapper"))
  98. .getText());
  99. }
  100. private void checkSelectedRowCount(int expected) {
  101. assertEquals("unexpected table selection size", expected,
  102. getSelectedRows().size());
  103. }
  104. private List<WebElement> getSelectedRows() {
  105. return table.findElement(By.className("v-table-body"))
  106. .findElements(By.className("v-selected"));
  107. }
  108. }