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.

UnnecessaryScrollbarWhenZoomingTest.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertNotNull;
  19. import java.util.Arrays;
  20. import org.junit.After;
  21. import org.junit.Assert;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import org.openqa.selenium.JavascriptExecutor;
  25. import org.openqa.selenium.Keys;
  26. import org.openqa.selenium.WebDriver;
  27. import org.openqa.selenium.WebElement;
  28. import org.openqa.selenium.remote.DesiredCapabilities;
  29. import org.openqa.selenium.support.ui.ExpectedConditions;
  30. import org.openqa.selenium.support.ui.WebDriverWait;
  31. import com.vaadin.testbench.By;
  32. import com.vaadin.testbench.commands.TestBenchCommandExecutor;
  33. import com.vaadin.testbench.parallel.BrowserUtil;
  34. import com.vaadin.tests.tb3.MultiBrowserTest;
  35. public class UnnecessaryScrollbarWhenZoomingTest extends MultiBrowserTest {
  36. private ZoomLevelSetter zoomSetter;
  37. private int zoomOutIterations = 3;
  38. private int zoomInIterations = 3;
  39. @Before
  40. public void init() {
  41. testBench().resizeViewPortTo(995, 400);
  42. DesiredCapabilities capabilities = getDesiredCapabilities();
  43. if (BrowserUtil.isChrome(capabilities)
  44. || BrowserUtil.isPhantomJS(capabilities)) {
  45. zoomSetter = new ChromeZoomLevelSetter(driver);
  46. } else {
  47. zoomSetter = new NonChromeZoomLevelSetter(driver);
  48. }
  49. zoomSetter.resetZoom();
  50. openTestURL();
  51. // IE sometimes has trouble waiting long enough.
  52. new WebDriverWait(getDriver(), 30).until(ExpectedConditions
  53. .presenceOfElementLocated(By
  54. .cssSelector(".v-table-body-wrapper")));
  55. }
  56. @Test
  57. public void testInitial() {
  58. testExtraScrollbarsNotShown();
  59. }
  60. @Test
  61. public void testZoomingIn() {
  62. for (int i = 0; i < zoomInIterations; i++) {
  63. zoomSetter.increaseZoom();
  64. testExtraScrollbarsNotShown();
  65. }
  66. }
  67. @Test
  68. public void testZoomingOut() throws InterruptedException {
  69. for (int i = 0; i < zoomOutIterations; i++) {
  70. zoomSetter.decreaseZoom();
  71. testExtraScrollbarsNotShown();
  72. }
  73. }
  74. @After
  75. public void resetZoomLevel() {
  76. zoomSetter.resetZoom();
  77. }
  78. private void testExtraScrollbarsNotShown() {
  79. // wait a bit for the layout
  80. try {
  81. Thread.sleep(1000);
  82. } catch (InterruptedException e) {
  83. Assert.fail();
  84. }
  85. WebElement element = findElement(By
  86. .cssSelector(".v-table-body-wrapper"));
  87. assertNotNull("There must be a table", element);
  88. String overflow = element.getCssValue("overflow");
  89. // As long as the overflow is hidden, there will not be scroll bars.
  90. if (!"hidden".equals(overflow)) {
  91. // compare scroll width to offset width. True if scrolling.
  92. String detectHorizontalScroll = "return arguments[0].scrollWidth > arguments[0].clientWidth";
  93. Boolean horizontal = (Boolean) ((TestBenchCommandExecutor) getDriver())
  94. .executeScript(detectHorizontalScroll, element);
  95. assertEquals("there must be no horizontal scrollbar", false,
  96. horizontal);
  97. String detectVerticalScroll = "return arguments[0].scrollHeight > arguments[0].clientHeight";
  98. Boolean vertical = (Boolean) ((TestBenchCommandExecutor) getDriver())
  99. .executeScript(detectVerticalScroll, element);
  100. assertEquals("there must be no vertical scrollbar", false, vertical);
  101. }
  102. }
  103. interface ZoomLevelSetter {
  104. public void increaseZoom();
  105. public void decreaseZoom();
  106. public void resetZoom();
  107. }
  108. /*
  109. * A class for setting the zoom levels by sending keys such as ctrl and +.
  110. */
  111. class NonChromeZoomLevelSetter implements ZoomLevelSetter {
  112. private WebDriver driver;
  113. public NonChromeZoomLevelSetter(WebDriver driver) {
  114. this.driver = driver;
  115. }
  116. @Override
  117. public void increaseZoom() {
  118. getElement().sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));
  119. }
  120. @Override
  121. public void decreaseZoom() {
  122. getElement().sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT));
  123. }
  124. @Override
  125. public void resetZoom() {
  126. getElement().sendKeys(Keys.chord(Keys.CONTROL, "0"));
  127. }
  128. private WebElement getElement() {
  129. return driver.findElement(By.tagName("html"));
  130. }
  131. }
  132. /*
  133. * A class for setting the zoom levels using JavaScript. This setter is used
  134. * for browsers for which the method of sending the keys ctrl and + does not
  135. * work.
  136. */
  137. class ChromeZoomLevelSetter implements ZoomLevelSetter {
  138. private JavascriptExecutor js;
  139. private int currentZoomIndex = 2;
  140. private int[] zoomLevels = { 70, 80, 90, 100, 110, 120, 130 };
  141. public ChromeZoomLevelSetter(WebDriver driver) {
  142. js = (JavascriptExecutor) driver;
  143. }
  144. @Override
  145. public void increaseZoom() {
  146. currentZoomIndex++;
  147. if (currentZoomIndex >= zoomLevels.length) {
  148. currentZoomIndex = zoomLevels.length - 1;
  149. }
  150. js.executeScript("document.body.style.zoom='"
  151. + zoomLevels[currentZoomIndex] + "%'");
  152. }
  153. @Override
  154. public void decreaseZoom() {
  155. currentZoomIndex--;
  156. if (currentZoomIndex < 0) {
  157. currentZoomIndex = 0;
  158. }
  159. js.executeScript("document.body.style.zoom='"
  160. + zoomLevels[currentZoomIndex] + "%'");
  161. }
  162. @Override
  163. public void resetZoom() {
  164. js.executeScript("document.body.style.zoom='100%'");
  165. currentZoomIndex = Arrays.binarySearch(zoomLevels, 100);
  166. }
  167. }
  168. }