Questo è quello di prendere Screenshot utilizzando Selenium Webdriver e di salvare in MS SQL Database. L'immagine sta salvando nel database con il nome dell'immagine allegato con il nome del browser, la versione del browser, il numero di sequenza e il nome dello scenario. Questo codice è supportato da Chrome, Firefox, Internet Explorer e Safari.
C'è un'altra opzione tra la riga 63-70 per salvare l'immagine in una cartella locale, se lo desideri. È possibile impostare una cartella nel sistema locale e salva l'immagine nella cartella specificata nel formato png e nel formato byte nel database MSSql.
Scatta screenshot e salva nel database MSSQL utilizzando Java e Selenium Webriver
pacchetto com.main;
import java.awt.image.BufferedImage;
import java.io.File;
importare java.io.FileInputStream;
import java.io.IOException;
importare java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
importare java.util.Properties;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
importare 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;
public class ImageSave {
private static int imageSeqNo = 0;
stringa statica privata scenName;
private static WebDriver browserDriver;
private static int browserWidth;
private static int browserHeight;
browser String statico pubblico;
driver WebDriver statico pubblico;
public static int getBrowserWidth () {
return browserWidth;
}
public static int getBrowserHeight () {
return browserHeight;
}
public static String getScenName () {
restituire scenName;
}
public static int getImageSeqNo () {
return imageSeqNo;
}
main static void main (String [] args) genera Exception {
// BrowserDriver.getCurrentDriver ();
}
public static void addScreenshot (driver WebDriver) genera IOException,
ClassNotFoundException, InterruptedException {
byte [] scrByte = getScreenShotBytes (driver);
browser = getBrowserAndVersion ();
String scenarioName = getScenName ();
String imageName = scenarioName + "" + browser + ""
+ System.currentTimeMillis () + ".png";
File scrFile = getScreenShotFile (driver);
String screenWidthHeight = getImageWidthHeight (scrFile);
// se si desidera salvare l'immagine della schermata nel sistema locale, abilitare la riga di seguito
// FileUtils.copyFile (scrFile, new File ("C: // screenshot //" + imageName));
insertImageDB (scrByte, scenarioName, imageName, screenWidthHeight,
browser);
Thread.sleep (1000);
}
public static String getImageWidthHeight (File imageFile) genera IOException {
BufferedImage bimg = ImageIO.read (imageFile);
int imageWidth = bimg.getWidth ();
int imageHeight = bimg.getHeight ();
if (imageWidth! = 0) {
return imageWidth + "x" + imageHeight;
} altro {
ritorna "FullScreenx" + imageHeight;
}
}
file statico pubblico getScreenShotFile (driver WebDriver) {
// WebDriver driverA = new Augmenter (). Augment (driver);
return ((TakesScreenshot) driver) .getScreenshotAs (OutputType.FILE);
}
byte statico pubblico [] getScreenShotBytes (driver WebDriver) {
return ((TakesScreenshot) driver) .getScreenshotAs (OutputType.BYTES);
}
public static void insertImageDB (byte [] imageByte, String scenName,
String imageName, String screenWidthHeight, String browser)
lancia ClassNotFoundException {
Proprietà dbProp = new Properties ();
InputStream dbPropInput = null;
ResultSet rs = null;
PreparedStatement ps = null;
Connessione con = null;
// setImageSeqNo (getImageSeqNo () + 1);
int seqNo = getImageSeqNo ();
System.out.println (scenName + "-" + browser + "-"
+ screenWidthHeight + "- Shot Number:" + seqNo);
provare {
String propPath = ". \ Src \ test \ resources \ props \ dbConnect.properties";
dbPropInput = new FileInputStream (propPath);
dbProp.load (dbPropInput); // carica il file delle proprietà
String dbDriver = (dbProp.getProperty ("dbDriver"));
String dbURL = (dbProp.getProperty ("dbURL"));
String stPro = (dbProp.getProperty ("SPSql"));
Class.forName (dbDriver);
con = DriverManager.getConnection (dbURL);
ps = con.prepareStatement (stPro);
data java.util.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); // Il valore di timeout può disabilitarsi in seguito, immagine
ps.setString (1, "Progetto");
ps.setString (2, scenName);
ps.setString (3, browser);
ps.setString (4, screenWidthHeight);
ps.setTimestamp (5, sqlTimestamp);
ps.setInt (6, seqNo);
ps.setString (7, imageName);
ps.setBytes (8, imageByte);
// commenta sotto la riga per disabilitare l'aggiornamento del database
ps.executeUpdate ();
} catch (IOException e) {
e.printStackTrace ();
} catch (SQLException e) {
e.printStackTrace ();
} finalmente {
provare {
if (dbPropInput! = null)
dbPropInput.close ();
if (rs! = null)
rs.close ();
if (ps! = null)
ps.close ();
se (con! = null)
con.Close ();
} catch (Exception e) {
e.printStackTrace ();
}
}
}
public static String getBrowserAndVersion () {
String browser_version = null;
Capabilities cap = ((RemoteWebDriver) driver) .getCapabilities ();
String browsername = cap.getBrowserName ();
// Questo blocco per scoprire il numero di versione di IE
if ("internet explorer" .equalsIgnoreCase (browsername)) {
String uAgent = (String) ((JavascriptExecutor) driver)
.executeScript ("return navigator.userAgent;");
System.out.println (uagent);
// uAgent restituisce come "MSIE 8.0 Windows" per IE8
if (uAgent.contains ("MSIE") && uAgent.contains ("Windows")) {
browser_version = uAgent.substring (uAgent.indexOf ("MSIE") + 5,
uAgent.indexOf ("Windows") - 2);
} else if (uAgent.contains ("Trident / 7.0")) {
browser_version = "11.0";
} altro {
browser_version = "00";
}
} altro {
// Versione del browser per Firefox e Chrome
browser_version = cap.getVersion (); // .split (".") [0];
}
String browserversion = browser_version.substring (0,
browser_version.indexOf ( “”));
String bVersion = String.format ("% 02d", Integer.parseInt (browserversion));
return ((browsername) + "_" + bVersion);
}
public static String browserNameConvert (String browser_name) {
if (org.apache.commons.lang3.StringUtils.containsIgnoreCase (
browser_name, "explorer")) {
restituire "IE";
} else if (org.apache.commons.lang3.StringUtils.containsIgnoreCase (
browser_name, "firefox")) {
restituire "FF";
} else if (org.apache.commons.lang3.StringUtils.containsIgnoreCase (
browser_name, "chrome")) {
restituire "CH";
} else if (org.apache.commons.lang3.StringUtils.containsIgnoreCase (
browser_name, "safari")) {
restituire "SF";
} altro {
restituire "NA";
}
}
}
File di proprietà della connessione al database MSSQL con credenziali utente e procedura di archiviazione
################## dbConnect.properties ##################
dbDriver = com.microsoft.sqlserver.jdbc.SQLServerDriver
dbURL = jdbc: sqlserver: // YOURSERVERURL; database = DATABASENAME; user = username; password = PASSWORD
SPSql = {call STOREPROCEDURE (?, ?, ?, ?, ?, ?, ?, ?)}
Per salvare nel database, la procedura di archiviazione già creata in questo esempio e tutti questi dati per la connessione al database e la procedura di archiviazione sono elencati nel file delle proprietà.
Il nome del browser viene convertito in 2 forme corte come FF (Firefox), CH (Chrome) ecc. Questo è anche in grado di trovare la versione del browser a livello di programmazione, incluso il numero di versione di Internet Explorer. Questo script supporta le versioni di Internet Explorer 8, 9, 10, 11.