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.

CustomTestBenchCommandExecutor.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.tb3;
  17. import java.awt.image.BufferedImage;
  18. import java.io.ByteArrayInputStream;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.util.logging.Logger;
  22. import javax.imageio.IIOException;
  23. import javax.imageio.ImageIO;
  24. import org.openqa.selenium.Dimension;
  25. import org.openqa.selenium.OutputType;
  26. import org.openqa.selenium.Point;
  27. import org.openqa.selenium.TakesScreenshot;
  28. import org.openqa.selenium.WebDriver;
  29. import org.openqa.selenium.WebElement;
  30. import com.vaadin.testbench.Parameters;
  31. import com.vaadin.testbench.screenshot.ImageComparison;
  32. import com.vaadin.testbench.screenshot.ImageFileUtil;
  33. /**
  34. * Internal hack to support capturing screenshots for elements.
  35. *
  36. * Most parts are from TestBenchCommandExecutor and the feature should be
  37. * integrated into TB4.
  38. *
  39. * @author Vaadin Ltd
  40. */
  41. public class CustomTestBenchCommandExecutor {
  42. private ImageComparison imageComparison = new ImageComparison();
  43. private final WebDriver actualDriver;
  44. public CustomTestBenchCommandExecutor(WebDriver driver) {
  45. actualDriver = driver;
  46. }
  47. /**
  48. * Compares the screenshot of the given element with the reference.
  49. *
  50. * Copied from TestBenchCommandExecutor
  51. */
  52. public boolean compareScreen(WebElement element, File reference,
  53. boolean isIE8) throws IOException {
  54. BufferedImage image = null;
  55. try {
  56. image = ImageIO.read(reference);
  57. } catch (IIOException e) {
  58. // Don't worry, an error screen shot will be generated that later
  59. // can be used as the reference
  60. }
  61. return compareScreen(element, image, reference.getName(), isIE8);
  62. }
  63. /**
  64. * Compares the screenshot of the given element with the reference.
  65. *
  66. * Copied from TestBenchCommandExecutor and added cropToElement
  67. */
  68. public boolean compareScreen(WebElement element, BufferedImage reference,
  69. String referenceName, boolean isIE8) throws IOException {
  70. for (int times = 0; times < Parameters.getMaxScreenshotRetries(); times++) {
  71. BufferedImage screenshotImage = cropToElement(element,
  72. ImageIO.read(new ByteArrayInputStream(
  73. ((TakesScreenshot) actualDriver)
  74. .getScreenshotAs(OutputType.BYTES))), isIE8);
  75. if (reference == null) {
  76. // Store the screenshot in the errors directory and fail the
  77. // test
  78. ImageFileUtil.createScreenshotDirectoriesIfNeeded();
  79. ImageIO.write(screenshotImage, "png",
  80. ImageFileUtil.getErrorScreenshotFile(referenceName));
  81. getLogger().severe(
  82. "No reference found for "
  83. + referenceName
  84. + " in "
  85. + ImageFileUtil
  86. .getScreenshotReferenceDirectory());
  87. return false;
  88. }
  89. if (imageComparison.imageEqualToReference(screenshotImage,
  90. reference, referenceName,
  91. Parameters.getScreenshotComparisonTolerance())) {
  92. return true;
  93. }
  94. pause(Parameters.getScreenshotRetryDelay());
  95. }
  96. return false;
  97. }
  98. /**
  99. * Crops the image to show only the element. If the element is partly off
  100. * screen, crops to show the part of the element which is in the screenshot
  101. *
  102. * @param element
  103. * the element to retain in the screenshot
  104. * @param fullScreen
  105. * the full screen image
  106. * @param isIE8
  107. * true if the browser is IE8
  108. * @return
  109. * @throws IOException
  110. */
  111. public static BufferedImage cropToElement(WebElement element,
  112. BufferedImage fullScreen, boolean isIE8) throws IOException {
  113. Point loc = element.getLocation();
  114. Dimension size = element.getSize();
  115. int x = loc.x, y = loc.y;
  116. int w = size.width;
  117. int h = size.height;
  118. if (isIE8) {
  119. // IE8 border...
  120. x += 2;
  121. y += 2;
  122. }
  123. if (x >= 0 && x < fullScreen.getWidth()) {
  124. // X loc on screen
  125. // Get the part of the element which is on screen
  126. w = Math.min(fullScreen.getWidth() - x, w);
  127. } else {
  128. throw new IOException("Element x is outside the screenshot (x: "
  129. + x + ", y: " + y + ")");
  130. }
  131. if (y >= 0 && y < fullScreen.getHeight()) {
  132. // Y loc on screen
  133. // Get the part of the element which is on screen
  134. h = Math.min(fullScreen.getHeight() - y, h);
  135. } else {
  136. throw new IOException("Element y is outside the screenshot (x: "
  137. + x + ", y: " + y + ")");
  138. }
  139. return fullScreen.getSubimage(x, y, w, h);
  140. }
  141. private void pause(int delay) {
  142. try {
  143. Thread.sleep(delay);
  144. } catch (InterruptedException e) {
  145. }
  146. }
  147. private static Logger getLogger() {
  148. return Logger.getLogger(CustomTestBenchCommandExecutor.class.getName());
  149. }
  150. }