org.openqa.selenium.TakesScreenshot类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(135)

本文整理了Java中org.openqa.selenium.TakesScreenshot类的一些代码示例,展示了TakesScreenshot类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TakesScreenshot类的具体详情如下:
包路径:org.openqa.selenium.TakesScreenshot
类名称:TakesScreenshot

TakesScreenshot介绍

[英]Indicates a driver that can capture a screenshot and store it in different ways.

Example usage:

File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); 
String screenshotBase64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);

[中]

代码示例

代码示例来源:origin: selenide/selenide

protected File takeScreenshotInMemory(TakesScreenshot driver) {
 try {
  return driver.getScreenshotAs(FILE);
 }
 catch (Exception e) {
  log.log(SEVERE, "Failed to take screenshot in memory", e);
  return null;
 }
}

代码示例来源:origin: apache/geode

private void takeScreenshot(String screenshotName) {
 WebDriver driver = this.webDriverSupplier.get();
 if (driver instanceof TakesScreenshot) {
  File tempFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
  try {
   File screenshot = new File("build/screenshots/" + screenshotName + ".png");
   FileUtils.copyFile(tempFile, screenshot);
   System.err.println("Screenshot saved to: " + screenshot.getCanonicalPath());
  } catch (IOException e) {
   throw new Error(e);
  }
 }
}

代码示例来源:origin: selenide/selenide

protected File takeScreenshotImage(Config config, TakesScreenshot driver, String fileName) {
 try {
  File scrFile = driver.getScreenshotAs(FILE);
  File imageFile = new File(config.reportsFolder(), fileName + ".png");
  try {
   copyFile(scrFile, imageFile);
  }
  catch (IOException e) {
   log.log(SEVERE, "Failed to save screenshot to " + imageFile, e);
  }
  return imageFile;
 }
 catch (WebDriverException e) {
  log.log(SEVERE, "Failed to take screenshot to " + fileName + " because of " + e);
  return null;
 }
}

代码示例来源:origin: cloudfoundry/uaa

