Điều này là để chụp ảnh màn hình bằng Selenium WebSearch và lưu trong Cơ sở dữ liệu MS SQL. Hình ảnh đang lưu trong cơ sở dữ liệu với tên hình ảnh được nối với tên trình duyệt, phiên bản trình duyệt, số thứ tự và tên kịch bản. Mã này được hỗ trợ Chrome, Firefox, Internet Explorer và Safari.
Có thêm một tùy chọn giữa dòng 63-70 để lưu ảnh vào thư mục cục bộ nếu bạn muốn. Bạn có thể đặt một thư mục trong hệ thống cục bộ và nó sẽ lưu ảnh vào thư mục bạn đã chỉ định ở định dạng png và định dạng byte trong Cơ sở dữ liệu MSSql.
Chụp ảnh màn hình và lưu vào Cơ sở dữ liệu MSSQL bằng Java và Selenium Webriver
gói com.main;
nhập java.awt.image.BufferedImage;
nhập java.io.File;
nhập java.io.FileInputStream;
nhập java.io.IOException;
nhập java.io.InputStream;
nhập java.sql. Kết nối;
nhập java.sql.DriverManager;
nhập java.sql.PreparedStatement;
nhập java.sql.ResultSet;
nhập java.sql.SQLException;
nhập java.util.Properies;
nhập java.util.conc hiện.TimeUnit;
nhập javax.imageio.ImageIO;
nhập org.openqa.selenium.Capabilities;
nhập org.openqa.selenium.JavascriptExecutor;
nhập org.openqa.selenium.OutputType;
nhập org.openqa.selenium.TakesSc mãi;
nhập org.openqa.selenium.WebDriver;
nhập org.openqa.selenium.remote.RemoteWebDriver;
lớp công khai ImageSave {
private int int imageSeqNo = 0;
chuỗi tĩnh tĩnh ScenName;
trình duyệt WebDriver tĩnh riêngDriver;
private int int browserWidth;
private int intHeight;
trình duyệt chuỗi tĩnh công khai;
trình điều khiển WebDriver tĩnh;
công khai tĩnh int getBrowserWidth () {
trả lại trình duyệtWidth;
}
công tĩnh int getBrowserHeight () {
trả lại trình duyệtHight;
}
Chuỗi tĩnh công khai getScenName () {
trả về perspectiveName;
}
công khai int int getImageSeqNo () {
trả lại hình ảnhSeqNo;
}
public static void main (String [] args) throws Exception {
// BrowserDriver.getCienDriver ();
}
public static void addSc mãi (trình điều khiển WebDriver) ném IOException,
ClassNotFoundException, Interrupttedception {
byte [] ScrByte = getScreenShotBytes (trình điều khiển);
trình duyệt = getBrowserAndVersion ();
Chuỗi scriptName = getScenName ();
Chuỗi imageName = scriptsName + '' '' '' '' '' '' ''
+ System.cienTimeMillis () +.
Tệp ScrFile = getScreenShotFile (trình điều khiển);
Chuỗi màn hìnhWidthHeight = getImageWidthHeight (ScrFile);
// nếu muốn lưu ảnh chụp màn hình trong hệ thống cục bộ, hãy bật dòng bên dưới
// FileUtils.copyFile (ScrFile, new File (khác C: // ảnh chụp màn hình // Khăn + imageName));
insertImageDB (ScrByte, scriptsName, imageName, screenWidthHeight,
trình duyệt);
Thread.s ngủ (1000);
}
chuỗi tĩnh công khai getImageWidthHeight (File imageFile) ném IOException {
BufferedImage bimg = ImageIO.read (imageFile);
int imageWidth = bimg.getWidth ();
int imageHeight = bimg.getHeight ();
if (imageWidth! = 0) {
trả về imageWidth + xóa x Hay + imageHeight;
} khác {
return quay lại FullScreenx trực tuyến + imageHeight;
}
}
Tệp tĩnh công khai getScreenShotFile (trình điều khiển WebDriver) {
// Trình điều khiển WebDriverA = new Augmenter (). Augment (driver);
trình điều khiển return ((TakesSccam)) .getSc mãiAs (OutputType.FILE);
}
byte tĩnh công khai [] getScreenShotBytes (trình điều khiển WebDriver) {
trình điều khiển return ((TakesSc mãi)) .getSc mãiAs (OutputType.BYTES);
}
static static void insertImageDB (byte [] imageByte, String perspectiveName,
Chuỗi imageName, String screenWidthHeight, Chuỗi trình duyệt)
ném ClassNotFoundException {
Thuộc tính dbProp = Thuộc tính mới ();
InputStream dbPropInput = null;
Kết quả rs = null;
PreparedStatement ps = null;
Kết nối con = null;
// setImageSeqNo (getImageSeqNo () + 1);
int seqNo = getImageSeqNo ();
System.out.println (phối cảnh
+ screenWidthHeight + Tấn Đò- Số lần bắn: Giật + seqKhông);
thử {
Chuỗi propPath =, . \ Src \ test \ resource \ props \ dbConnect.properies
dbPropInput = new FileInputStream (propPath);
dbProp.load (dbPropInput); // tải tập tin thuộc tính
Chuỗi dbDriver = (dbProp.getProperty (thời gian dbDriver));
Chuỗi dbURL = (dbProp.getProperty (Thời gian dbURL));
Chuỗi stPro = (dbProp.getProperty (S S SPSl
Class.forName (dbDriver);
con = DriverManager.getConnection (dbURL);
ps = con.prepareStatement (stPro);
java.util.Date date = new java.util.Date ();
java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp (date.getTime ());
System.out.println (Tim Image Timestamp = Hay + sqlTimestamp);
ps.setEscapeProcessing (đúng);
ps.setQueryTimeout (90); // giá trị thời gian chờ có thể vô hiệu hóa sau, hình ảnh
ps.setString (1, Bản Project Project);
ps.setString (2, perspectiveName);
ps.setString (3, trình duyệt);
ps.setString (4, screenWidthHeight);
ps.setTimestamp (5, sqlTimestamp);
ps.setInt (6, seqKhông);
ps.setString (7, imageName);
ps.setBytes (8, imageByte);
// bình luận bên dưới dòng để vô hiệu hóa cập nhật cơ sở dữ liệu
ps.executeUpdate ();
} bắt (IOException e) {
e.printStackTrace ();
} bắt (SQLException e) {
e.printStackTrace ();
} cuối cùng {
thử {
if (dbPropInput! = null)
dbPropInput.c Đóng ();
nếu (rs! = null)
rs.c Đóng ();
if (ps! = null)
ps.c Đóng ();
if (con! = null)
con.close ();
} bắt (Ngoại lệ e) {
e.printStackTrace ();
}
}
}
chuỗi tĩnh công khai getBrowserAndVersion () {
Chuỗi browser_version = null;
Trình điều khiển cap Cap = ((RemoteWebDriver)) .getCapabilities ();
Chuỗi lông mày = cap.getBrowserName ();
// Khối này để tìm ra số Phiên bản IE
if (thám hiểm trên mạng Internet Trực tiếp .equalsIgnoreCase (tên người dùng)) {
Trình điều khiển chuỗi uAgent = (Chuỗi) ((JavascriptExecutor))
.executeScript (Quay trở lại navigator.userAgent; hung);
System.out.println (uAgent);
// uAgent trở lại là phiên bản Windows MSIE 8.0 Windows cho IE8
if (uAgent.contains (Mạo MSIE)) && uAgent.contains (Windows Windows)) {
browser_version = uAgent.subopes (uAgent.indexOf (ED MSIE) + 5,
uAgent.indexOf (Windows Windows ') - 2);
} if if (uAgent.contains (Trident / 7.0)) {
browser_version = Hồi 11.0
} khác {
browser_version = Sau 00 00;
}
} khác {
// Phiên bản trình duyệt cho Firefox và Chrome
browser_version = cap.getVersion (); // .split (., .)) [0];
}
Chuỗi browserversion = browser_version.sub chuỗi (0,
browser_version.indexOf (Thời gian trực tuyến);
Chuỗi bVersion = String.format (Thời gian% 02d Lần, Integer.parseInt (browserversion));
return ((tên người dùng) + mệnh _ hung + bVersion);
}
chuỗi tĩnh công khai browserNameConvert (Chuỗi browser_name) {
if (org.apache.commons.lang3.StringUtils.containsIgnoreCase (
browser_name, người thám hiểm người Hồi giáo)) {
Trả lại IE IE;
} if if (org.apache.commons.lang3.StringUtils.containsIgnoreCase (
browser_name, nội dung firefox trực tuyến)) {
trả lại FF FF;
} if if (org.apache.commons.lang3.StringUtils.containsIgnoreCase (
browser_name, Tiếng chrome chrome)) {
trở về
} if if (org.apache.commons.lang3.StringUtils.containsIgnoreCase (
browser_name, Hồi safariiên)) {
return SF SF;
} khác {
trở về NỮ NA NA;
}
}
}
Tệp thuộc tính kết nối cơ sở dữ liệu MSSQL với thông tin người dùng và Quy trình lưu trữ
################## dbConnect.properies ###################
dbDriver = com.microsoft.sqlserver.jdbc.SQLServerDriver
dbURL = jdbc: sqlserver: // YOUSERVERURL; cơ sở dữ liệu = DATABASENAME; user = USERNAME; password = PASSWORD
SPSql = {gọi STOREPROCEDURE (?, ?, ?, ?, ?, ?, ?, ?)}
Để lưu trong cơ sở dữ liệu, quy trình lưu trữ đã được tạo trong ví dụ này và tất cả các dữ liệu này cho kết nối cơ sở dữ liệu và quy trình lưu trữ được liệt kê trong tệp thuộc tính.
Tên trình duyệt chuyển đổi thành 2 dạng char ngắn như FF (Firefox), CH (Chrome), v.v. Điều này cũng có khả năng tìm phiên bản trình duyệt theo chương trình bao gồm số phiên bản Internet Explorer. Kịch bản này hỗ trợ các phiên bản Internet Explorer 8, 9, 10, 11.