org.openqa.selenium.TakesScreenshot.getScreenshotAs()方法的使用及代码示例

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

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

TakesScreenshot.getScreenshotAs介绍

[英]Capture the screenshot and store it in the specified location.

For WebDriver extending TakesScreenshot, this makes a best effort depending on the browser to return the following in order of preference:

  • Entire page
  • Current window
  • Visible portion of the current frame
  • The screenshot of the entire display containing the browser

For WebElement extending TakesScreenshot, this makes a best effort depending on the browser to return the following in order of preference: - The entire content of the HTML element - The visible portion of the HTML element
[中]捕获屏幕截图并将其存储在指定位置。
对于WebDriver扩展TakesScreenshot,这将尽最大努力根据浏览器返回以下内容:
*整页
*当前窗口
*当前帧的可见部分
*包含浏览器的整个显示屏的屏幕截图
对于WebElement扩展TakesScreenshot,这将根据浏览器尽最大努力按优先顺序返回以下内容:-HTML元素的整个内容-HTML元素的可见部分

代码示例

代码示例来源: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: 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: 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);
  }
}

代码示例来源:origin: com.wso2telco.test/uitest-framework

public void takeScreenshot(WebDriver webDriver) {

  try {
    source = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);

  FileUtils.copyFile(source, new File ("./ScreenShots/screenShotName.png"));
  System.out.println("Screenshot Taken!!!!");

  } catch (IOException e) {
    e.printStackTrace();
  } 
}

代码示例来源:origin: com.vaadin/vaadin-testbench-core

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

代码示例来源:origin: com.github.wiselenium/wiselenium-elements

@Override
public void takeScreenShot(String fileName) {
  File scrFile = ((TakesScreenshot) this.driver).getScreenshotAs(OutputType.FILE);
  try {
    File destFile = new File(fileName);
    FileUtils.copyFile(scrFile, destFile);
  } catch (IOException e) {
    throw new ScreenShotException(e);
  }
}

代码示例来源:origin: gradle.plugin.GoBqa/gradle-plugin

/**
 * El nombre debe leerse: Get Sequence Screen Shot. Obtiene un screenshot cuyo nombre contiene un
 * Nro que va variando con cada llamada.
 * @throws Exception
 */
public void getSeqScreenShot() throws Exception
{
  File scrFile = ((TakesScreenshot)webdriver).getScreenshotAs(OutputType.FILE);
  //The below method will save the screen shot in d drive with name "screenshot.png"
  FileUtils.copyFile(scrFile, new File(sceernSeqshotName()));
}

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

public<T> Object getScreenshotAs(OutputType<T> outputType) {
  Augmenter augmenter = new Augmenter(); 
  TakesScreenshot ts = (TakesScreenshot) augmenter.augment(driver);
  return ts.getScreenshotAs(outputType);
}

相关文章

TakesScreenshot类方法