public static void takeScreenShot(String prefix, WebDriver webDriver) {
  File scrFile = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
  try {
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd-HHmmss.SSS");
    String now = format.format(new Date(System.currentTimeMillis()));
    FileUtils.copyFile(scrFile, new File("build/reports/", prefix + now + ".png"));
  } catch (IOException e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: galenframework/galen

public static File takeScreenshot(WebDriver driver) throws IOException {
  File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  if (GalenConfig.getConfig().shouldAutoresizeScreenshots()) {
    BufferedImage image = Rainbow4J.loadImage(file.getAbsolutePath());
    File newFile = File.createTempFile("screenshot", ".png");
    image = GalenUtils.resizeScreenshotIfNeeded(driver, image);
    Rainbow4J.saveImage(image, newFile);
    return newFile;
  }
  else return file;
}

代码示例来源:origin: galenframework/galen

public static File makeFullScreenshot(WebDriver driver) throws IOException, InterruptedException {
  byte[] bytes = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
  BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes));
  int capturedWidth = image.getWidth();
      scroll += scrollOffset;
      scrollVerticallyTo(driver, scroll);
      BufferedImage nextImage = ImageIO.read(new ByteArrayInputStream(((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES)));
      g2dTile.drawImage(nextImage, 0, (i+1) * capturedHeight, null);
      scroll += scrollOffset;
      scrollVerticallyTo(driver, scroll);
      BufferedImage nextImage = ImageIO.read(new ByteArrayInputStream(((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES)));
      BufferedImage lastPart = nextImage.getSubimage(0, nextImage.getHeight() - (int)(((double)leftover) * devicePixelRatio), nextImage.getWidth(), leftover);
      g2dTile.drawImage(lastPart, 0, times * capturedHeight, null);

代码示例来源:origin: selenide/selenide

private BufferedImage takeScreenshotAsImage(WebDriver webdriver, WebElement element) {
 if (!(webdriver instanceof TakesScreenshot)) {
  log.warning("Cannot take screenshot because browser does not support screenshots");
  return null;
 }
 byte[] screen = ((TakesScreenshot) webdriver).getScreenshotAs(OutputType.BYTES);
 Point elementLocation = element.getLocation();
 try {
  BufferedImage img = ImageIO.read(new ByteArrayInputStream(screen));
  int elementWidth = element.getSize().getWidth();
  int elementHeight = element.getSize().getHeight();
  if (elementWidth > img.getWidth()) {
   elementWidth = img.getWidth() - elementLocation.getX();
  }
  if (elementHeight > img.getHeight()) {
   elementHeight = img.getHeight() - elementLocation.getY();
  }
  return img.getSubimage(elementLocation.getX(), elementLocation.getY(), elementWidth, elementHeight);
 }
 catch (IOException e) {
  log.log(SEVERE, "Failed to take screenshot of " + element, e);
  return null;
 }
 catch (RasterFormatException e) {
  log.warning("Cannot take screenshot because element is not displayed on current screen position");
  return null;
 }
}

代码示例来源:origin: cloudfoundry/uaa

public void debugPage(String className, String description) {
  TakesScreenshot takesScreenshot = (TakesScreenshot) browser;
  File scrFile = takesScreenshot.getScreenshotAs(OutputType.FILE);
  File destFile = getDestinationFile(className, description);
  try {
    FileUtils.copyFile(scrFile, destFile);
  } catch (IOException ioe) {
    throw new RuntimeException(ioe);
  }
  File pageSourceFile = getDestinationFile(className, description + ".html");
  String pageSource = browser.getPageSource();
  try {
    FileUtils.write(pageSourceFile, pageSource);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: selenide/selenide

return null;
byte[] screen = ((TakesScreenshot) webdriver).getScreenshotAs(OutputType.BYTES);
Point iframeLocation = iframe.getLocation();
BufferedImage img;

代码示例来源:origin: stackoverflow.com

public class ScreenshotListener extends RunListener {

  private TakesScreenshot screenshotTaker;

  @Override
  public void testFailure(Failure failure) throws Exception {
    File file = screenshotTaker.getScreenshotAs(OutputType.File);
    // do something with your file
  }

}

代码示例来源:origin: org.codelibs.robot/s2robot

@Override
public <X> X getScreenshotAs(final OutputType<X> target)
    throws WebDriverException {
  return ((TakesScreenshot) webDriver).getScreenshotAs(target);
}

代码示例来源:origin: org.codelibs.robot/s2-robot

@Override
public <X> X getScreenshotAs(OutputType<X> target)
    throws WebDriverException {
  return ((TakesScreenshot) webDriver).getScreenshotAs(target);
}

代码示例来源:origin: stackoverflow.com

if (driver instanceof TakesScreenshot) {
  TakesScreenshot ts = (TakesScreenshot) driver;
  File screenshotFile = ts.getScreenshotAs(OutputType.FILE);
  //feel free to move or rename the file as you see fit.
}else{ /* unsupported*/ }

代码示例来源:origin: qaprosoft/carina

/**
 * Makes screenshot of visible part of the page
 *
 * @param augmentedDriver
 *            - webDriver.
 * @exception IOException
 * 
 * @return screenshot image
 */
private static BufferedImage takeVisibleScreenshot(WebDriver augmentedDriver) throws Exception {
  return ImageIO.read(((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE));
}

代码示例来源:origin: io.github.aktoluna/slnarch-core

public <X> X getScreenshotAs(OutputType<X> target) throws WebDriverException {
 if (driver instanceof TakesScreenshot) {
  return ((TakesScreenshot) driver).getScreenshotAs(target);
 }
 throw new UnsupportedOperationException(
   "Underlying driver instance does not support taking screenshots");
}

代码示例来源:origin: com.lohika.alp/alp-selenium

@Override
public <X> X getScreenshotAs(OutputType<X> target)
    throws WebDriverException {
  if (driver instanceof TakesScreenshot) {
    return ((TakesScreenshot) driver).getScreenshotAs(target);
  }
  throw new UnsupportedOperationException(
      "Underlying driver instance does not support taking screenshots");
}

代码示例来源:origin: org.bitbucket.iamkenos/cissnei-selenium

public Object takeScreenshot(OutputType outputType) {
  try {
    return ((TakesScreenshot) webDriver).getScreenshotAs(outputType);
  } catch (Exception e) {
    LOGGER.error(e.getMessage());
    throw e;
  }
}

代码示例来源:origin: stackoverflow.com

private static void snapshotBrowser(TakesScreenshot driver, String screenSnapshotName, File browserFile) {
   try {
     File scrFile = driver.getScreenshotAs(OutputType.FILE);
     log.info("PNG browser snapshot file name: \"{}\"", browserFile.toURI().toString());
     FileUtils.deleteQuietly(browserFile);
     FileUtils.moveFile(scrFile, browserFile);
   } catch (Exception e) {
     log.error("Could not create browser snapshot: " + screenSnapshotName, e);
   }
 }

代码示例来源:origin: bonigarcia/selenium-jupiter

void logBase64Screenshot(WebDriver driver, String fileName) {
  try {
    String screenshotBase64 = ((TakesScreenshot) driver)
        .getScreenshotAs(BASE64);
    log.info("Screenshot (in Base64) at the end of {} "
        + "(copy&paste this string as URL in browser to watch it):\r\n"
        + "data:image/png;base64,{}", fileName, screenshotBase64);
  } catch (Exception e) {
    log.trace("Exception getting screenshot in Base64", e);
  }
}

代码示例来源:origin: com.twosigma.webtau/webtau-browser

private BufferedImage takeBufferedImage() {
  byte[] screenshotBytes = screenshotTaker.getScreenshotAs(OutputType.BYTES);
  try {
    return ImageIO.read(new ByteArrayInputStream(screenshotBytes));
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

相关文章

TakesScreenshot类方法