Webdriver中实现区域截图的方式以及如何截取frame中的图片
2016-09-15 22:12
567 查看
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class ScreenShot {
public void screenShotForElement(WebDriver driver,WebElement element, String path,int x,int y) throws InterruptedException {
//截取整个页面的图片
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
//获取元素在所处frame中位置对象
Point p = element.getLocation();
//获取元素的宽与高
int width = element.getSize().getWidth();
int height = element.getSize().getHeight();
//矩形图像对象
Rectangle rect = new Rectangle(width, height);
BufferedImage img = ImageIO.read(scrFile);
//x、y表示加上当前frame的左边距,上边距
BufferedImage dest = img.getSubimage(p.getX()+x, p.getY()+y,rect.width, rect.height);
ImageIO.write(dest, "png", scrFile);
FileUtils.copyFile(scrFile, new File(path));
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class ScreenShot {
public void screenShotForElement(WebDriver driver,WebElement element, String path,int x,int y) throws InterruptedException {
//截取整个页面的图片
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
//获取元素在所处frame中位置对象
Point p = element.getLocation();
//获取元素的宽与高
int width = element.getSize().getWidth();
int height = element.getSize().getHeight();
//矩形图像对象
Rectangle rect = new Rectangle(width, height);
BufferedImage img = ImageIO.read(scrFile);
//x、y表示加上当前frame的左边距,上边距
BufferedImage dest = img.getSubimage(p.getX()+x, p.getY()+y,rect.width, rect.height);
ImageIO.write(dest, "png", scrFile);
FileUtils.copyFile(scrFile, new File(path));
} catch (IOException e) {
e.printStackTrace();
}
}
}
相关文章推荐
- WebDriver中实现对特定的Web区域截图方法
- webdriver-如何实现截图出现黑屏现象
- .net 实现鼠标滑动截取picturebox相关区域高宽图片
- J2ME网络编程(获得Web中的文字,图片,多媒体,与JSP交互)以及网络游戏的实现
- 详细解释如何通过Android自带的方式来实现图片的裁剪——原理分析+解决方案
- UI中如何用纯代码的方式来实现一个图片轮播器
- java中如何实现对已有图片的部分截图
- ApplicationContext的三种实现方式以及在web.xml配置的两种方式
- ApplicationContext的三种实现方式以及在web.xml配置的两种方式
- Selenium2学习-023-WebUI自动化实战实例-021-获取浏览器显示区域大小,通过 WebDriver 截图功能
- ApplicationContext的三种实现方式以及在web.xml配置的两种方式
- Webdriver+Testng实现测试用例失败自动截图功能
- 了解 Web 应用程序的运行方式以及如何提高性能
- 屏幕区域截取图片的实现
- Win form 在pictureBox控件中实现选择图片然后以二进制方式保存到数据库。以及在pictureBox控件显示出来
- 【iOS开发-3】sandbox沙盒介绍以及如何取得沙盒里面各文件的路径,图片导入方式和图片路径获取
- 截图功能实现(二)如何框选区域
- HIGHCHARTS 在WEB生成SVG图片导出到word的详细操作步骤 导出另存为以及文件中文名称的实现
- iOS UITableViewCell 多线程 网络+沙盒的图片加载以及第三方框架SDWebImage方式加载
- 如何 在adobe reader中 截取 画面 清晰的图片 图表 截图(原来画面不清楚,模糊 最为明显) pdf 。word