public static boolean compareScreen(
     BufferedImage reference,
     String referenceName,
     ImageComparison imageComparison,
     TakesScreenshot takesScreenshot,
     HasCapabilities driver)
     throws IOException {
   for (int times = 0; times < Parameters.getMaxScreenshotRetries(); times++) {
     BufferedImage screenshotImage =
         ImageIO.read(new ByteArrayInputStream(takesScreenshot.getScreenshotAs(OutputType.BYTES)));
     if (reference == null) {
       // Store the screenshot in the errors directory and fail the
       // test
       ImageFileUtil.createScreenshotDirectoriesIfNeeded();
       ImageIO.write(screenshotImage, "png", ImageFileUtil.getErrorScreenshotFile(referenceName));
       getLogger()
           .severe(
               "No reference found for "
                   + referenceName
                   + " in "
                   + ImageFileUtil.getScreenshotReferenceDirectory());
       return false;
     }
     if (imageComparison.imageEqualToReference(
         screenshotImage,
         reference,
         referenceName,
         Parameters.getScreenshotComparisonTolerance())) {
       return true;
     }
     pause(Parameters.getScreenshotRetryDelay());
   }
   return false;
 }
 protected File takeScreenshotInMemory(TakesScreenshot driver) {
   try {
     return driver.getScreenshotAs(FILE);
   } catch (Exception e) {
     printOnce("takeScreenshotAsFile", e);
     return null;
   }
 }
 public static void saveScreenshot(By element, String path) {
   TakesScreenshot screenMaker = (TakesScreenshot) getDriver().findElement(element);
   File screen = screenMaker.getScreenshotAs(OutputType.FILE);
   try {
     FileUtils.copyFile(screen, new File(path));
   } catch (IOException ex) {
     System.out.println(ex.getMessage());
   }
 }
Beispiel #4
0
 static void screenShot(TakesScreenshot drivername, String filename) {
   File scrFile = drivername.getScreenshotAs(OutputType.FILE);
   // System.out.println("save snapshot path is:c:\\" + filename);
   try {
     FileUtils.copyFile(scrFile, new File(filename));
   } catch (IOException e) {
     e.printStackTrace();
   } finally {
     System.out.println("screen shot finished");
   }
 }
 protected File takeScreenshotImage(TakesScreenshot driver, String fileName) {
   try {
     File scrFile = driver.getScreenshotAs(FILE);
     File imageFile = new File(reportsFolder, fileName + ".png");
     copyFile(scrFile, imageFile);
     return imageFile;
   } catch (Exception e) {
     printOnce("takeScreenshotImage", e);
     return null;
   }
 }
