
com.github.markusbernhardt.selenium2library.keywords.Screenshot Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of robotframework-selenium2library-java Show documentation
Show all versions of robotframework-selenium2library-java Show documentation
Java port of the Selenium 2 (WebDriver) Python library for Robot Framework
package com.github.markusbernhardt.selenium2library.keywords;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import com.github.markusbernhardt.selenium2library.utils.Robotframework;
public abstract class Screenshot extends RunOnFailure {
// ##############################
// Keywords
// ##############################
public void capturePageScreenshot() {
capturePageScreenshot(null);
}
public void capturePageScreenshot(String filename) {
String normalizedFilename = normalizeFilename(filename);
File logdir = getLogDir();
File path = new File(logdir, normalizedFilename);
byte[] png = ((TakesScreenshot) webDriverCache.getCurrent())
.getScreenshotAs(OutputType.BYTES);
writeScreenshot(path, png);
String link = Robotframework.getLinkPath(path, logdir);
html(String
.format("
",
link, link));
}
// ##############################
// Internal Methods
// ##############################
private int screenshotIndex = 0;
private void writeScreenshot(File path, byte[] png) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(path);
fos.write(png);
fos.flush();
} catch (IOException e) {
warn(String.format("Can't write screenshot '%s'",
path.getAbsolutePath()));
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
warn("Can't even close stream");
}
}
}
}
private String normalizeFilename(String filename) {
if (filename == null) {
screenshotIndex++;
filename = String.format("selenium-screenshot-%d.png",
screenshotIndex);
}
else {
filename = filename.replace('/', File.separatorChar);
}
return filename;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy