Example #1
0
  /**
   * This method takes screen shot of web page. After taking screen shot, it saves that to file with
   * test method name and time stamp. This also writes a comment (having link to screenshot) to
   * selenium logging file created through LoggingSelenium.
   */
  public void captureScreenShot(LoggingSelenium selenium, String screenshotsResultsPath) {

    try {

      final String SCREENSHOT_PATH = "screenshots";
      /*
      String screenshotsResultsPath = new File(RESULTS_BASE_PATH
      		+ File.separator + SCREENSHOT_PATH).getAbsolutePath();


      if (!new File(screenshotsResultsPath).exists()) {
      	new File(screenshotsResultsPath).mkdirs();
      }*/

      String imageName = "Image" + timeStampForFileName() + ".png";

      String finalImage = screenshotsResultsPath + File.separator + imageName;

      String browser = ConfigFileReader.getConfigItemValue("browser");
      if (browser.equals("*chrome") || browser.equals("*firefox") || browser.equals("*iehta")) {
        // call captureEntirePageScreenshotToString method and it will
        // return Base64 encoded string referring to screen shot
        String base64Screenshot = selenium.captureEntirePageScreenshotToString("");

        // Decode that Base64 encoded string to get actual screen shot.
        // Finally this screen shot will be written to file object.
        byte[] decodedScreenshot = Base64.decode(base64Screenshot);

        // create instance of FileOutputStream by passing it filename
        FileOutputStream fos = new FileOutputStream(new File(finalImage));

        // Write decoded screenshot to fileoutputstream object
        fos.write(decodedScreenshot);
        fos.close();
      } else {
        selenium.captureScreenshot(finalImage);
      }

      String failedSS =
          "<a href=\"file://" + finalImage + "\">Screenshot for previous action " + "</a>";
      logger.debug(finalImage);
      // selenium.logComment(failedSS);
      selenium.logComment(
          "<a href=\""
              + SCREENSHOT_PATH
              + "/"
              + imageName
              + "\"><img src=\""
              + SCREENSHOT_PATH
              + "/"
              + imageName
              + "\" alt=\"Page Screen Shot\" width=\"150\" height=\"300\" border=\"5\"/></a>");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }