[英]Taking a screenshot of a modal window produces a black image
我在Eclipse下使用IE11和Java。 我沒有在遠程系統上運行測試,也沒有使用RDC。 此測試正在我的本地計算機上運行。
當我嘗試拍攝以下屏幕快照時,Selenium似乎有問題。
這是執行以下代碼時的工作:
File scrFile = ((TakesScreenshot) webdr).getScreenshotAs(OutputType.FILE);
它執行大約三遍,然后繼續執行下一條語句。
這是我截屏時調用的實際方法:
public String captureScreenShot() {
String screenShotLocation = System.getProperty("user.dir");
String TCID = GlobalVars.getInstance().getTCID();
WebDriver webdr = GlobalVars.getInstance().getWebdr();
String screenshotDir = GlobalVars.getInstance().getScreenshotDir();
String methodName = getCallingMethod(0);
String screenShotName = null;
// I'm using the test case ID as the directory name where the image will be stored.
if (screenshotDir == null) {
if (TCID.toLowerCase().contains("like")) {
String[] parsedName = TCID.split(" ");
screenshotDir = parsedName[1];
} else {
screenshotDir = TCID;
}
}
try {
File scrFile = ((TakesScreenshot) webdr).getScreenshotAs(OutputType.FILE);
screenShotName = generateUniqueValue().retStringValue + ".png";
String[] pathSections = GlobalVars.getInstance().getLogDir().split("\\\\");
pathSections[pathSections.length-1] = "";
String path = "";
for (int x = 0; x < pathSections.length-1; x++) {
path = path + pathSections[x] + "\\";
}
screenShotLocation = path + "screenshots\\" + screenshotDir +"\\" + screenShotName;
FileUtils.copyFile(scrFile, new File(screenShotLocation));
} catch (IOException e) {
logMessage(MessType.FAIL, "From Common (" + methodName + ") Sorry, Because I received an exception while trying to capture the a screenshot, a screenshot will not be included.", "System Returned: " + e.toString());
}
return screenShotLocation;
}
條件恰好發生在以下行:
File scrFile = ((TakesScreenshot) webdr).getScreenshotAs(OutputType.FILE);
GlobalVars是一個單例類,主要是Setters和Getters。 這是代碼:
/**
* The GlobalVars class is a singleton class that provides the ability to set global variables during the execution of a script.
* @author lgonzalez
* @since Dec 10, 2015
*/
public static class GlobalVars {
private static GlobalVars instance;
public static GlobalVars getInstance() {
if (instance == null) {
instance = new GlobalVars();
}
return instance;
}
private String currentTestCaseID;
private String screenshotDir;
private WebDriver webdr;
private String LogDir;
private BufferedWriter bfWritter;
private FileWriter flWriter;
private LogFileHandler fileHandler;
private int logLevel;
private int passed;
private int failed;
// I create a variable containing the LogHandler class when the singleton class is invoked
private GlobalVars() {
fileHandler = new LogFileHandler();
}
//---------------------------------------------------
// G E T T E R S
//---------------------------------------------------
public BufferedWriter getbfWriter() {
return bfWritter;
}
public FileWriter getflWriter() {
return flWriter;
}
public String getLogDir() {
return LogDir;
}
public LogFileHandler getLogHandler() {
return fileHandler;
}
public int getLogLevel() {
return logLevel;
}
public String getScreenshotDir() {
return screenshotDir;
}
public String getTCID() {
return currentTestCaseID;
}
public WebDriver getWebdr() {
return webdr;
}
public int getPassed() {
return passed;
}
public int getFailed() {
return failed;
}
//---------------------------------------------------
// S E T T E R S
//---------------------------------------------------
public void setbfWriter(BufferedWriter bfwritter) {
this.bfWritter = bfwritter;
}
public void setflWriter(FileWriter flwriter) {
this.flWriter = flwriter;
}
public void setLogDir(String logDir) {
this.LogDir = logDir;
}
public void setLogLevel(int loglevel) {
this.logLevel = loglevel;
}
public void setScreenshotDir(String screenshotDir) {
this.screenshotDir = screenshotDir;
}
public void setTCID(String currentTestCaseID) {
this.currentTestCaseID = currentTestCaseID;
}
public void setWebdr(WebDriver webdr) {
this.webdr = webdr;
}
public void setPassed(int passed) {
this.passed = passed;
}
public void setFailed(int failed) {
this.failed = failed;
}
}
至於FileUtils。 此類是作為org.apache.commons.io.FileUtils的一部分導入的
我從未能夠解決該特定問題,但是我找到了一個效果更好的解決方案。 我使用了Java的Robot類,所以我替換了Selenium的方法:
File scrFile = ((TakesScreenshot) webdr).getScreenshotAs(OutputType.FILE);
使用以下機器人代碼:
try {
Robot robot = new Robot();
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
//Create the image
BufferedImage exportImage = robot.createScreenCapture(new Rectangle(0, 0, (int) scrSize.getWidth(), (int) scrSize.getHeight()));
//Get graphics - Get the layer we can actually draw on
Graphics2D imageGraphics = (Graphics2D) exportImage.getGraphics();
//Cleanup after ourselves
imageGraphics.dispose();
screenShotName = generateUniqueValue().retStringValue + ".png";
String[] pathSections = GlobalVars.getInstance().getLogDir().split("\\\\");
pathSections[pathSections.length-1] = "";
String path = "";
for (int x = 0; x < pathSections.length-1; x++) {
path = path + pathSections[x] + "\\";
}
效果更好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.