本文整理汇总了Java中org.openqa.selenium.TakesScreenshot类的典型用法代码示例。如果您正苦于以下问题:Java TakesScreenshot类的具体用法?Java TakesScreenshot怎么用?Java TakesScreenshot使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TakesScreenshot类属于org.openqa.selenium包,在下文中一共展示了TakesScreenshot类的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: screenshot
import org.openqa.selenium.TakesScreenshot; //导入依赖的package包/类
public void screenshot() throws Throwable {
try {
driver = new JavaDriver();
SwingUtilities.invokeAndWait(new Runnable() {
@Override public void run() {
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
if (driver instanceof TakesScreenshot) {
Thread.sleep(1000);
File screenshotAs = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
System.out.println(screenshotAs.getAbsolutePath());
Thread.sleep(20000);
}
} finally {
JavaElementFactory.reset();
}
}
示例3: createScreenshot
import org.openqa.selenium.TakesScreenshot; //导入依赖的package包/类
public static void createScreenshot(String fileName) {
if (SeleniumDriver.getInstance() instanceof TakesScreenshot) {
File fileSrc = ((TakesScreenshot) SeleniumDriver.getInstance()).getScreenshotAs(OutputType.FILE);
try {
File destFile = new File(String.format(SCREENSHOTS_FORMAT, fileName));
FileUtils.copyFile(fileSrc, destFile);
LOG.info("[Screenshot] " + destFile.getAbsolutePath());
} catch (IOException e) {
;
}
}
}
示例4: afterTest
import org.openqa.selenium.TakesScreenshot; //导入依赖的package包/类
/**
* <p>
* Takes screen-shot if the scenario fails
* </p>
*
* @param scenario will be the individual scenario's within the Feature files
* @throws InterruptedException Exception thrown if there is an interuption within the JVM
*/
@After()
public void afterTest(Scenario scenario) throws InterruptedException {
LOG.info("Taking screenshot IF Test Failed");
System.out.println("Taking screenshot IF Test Failed (sysOut)");
if (scenario.isFailed()) {
try {
System.out.println("Scenario FAILED... screen shot taken");
scenario.write(getDriver().getCurrentUrl());
byte[] screenShot = ((TakesScreenshot) getDriver()).getScreenshotAs(OutputType.BYTES);
scenario.embed(screenShot, "image/png");
} catch (WebDriverException e) {
LOG.error(e.getMessage());
}
}
}
示例5: setScreenshot
import org.openqa.selenium.TakesScreenshot; //导入依赖的package包/类
@AfterMethod
public void setScreenshot(ITestResult result) {
if (!result.isSuccess()) {
try {
WebDriver returned = new Augmenter().augment(webDriver);
if (returned != null) {
File f = ((TakesScreenshot) returned).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(f,
new File(SCREENSHOT_FOLDER + result.getName() + SCREENSHOT_FORMAT));
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (ScreenshotException se) {
se.printStackTrace();
}
}
}
示例6: tearDown
import org.openqa.selenium.TakesScreenshot; //导入依赖的package包/类
@After
public void tearDown(Scenario scenario) {
if (scenario.isFailed()) {
try {
logger.info("Test failed, taking screenshot");
byte[] screenshot = ((TakesScreenshot) new Augmenter().augment(getAppiumDriver()))
.getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshot, "image/png");
}
catch (WebDriverException wde) {
System.err.println(wde.getMessage());
}
catch (ClassCastException cce) {
cce.printStackTrace();
}
}
application.tearDown();
}
示例7: afterScenario
import org.openqa.selenium.TakesScreenshot; //导入依赖的package包/类
@After
public void afterScenario( Scenario scenario ) throws Exception
{
try
{
attachLog();
if( scenario.isFailed() )
{
try
{
scenario.write( "Current Page URL is " + getDriver().getCurrentUrl() );
byte[] screenshot;
try
{
screenshot = ( ( TakesScreenshot ) getDriver() ).getScreenshotAs( OutputType.BYTES );
} catch( ClassCastException weNeedToAugmentOurDriverObject )
{
screenshot = ( ( TakesScreenshot ) new Augmenter().augment( getDriver() ) ).getScreenshotAs( OutputType.BYTES );
}
String relativeScrnShotPath = takeScreenshot().substring(takeScreenshot().indexOf("screenshots"));
Reporter.addScreenCaptureFromPath(relativeScrnShotPath, getDriver().getCurrentUrl());
} catch( WebDriverException somePlatformsDontSupportScreenshots )
{
System.err.println( somePlatformsDontSupportScreenshots.getMessage() );
}
}
} finally
{
closeDriverObjects();
}
}
示例8: main
import org.openqa.selenium.TakesScreenshot; //导入依赖的package包/类
public static void main(String[] args) {
String phantomJsPath = "D:/Program Files/PhantomJS/phantomjs-2.1.1-windows/bin/phantomjs.exe";
String savePath = "D:/screenshot.png";
Gospy.custom()
.setScheduler(Schedulers.VerifiableScheduler.custom()
.setPendingTimeInSeconds(60).build())
.addFetcher(Fetchers.TransparentFetcher.getDefault())
.addProcessor(Processors.PhantomJSProcessor.custom()
.setPhantomJsBinaryPath(phantomJsPath)
.setWebDriverExecutor((page, webDriver) -> {
TakesScreenshot screenshot = (TakesScreenshot) webDriver;
File src = screenshot.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File(savePath));
System.out.println("screenshot has been saved to " + savePath);
return new Result<>();
})
.build())
.build().addTask("phantomjs://http://www.zhihu.com").start();
}
示例9: takeScreenShot
import org.openqa.selenium.TakesScreenshot; //导入依赖的package包/类
static Consumer<WebDriver> takeScreenShot() {
return (webDriver) -> {
//take Screenshot
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
outputStream.write(((TakesScreenshot) webDriver).getScreenshotAs(OutputType.BYTES));
//write to target/screenshot-[timestamp].jpg
final FileOutputStream out = new FileOutputStream("target/screenshot-" + LocalDateTime.now() + ".png");
out.write(outputStream.toByteArray());
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
};
}
示例10: createScreenShot
import org.openqa.selenium.TakesScreenshot; //导入依赖的package包/类
public File createScreenShot() {
try {
if (driver == null) {
System.err.println("Report Driver[" + runContext.BrowserName + "] is not initialized");
} else if (isAlive()) {
if (alertPresent()) {
System.err.println("Couldn't take ScreenShot Alert Present in the page");
return ((TakesScreenshot) (new EmptyDriver())).getScreenshotAs(OutputType.FILE);
} else if (driver instanceof MobileDriver || driver instanceof ExtendedHtmlUnitDriver
|| driver instanceof EmptyDriver) {
return ((TakesScreenshot) (driver)).getScreenshotAs(OutputType.FILE);
} else {
return createNewScreenshot();
}
}
} catch (DriverClosedException ex) {
System.err.println("Couldn't take ScreenShot Driver is closed or connection is lost with driver");
}
return null;
}
示例11: onError
import org.openqa.selenium.TakesScreenshot; //导入依赖的package包/类
@Override
protected void onError(Description description, Throwable testFailure) throws Throwable {
super.onError(description, testFailure);
if(Objects.nonNull(screenshotProvider)) {
WebDriver webDriver = seleniumProvider.getWebDriver();
if (webDriver instanceof TakesScreenshot) {
BufferedImage screenshot = screenshotProvider.takeScreenshot(webDriver);
File destFile = TestReportFile.forTest(description).withPostix(".png").build().getFile();
ImageIO.write(screenshot, "PNG", destFile);
LOGGER.info("Saved screenshot as " + destFile.getAbsolutePath());
} else {
testFailure.addSuppressed(
new RuntimeException(
"Could not take screenshot, since webdriver is not a " +
TakesScreenshot.class.getName() + " instance as expected. Actual class is " +
webDriver.getClass().getName()));
}
} else {
LOGGER.info("Due to SreenshotProvider is not specified, no screenshot is taken");
}
}
示例12: captureAndDisplayScreenShot
import org.openqa.selenium.TakesScreenshot; //导入依赖的package包/类
/**
* This method captures the screenshot and places it in the execution report
**/
public static void captureAndDisplayScreenShot(WebDriver ldriver,
ExtentTest eTest) {
Date dtnow2 = new Date();
String strStartDate2 = dtnow2.toString().replace(":", "_");
String strDateStamp2 = strStartDate2.replace(" ", "_");
File src = ((TakesScreenshot) ldriver).getScreenshotAs(OutputType.FILE);
try {
String screenshotPath = dirPath + "_" + strDateStamp2 + ".png";
FileUtils.copyFile(src, new File(screenshotPath));
eTest.addScreenCaptureFromPath(screenshotPath);
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
示例13: 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());
}
}
示例14: saveScreenShot
import org.openqa.selenium.TakesScreenshot; //导入依赖的package包/类
/**
* Save screenshot to file.
*
* @param fileName
*/
public static void saveScreenShot(String fileName){
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
Path destPath = Paths.get(
Logger.getLogger().getLogDirPath().toString()
+ "/" + fileName);
FileUtils.copyFile(scrFile, new File(destPath.toString()));
logger.log("Saved screenshot: " + destPath.toString());
} catch (IOException e) {
logger.logLines(e.getStackTrace().toString());
logger.log("Failed to save screenshot!");
System.out.println();
}
}
示例15: taskScreenShot
import org.openqa.selenium.TakesScreenshot; //导入依赖的package包/类
public static void taskScreenShot(WebDriver driver,File saveFile){
if(saveFile.exists()){
saveFile.delete();
}
byte[] src=((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);//.FILE);linux下非root用户,java创建临时文件存在问题
log.info("截图文件字节长度"+src.length);
try {
FileUtils.writeByteArrayToFile(saveFile, src);
} catch (IOException e) {
e.printStackTrace();
log.error("截图写入失败",e);
}
}