Beispiel #6
0
 public static String CaptureScreen(WebDriver driver, String ImagesPath) {
   TakesScreenshot oScn = (TakesScreenshot) driver;
   File oScnShot = oScn.getScreenshotAs(OutputType.FILE);
   File oDest = new File(ImagesPath + ".jpg");
   try {
     FileUtils.copyFile(oScnShot, oDest);
   } catch (IOException e) {
     System.out.println(e.getMessage());
   }
   return ImagesPath + ".jpg";
 }
 public static String take() {
   Date date = new Date();
   String path = "./src/test/results/screenshots/" + date.getTime() + ".png";
   TakesScreenshot takesScreenshot = (TakesScreenshot) Driver();
   File source = takesScreenshot.getScreenshotAs(OutputType.FILE);
   try {
     FileUtils.copyFile(source, new File(path));
   } catch (IOException e) {
     e.printStackTrace();
   }
   return path;
 }
  /**
   * Captures a screenshot of the given screen (if parameter is a driver) or the given element (if
   * the parameter is a WebElement).
   *
   * @param screenshotContext
   * @param capabilities
   * @param isIE8 <code>true</code> if this is IE8, <code>false</code> otherwise
   * @return
   * @throws IOException
   */
  private static BufferedImage getScreenshot(
      TakesScreenshot driver, TakesScreenshot screenshotContext, Capabilities capabilities)
      throws IOException {
    boolean elementScreenshot = (screenshotContext instanceof WebElement);

    if (elementScreenshot && supportsElementScreenshots == null) {
      if (BrowserUtil.isPhantomJS(capabilities)) {
        // PhantomJS will die if you try to detect this...
        supportsElementScreenshots = false;
      } else {
        // Detect if the driver supports element screenshots or not
        try {
          byte[] screenshotBytes = screenshotContext.getScreenshotAs(OutputType.BYTES);
          supportsElementScreenshots = true;
          return ImageIO.read(new ByteArrayInputStream(screenshotBytes));
        } catch (UnsupportedCommandException e) {
          supportsElementScreenshots = false;
        } catch (WebDriverException e) {
          if (e.getCause() instanceof UnsupportedCommandException) {
            supportsElementScreenshots = false;
          } else {
            throw e;
          }
        }
      }
    }

    if (elementScreenshot && !supportsElementScreenshots) {
      // Driver does not support element screenshots, get whole screen
      // and crop
      BufferedImage image =
          ImageIO.read(new ByteArrayInputStream(driver.getScreenshotAs(OutputType.BYTES)));
      return cropToElement((WebElement) screenshotContext, image, BrowserUtil.isIE8(capabilities));
    } else {
      // Element or full screen image
      return ImageIO.read(
          new ByteArrayInputStream(screenshotContext.getScreenshotAs(OutputType.BYTES)));
    }
  }
Beispiel #9
0
  /**
   * This Method create for take screenshot
   *
   * @author Young
   * @param drivername
   * @param filename
   */
  public static void snapshot(TakesScreenshot drivername, String filename) {
    // this method will take screen shot ,require two parameters ,one is
    // driver name, another is file name

    String currentPath = System.getProperty("user.dir"); // get current work
    // folder
    File scrFile = drivername.getScreenshotAs(OutputType.FILE);
    // Now you can do whatever you need to do with it, for example copy
    // somewhere
    try {
      System.out.println("save snapshot path is:" + currentPath + "/" + filename);
      FileUtils.copyFile(scrFile, new File(currentPath + "\\" + filename));
    } catch (IOException e) {
      System.out.println("Can't save screenshot");
      e.printStackTrace();
    } finally {
      System.out.println("screen shot finished, it's in " + currentPath + " folder");
    }
  }
  /**
   * Take a screenshot of the current page and embed it into the log.<br>
   * <br>
   * The <b>filename</b> argument specifies the name of the file to write the screenshot into. If no
   * filename is given, the screenshot is saved into file selenium-screenshot-&lt;counter&gt;.png
   * under the directory where the Robot Framework log file is written into. The filename is also
   * considered relative to the same directory, if it is not given in absolute format.<br>
   * <br>
   * A CSS can be used to modify how the screenshot is taken. By default the background color is
   * changed to avoid possible problems with background leaking when the page layout is somehow
   * broken.<br>
   *
   * @param filename Default=NONE. Name of the file to write.
   */
  @RobotKeyword
  @ArgumentNames({"filename=NONE"})
  public void capturePageScreenshot(String filename) {
    File logdir = logging.getLogDir();
    File path = new File(logdir, normalizeFilename(filename));
    String link = Robotframework.getLinkPath(path, logdir);

    TakesScreenshot takesScreenshot = ((TakesScreenshot) browserManagement.getCurrentWebDriver());
    if (takesScreenshot == null) {
      logging.warn("Can't take screenshot. No open browser found");
      return;
    }

    byte[] png = takesScreenshot.getScreenshotAs(OutputType.BYTES);
    writeScreenshot(path, png);

    logging.html(
        String.format(
            "</td></tr><tr><td colspan=\"3\"><a href=\"%s\"><img src=\"%s\" width=\"800px\"></a>",
            link, link));
  }