Esempio n. 1
0
  /**
   * Check if the current webpage contains any of the given colors. Used on tests that use red to
   * show a failure.
   *
   * @param colors list of colors to check for.
   * @return true if the page contains any of the given colors, false otherwise.
   */
  public boolean containsColor(OperaColors... colors) {
    Canvas canvas = buildCanvas();
    ScreenShotReply reply = execService.containsColor(canvas, 100L, colors);

    List<ColorResult> results = reply.getColorResult();
    for (ColorResult result : results) {
      if (result.getCount() > 0) {
        return true;
      }
    }

    return false;
  }
Esempio n. 2
0
 /**
  * Take a screenshot of the area this element covers. If the hash of the image matches any of the
  * given hashes then no image is saved, otherwise it saves a copy of the image to the given
  * filename.
  *
  * @param filename The location to save the screenshot.
  * @param timeout The number of milliseconds to wait before taking the screenshot.
  * @param includeImage Whether to get the image data. Disable if you just need the MD5 hash.
  * @param hashes Known image hashes.
  * @return The MD5 hash of the screenshot.
  */
 public String saveScreenshot(
     String filename, long timeout, boolean includeImage, String... hashes) {
   Canvas canvas = buildCanvas();
   ScreenShotReply reply = execService.screenWatcher(canvas, timeout, includeImage, hashes);
   if (includeImage && reply.getPng() != null) {
     FileChannel stream;
     try {
       stream = new FileOutputStream(filename).getChannel();
       stream.write(ByteBuffer.wrap(reply.getPng()));
       stream.close();
     } catch (Exception e) {
       throw new WebDriverException("Failed to write file: " + e.getMessage());
     }
   }
   return reply.getMd5();
 }
  /**
   * Check if the current webpage contains any of the given colors. Used on tests that use red to
   * show a failure.
   *
   * @param colors list of colors to check for.
   * @return true if the page contains any of the given colors, false otherwise.
   * @deprecated
   */
  @SuppressWarnings("unused")
  @Deprecated
  public boolean containsColor(OperaColors... colors) {
    assertElementNotStale();

    Canvas canvas = buildCanvas();
    ScreenShotReply reply = execService.containsColor(canvas, 100L, colors);

    List<ColorResult> results = reply.getColorResult();

    for (ColorResult result : results) {
      if (result.getCount() > 0) {
        return true;
      }
    }

    return false;
  }
  /**
   * Take screenshot using external program. Will not trigger a screen repaint.
   *
   * @throws OperaRunnerException if launcher is shutdown or not running
   */
  @Override
  public ScreenShotReply saveScreenshot(long timeout, String... hashes)
      throws OperaRunnerException {
    assertLauncherAlive();

    String resultMd5;
    byte[] resultBytes;
    boolean blank = false;

    try {
      LauncherScreenshotRequest.Builder request = LauncherScreenshotRequest.newBuilder();
      for (String hash : hashes) {
        request.addKnownMD5S(hash);
      }
      request.setKnownMD5STimeoutMs((int) timeout);

      ResponseEncapsulation res =
          protocol.sendRequest(MessageType.MSG_SCREENSHOT, request.build().toByteArray());
      LauncherScreenshotResponse response = (LauncherScreenshotResponse) res.getResponse();

      resultMd5 = response.getMd5();
      resultBytes = response.getImagedata().toByteArray();

      if (response.hasBlank()) {
        blank = response.getBlank();
      }

    } catch (SocketTimeoutException e) {
      throw new OperaRunnerException("Could not get screenshot from launcher", e);
    } catch (IOException e) {
      throw new OperaRunnerException("Could not get screenshot from launcher", e);
    }

    ScreenShotReply screenshotreply = new ScreenShotReply(resultMd5, resultBytes);
    screenshotreply.setBlank(blank);
    screenshotreply.setCrashed(this.hasOperaCrashed());

    return screenshotreply;
  }