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 4.1KB

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