これはSelenium Webdriverを使用してスクリーンショットを撮り、MS SQLデータベースに保存することです。 画像は、ブラウザ名、ブラウザのバージョン、シーケンス番号、シナリオ名を追加した画像名でデータベースに保存されます。 このコードはChrome、Firefox、Internet Explorer、およびSafariでサポートされています。
必要に応じて、画像をローカルフォルダに保存するための63〜70行目にもう1つオプションがあります。 あなたは、ローカルシステムでフォルダを設定することができます、そしてそれはあなたがMSSqlデータベースでpng形式とバイト形式で指定したフォルダに絵を保存します。
スクリーンショットを取り、JavaとSelenium Webriverを使用してMSSQLデータベースに保存します。
パッケージcom.main;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
パブリッククラスImageSave {
プライベート静的int型imageSeqNo = 0。
private static String scenName;
プライベート静的WebDriver browserDriver。
private static int browserWidth;
private static int browserHeight;
public static Stringブラウザ。
公開静的WebDriverドライバ。
public static int getBrowserWidth(){
browserWidthを返します。
}
public static int getBrowserHeight(){
ブラウザの高さを返す。
}
public static String getScenName(){
scenNameを返します。
}
public static int getImageSeqNo(){
imageSeqNoを返します。
}
public static void main(String [] args)は例外を投げます{
// BrowserDriver.getCurrentDriver();
}
public static void addScreenshot(WebDriverドライバ)がIOExceptionをスローします、
ClassNotFoundException、InterruptedException {
byte [] scrByte = getScreenShotBytes(ドライバ);
browser = getBrowserAndVersion();
String scenarioName = getScenName();
文字列imageName = scenarioName +”” + browser +””
+ System.currentTimeMillis()+ ".png";
ファイルscrFile = getScreenShotFile(ドライバ);
文字列screenWidthHeight = getImageWidthHeight(scrFile);
//スクリーンショットの画像をローカルシステムに保存する場合は、以下の行を有効にします
// FileUtils.copyFile(scrFile、new File(“ C:// screenshot //” + imageName));
insertImageDB(scrByte、scenarioName、imageName、screenWidthHeight、
ブラウザ);
Thread.sleep(1000);
}
public static String getImageWidthHeight(File imageFile)はIOExceptionをスローします。
BufferedImage bimg = ImageIO.read(imageFile);
int imageWidth = bimg.getWidth();
int imageHeight = bimg.getHeight();
if(imageWidth!= 0){
imageWidth +“ x” + imageHeightを返します。
その他{
“ FullScreenx” + imageHeightを返します。
}
}
パブリック静的ファイルgetScreenShotFile(WebDriverドライバ){
// WebDriver driverA = new Augmenter()。augment(driver);
return((TakesScreenshot)ドライバ).getScreenshotAs(OutputType.FILE);
}
public static byte [] getScreenShotBytes(WebDriverドライバ){
return((TakesScreenshot)ドライバ).getScreenshotAs(OutputType.BYTES);
}
public static void insertImageDB(byte [] imageByte、String scenName、
文字列imageName、文字列screenWidthHeight、文字列ブラウザ)
ClassNotFoundExceptionをスローします{
プロパティdbProp = new Properties();
InputStream dbPropInput = null。
ResultSet rs = null。
PreparedStatement ps = null;
接続con = null。
// setImageSeqNo(getImageSeqNo()+ 1);
int seqNo = getImageSeqNo();
System.out.println(scenName +” - ” + browser +” - ”
+ screenWidthHeight + " - - ショット番号:" + seqNo);
試してください{
String propPath =“。\ src \ test \ resources \ props \ dbConnect.properties”;
dbPropInput = new FileInputStream(propPath);
dbProp.load(dbPropInput); //プロパティファイルを読み込む
文字列dbDriver =(dbProp.getProperty(“ dbDriver”));
文字列dbURL =(dbProp.getProperty(“ dbURL”));
文字列stPro =(dbProp.getProperty(“ SPSql”));
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(“ Image Timestamp =” + sqlTimestamp);
ps.setEscapeProcessing(true);
ps.setQueryTimeout(90); //タイムアウト値は後で無効になる可能性がある
ps.setString(1、“ Project”);
ps.setString(2、scenName);
ps.setString(3、ブラウザ);
ps.setString(4、screenWidthHeight);
ps.setTimestamp(5、sqlTimestamp);
ps.setInt(6、seqNo);
ps.setString(7、imageName);
ps.setBytes(8、imageByte);
//データベースの更新を無効にする
ps.executeUpdate();
catch(IOException e){
e.printStackTrace();
catch(SQLException e){
e.printStackTrace();
} 最後に {
試してください{
if(dbPropInput!= null)
dbPropInput.close();
if(rs!= null)
rs.close();
if(ps!= null)
ps.close();
if(con!= null)
con.close();
catch(例外e){
e.printStackTrace();
}
}
}
public static String getBrowserAndVersion(){
文字列browser_version = null。
Capabilities cap =((RemoteWebDriver)driver).getCapabilities();
String browsername = cap.getBrowserName();
// IEのバージョン番号を調べるためのこのブロック
if(“インターネットエクスプローラ” .equalsIgnoreCase(browsername)){
文字列uAgent =(文字列)((JavascriptExecutor)ドライバ)
.executeScript(“ return navigator.userAgent;”);
System.out.println(uAgent);
// IE8の場合、uAgentは「MSIE 8.0 Windows」として戻ります
if(uAgent.contains(“ MSIE”)&& uAgent.contains(“ Windows”)){
browser_version = uAgent.substring(uAgent.indexOf( "MSIE")+ 5、
uAgent.indexOf(“ Windows”) - 2);
そうでなければ(uAgent.contains(“ Trident / 7.0”))){
browser_version =“ 11.0”;
その他{
browser_version = "00";
}
その他{
// FirefoxとChromeのブラウザバージョン
browser_version = cap.getVersion(); // .split(“。”)[0];
}
文字列browserversion = browser_version.substring(0、
browser_version.indexOf(“。”));
文字列bVersion = String.format(“%02d”、Integer.parseInt(browserversion));
return((ブラウザ名)+“ _” + bVersion);
}
public static String browserNameConvert(String browser_name){
if(org.apache.commons.lang3.StringUtils.containsIgnoreCase(
browser_name、“ explorer”)){
“ IE”を返します。
else if(org.apache.commons.lang3.StringUtils.containsIgnoreCase(
browser_name、“ firefox”)){
“ FF”を返します。
else if(org.apache.commons.lang3.StringUtils.containsIgnoreCase(
browser_name、“ chrome”)){
“ CH”を返します。
else if(org.apache.commons.lang3.StringUtils.containsIgnoreCase(
browser_name、“ safari”)){
"SF"を返します。
その他{
NAを返します。
}
}
}
ユーザー資格情報とストアドプロシージャを含むMSSQLデータベース接続プロパティファイル
################## dbConnect.properties ##################
dbDriver = com.microsoft.sqlserver.jdbc.SQLServerDriver
dbURL = jdbc:sqlserver:// YOURSERVERURL;データベース= DATABASENAME;ユーザー= USERNAME;パスワード= PASSWORD
SPSql = {call STOREPROCEDURE(?、?、?、?、?、?、?、?)}
データベースに保存するには、この例ですでに作成されているストアドプロシージャと、データベース接続およびストアドプロシージャに関するこれらすべてのデータがプロパティファイルにリストされています。
ブラウザ名をFF(Firefox)、CH(Chrome)などのように2文字の短い形式に変換します。これは、Internet Explorerのバージョン番号を含むプログラムでブラウザのバージョンを見つけることもできます。 このスクリプトは、Internet Explorerのバージョン8、9、10、11をサポートしています。