本文整理汇总了Java中org.openqa.selenium.TakesScreenshot.getScreenshotAs方法的典型用法代码示例。如果您正苦于以下问题:Java TakesScreenshot.getScreenshotAs方法的具体用法?Java TakesScreenshot.getScreenshotAs怎么用?Java TakesScreenshot.getScreenshotAs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.openqa.selenium.TakesScreenshot
的用法示例。
在下文中一共展示了TakesScreenshot.getScreenshotAs方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: captureScreenshot
import org.openqa.selenium.TakesScreenshot; //导入方法依赖的package包/类
private static String captureScreenshot(WebDriver driver, String screenshotPath, String ScreenshotName) {
String destinationPath = null;
try {
File destFolder = new File(screenshotPath);
destinationPath = destFolder.getCanonicalPath() + "/" + ScreenshotName + ".png";
// Cast webdriver to Screenshot
TakesScreenshot screenshot = (TakesScreenshot) driver;
File sourceFile = screenshot.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(sourceFile, new File(destinationPath));
} catch (Exception e) {
System.out.println("Error capturing screenshot...\n" + e.getMessage());
e.printStackTrace();
}
return destinationPath;
}
示例2: captureScreenshot
import org.openqa.selenium.TakesScreenshot; //导入方法依赖的package包/类
/**
* This method captures a screenshot
**/
public static void captureScreenshot(WebDriver driver, String screenshotName) {
try {
TakesScreenshot ts = (TakesScreenshot) driver;
File source = ts.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(source, new File(dirPath + "/ " + screenshotName
+ "_" + strDateStamp
+ ".png"));
String ESCAPE_PROPERTY = "org.uncommons.reportng.escape-output";
System.setProperty(ESCAPE_PROPERTY, "false");
URL path = new File(dirPath + "/ " + screenshotName + "_"
+ strDateStamp + ".png").toURI().toURL();
String test = "<a href=" + path + "> click to open screenshot of "
+ screenshotName + "</a>";
Reporter.log(screenshotName + test + "<br>");
Reporter.log("<br>");
}
catch (Exception e) {
System.out.println("Exception while taking screenshot "
+ e.getMessage());
}
}
示例3: snapshot
import org.openqa.selenium.TakesScreenshot; //导入方法依赖的package包/类
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
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:"+filename);
FileUtils.copyFile(scrFile, new File(filename));
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Can't save screenshot");
e.printStackTrace();
}
finally
{
System.out.println("screen shot finished");
}
}
示例4: captureEntirePageScreenshotToString
import org.openqa.selenium.TakesScreenshot; //导入方法依赖的package包/类
public static String captureEntirePageScreenshotToString(final WebDriver driver, final String arg0) {
if (driver == null) {
return "";
}
try {
// Don't capture snapshot for htmlunit
if (WebUIDriver.getWebUIDriver().getBrowser().equalsIgnoreCase(BrowserType.HtmlUnit.getBrowserType())) {
return null;
}
if (WebUIDriver.getWebUIDriver().getBrowser().equalsIgnoreCase(BrowserType.Android.getBrowserType())) {
return null;
}
TakesScreenshot screenShot = (TakesScreenshot) driver;
return screenShot.getScreenshotAs(OutputType.BASE64);
} catch (Exception ex) {
// Ignore all exceptions
ex.printStackTrace();
}
return "";
}
示例5: takeScreenshot
import org.openqa.selenium.TakesScreenshot; //导入方法依赖的package包/类
/**
* Takes screenshot of current page (as .png).
* @param baseName name for file created (without extension),
* if a file already exists with the supplied name an
* '_index' will be added.
* @return absolute path of file created.
*/
public String takeScreenshot(String baseName) {
String result = null;
WebDriver d = driver();
if (!(d instanceof TakesScreenshot)) {
d = new Augmenter().augment(d);
}
if (d instanceof TakesScreenshot) {
TakesScreenshot ts = (TakesScreenshot) d;
byte[] png = ts.getScreenshotAs(OutputType.BYTES);
result = writeScreenshot(baseName, png);
}
return result;
}
示例6: afterScenario
import org.openqa.selenium.TakesScreenshot; //导入方法依赖的package包/类
/**
* Capture a selenium screenshot when a test fails and add it to the Cucumber generated report
* if the scenario has accessed selenium in its lifetime
* @throws InterruptedException
*/
@After
public void afterScenario(Scenario scenario) throws InterruptedException {
log.debug("Scenarion finished");
ScenarioGlobals scenarioGlobals = getCucumberManager().getCurrentScenarioGlobals();
TestEnvironment testNev = getSeleniumManager().getAssociatedTestEnvironment();
if (testNev != null && scenarioGlobals != null) {
boolean scenarioUsedSelenium = testNev.isWebDriverAccessedSince(scenarioGlobals.getScenarioStart());
if (scenarioUsedSelenium) {
if (scenario.isFailed()) {
log.debug("Scenarion failed while using selenium, so capture screenshot");
TakesScreenshot seleniumDriver = (TakesScreenshot) SenBotContext.getSeleniumDriver();
byte[] screenshotAs = seleniumDriver.getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshotAs, "image/png");
}
}
}
getCucumberManager().stopNewScenario();
}
示例7: getScreenShot
import org.openqa.selenium.TakesScreenshot; //导入方法依赖的package包/类
public static String getScreenShot(String fileName, String screensPath) {
WebDriver driver = WebDriverConfig.getDriver();
DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
fileName = (dfm.format(new Date())) + "-" + fileName + ".jpg";
fileName = FileUtils.getValidFileName(fileName);
String filePath = screensPath + fileName;
try {
File screensDir = new File(screensPath);
screensDir.mkdirs();
LOGGER.info("Screenshot: " + filePath);
TakesScreenshot takesScreenshot = (TakesScreenshot) driver;
File screenShot = takesScreenshot.getScreenshotAs(OutputType.FILE);
screenShot.setWritable(true);
File file = new File(filePath);
screenShot.renameTo(file);
} catch (Exception e) {
LOGGER.error("Failed to capture screenshot: ", e);
}
return fileName;
}
示例8: capturePageScreenshot
import org.openqa.selenium.TakesScreenshot; //导入方法依赖的package包/类
/**
* 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-<counter>.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));
}
示例9: takeScreenshot
import org.openqa.selenium.TakesScreenshot; //导入方法依赖的package包/类
@Override
public BufferedImage takeScreenshot(WebDriver webDriver) {
TakesScreenshot takesScreenshot = (TakesScreenshot) webDriver;
ByteArrayInputStream imageArrayStream = new ByteArrayInputStream(takesScreenshot.getScreenshotAs(OutputType.BYTES));
try {
return ImageIO.read(imageArrayStream);
} catch (IOException e) {
throw new RuntimeException("Can not parse screenshot to BufferedImage.", e);
}
}
示例10: createErrorReport
import org.openqa.selenium.TakesScreenshot; //导入方法依赖的package包/类
/**
* Creates a folder named {@code selenium} in the project module's
* {@code target} folder. Here, a screenshot and HTML source code of the
* current page are saved when an error occurs.
*
* @param driver The driver associated with the current browser window.
* @param clazz The current JUnit test class.
* @throws IOException
*/
public static void createErrorReport(WebDriver driver, Class clazz, FrameworkMethod method) throws IOException {
System.err.println("Test failed at " + driver.getCurrentUrl());
// Create Selenium folder for storage of error report files
File targetDir = new File("target");
File seleniumDir = new File(targetDir, "selenium");
seleniumDir.mkdir();
// Save screenshot
TakesScreenshot takesScreenshot = (TakesScreenshot) driver;
File screenshot = takesScreenshot.getScreenshotAs(OutputType.FILE);
File screenshotCopy = new File(seleniumDir, clazz.getSimpleName() + "_" + method.getName() + ".png");
FileUtils.copyFile(screenshot, screenshotCopy);
screenshot.delete();
if (isOnJenkinsServer()) {
System.err.println("Screenshot: " + JENKINS_URL + seleniumDir + "/" + screenshotCopy.getName());
} else {
System.err.println("Screenshot was saved at " + screenshotCopy.getAbsolutePath());
}
// Save HTML source code
File pageSource = new File(seleniumDir, clazz.getSimpleName() + ".html");
BufferedWriter bw = new BufferedWriter(new FileWriter(pageSource));
bw.write(driver.getPageSource());
bw.close();
if (isOnJenkinsServer()) {
System.err.println("Page source code: " + JENKINS_URL + seleniumDir + "/" + pageSource.getName());
} else {
System.err.println("Page source code was saved at " + pageSource.getAbsolutePath());
}
}
示例11: takeScreenShot
import org.openqa.selenium.TakesScreenshot; //导入方法依赖的package包/类
private void takeScreenShot(WebDriver driver, String name) throws IOException {
if(!(driver instanceof TakesScreenshot)) {
return;
}
TakesScreenshot shot = (TakesScreenshot) driver;
byte[] screenshotAs = shot.getScreenshotAs(OutputType.BYTES);
String prefix = String.format("%03d", screenShotCount++) + "_";
File file = new File("build/" + prefix + name + ".png");
file.getParentFile().mkdirs();
file.createNewFile();
FileCopyUtils.copy(screenshotAs, file);
}
示例12: failed
import org.openqa.selenium.TakesScreenshot; //导入方法依赖的package包/类
@Override
protected void failed(Throwable e, Description description) {
TakesScreenshot takesScreenshot = (TakesScreenshot) browser;
File scrFile = takesScreenshot.getScreenshotAs(OutputType.FILE);
File destFile = getDestinationFile(description.getMethodName());
try {
FileUtils.copyFile(scrFile, destFile);
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
示例13: makeScreenshot
import org.openqa.selenium.TakesScreenshot; //导入方法依赖的package包/类
public static void makeScreenshot(WebDriver driver, Scenario scenario) {
LOG.debug("Capturing screenshot for scenario {}", scenario.getName());
if (driver instanceof TakesScreenshot) {
TakesScreenshot screenshotableDriver = (TakesScreenshot) driver;
try {
byte[] screenshot = screenshotableDriver.getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshot, "image/png");
} catch (WebDriverException somePlatformsDontSupportScreenshots) {
LOG.error(somePlatformsDontSupportScreenshots.getMessage());
}
} else {
LOG.warn("This web driver implementation {} cannot create screenshots", driver.getClass().getName());
}
}
示例14: getScreenshotAs
import org.openqa.selenium.TakesScreenshot; //导入方法依赖的package包/类
public <T> T getScreenshotAs(OutputType<T> target) throws WebDriverException {
try {
TakesScreenshot takesScreenshot = webDriver.getClass().isAnnotationPresent(Augmentable.class) ?
(TakesScreenshot) new Augmenter().augment(webDriver) :
(TakesScreenshot) webDriver;
return takesScreenshot.getScreenshotAs(target);
} catch (ClassCastException e) {
throw new WebDriverException(
"Taking screenshots is not supported by the concrete implementation of WebDriver [" + webDriver.getClass() + "].");
}
}
示例15: saveScreenShotAsPng
import org.openqa.selenium.TakesScreenshot; //导入方法依赖的package包/类
public void saveScreenShotAsPng(String fileName) {
TakesScreenshot driver = RemoteWebDriverFactory.screenshotInstance();
try {
File screenShotPng = driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenShotPng, new File(fileName));
} catch (IOException e) {
e.printStackTrace();
}
}