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.

TableInIframeRowClickScrollJumpTest.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.vaadin.tests.components.table;
  2. import static org.hamcrest.MatcherAssert.assertThat;
  3. import static org.junit.Assert.assertFalse;
  4. import java.util.List;
  5. import org.junit.Test;
  6. import org.openqa.selenium.JavascriptExecutor;
  7. import org.openqa.selenium.WebElement;
  8. import com.vaadin.testbench.By;
  9. import com.vaadin.testbench.elements.ButtonElement;
  10. import com.vaadin.testbench.elements.TableElement;
  11. import com.vaadin.tests.tb3.MultiBrowserTest;
  12. /**
  13. * For testing that UI scroll does not jump back to up when: 1. UI is in iframe
  14. * 2. the window scrolled down 3. and table is clicked
  15. *
  16. * @author Vaadin Ltd
  17. */
  18. public class TableInIframeRowClickScrollJumpTest extends MultiBrowserTest {
  19. private static final String TEST_URL = "statictestfiles/TableInIframeRowClickScrollJumpTest.html";
  20. @Test
  21. public void testRowClicking_WhenScrolledDown_shouldMaintainScrollPosition()
  22. throws InterruptedException {
  23. System.out.println(">>>" + getBaseURL() + TEST_URL);
  24. driver.get(getUrl());
  25. // using non-standard way because of iframe
  26. sleep(4000);
  27. // make sure we are in the "main content"
  28. driver.switchTo().defaultContent();
  29. sleep(2000);
  30. switchIntoIframe();
  31. // using non-standard way because of iframe
  32. waitForElementVisible(By.id("scroll-button"));
  33. ButtonElement scrollbutton = $(ButtonElement.class).id("scroll-button");
  34. scrollbutton.click();
  35. // using non-standard way because of iframe
  36. sleep(1000);
  37. Long scrollPosition = getWindowsScrollPosition();
  38. assertThat("Scroll position should be greater than 100 (it was "
  39. + scrollPosition + ")", scrollPosition > 100);
  40. TableElement table = $(TableElement.class).first();
  41. table.getRow(13).getCell(0).click();
  42. // using non-standard way because of iframe
  43. sleep(1000);
  44. Long scrollPosition2 = getWindowsScrollPosition();
  45. assertThat(
  46. "Scroll position should stay about the same. Old was "
  47. + scrollPosition + " and new one " + scrollPosition2,
  48. Math.abs(scrollPosition - scrollPosition2) < 10);
  49. }
  50. private String getUrl() {
  51. String url;
  52. // using non-standard way because of iframe
  53. if (getBaseURL().charAt(getBaseURL().length() - 1) == '/') {
  54. url = getBaseURL() + TEST_URL;
  55. } else {
  56. // this one is for gerrit's teamcity :(
  57. url = getBaseURL() + '/' + TEST_URL;
  58. }
  59. return url;
  60. }
  61. public void switchIntoIframe() {
  62. List<WebElement> frames = driver.findElements(By.tagName("iframe"));
  63. assertFalse("No frames was found", frames.isEmpty());
  64. driver.switchTo().frame(frames.get(0));
  65. }
  66. private Long getWindowsScrollPosition() {
  67. // measure scroll pos in the main window
  68. driver.switchTo().defaultContent();
  69. JavascriptExecutor executor = (JavascriptExecutor) driver;
  70. Long value = (Long) executor.executeScript(
  71. "if (window.pageYOffset) return window.pageYOffset;else if (window.document.documentElement.scrollTop) return window.document.documentElement.scrollTop;else return window.document.body.scrollTop;");
  72. // back to the iframe
  73. switchIntoIframe();
  74. return value;
  75. }
  76. @Override
  77. // using non-standard way because of iframe
  78. protected void closeApplication() {
  79. if (driver != null) {
  80. try {
  81. driver.get(getUrl() + "?closeApplication");
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. }
  85. }
  86. }
  87. }