Java 拍摄模式窗口的屏幕截图会生成一个黑色图像
我在Eclipse下使用IE11和Java。我不是在远程系统上运行测试,也不是在使用RDC。此测试正在我的本地计算机上运行 当我尝试拍摄以下屏幕的快照时,Selenium似乎有问题 执行以下代码时,它就是这样做的:Java 拍摄模式窗口的屏幕截图会生成一个黑色图像,java,selenium,testing,automated-tests,screenshot,Java,Selenium,Testing,Automated Tests,Screenshot,我在Eclipse下使用IE11和Java。我不是在远程系统上运行测试,也不是在使用RDC。此测试正在我的本地计算机上运行 当我尝试拍摄以下屏幕的快照时,Selenium似乎有问题 执行以下代码时,它就是这样做的: File scrFile = ((TakesScreenshot) webdr).getScreenshotAs(OutputType.FILE); 它将逐步向上和向左移动模态窗口。因此,如果原始窗口位置为(5,5),它会将其移动到(4,4) 它播放“警报”声——(叮!) 它这样做
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;
GlobalVars是一个单例类,主要是setter和getter。代码如下:
/**
* 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] + "\\";
}
试试看{
机器人=新机器人();
维度scrSize=Toolkit.getDefaultToolkit().getScreenSize();
//创建图像
BuffereImage exportImage=robot.createScreenCapture(新矩形(0,0,(int)scrSize.getWidth(),(int)scrSize.getHeight());
//获取图形-获取我们可以实际绘制的图层
Graphics2D imageGraphics=(Graphics2D)exportImage.getGraphics();
//我们自己打扫
dispose();
screenShotName=generateUniqueValue().retStringValue+“.png”;
String[]pathSections=GlobalVars.getInstance().getLogDir().split(“\\\”);
pathSections[pathSections.length-1]=“”;
字符串路径=”;
对于(int x=0;x
这样做效果更好。您是否尝试过在其他浏览器(如Chrome)中拍摄屏幕截图?这可能是一个特殊的IE问题是否可能是模式窗口没有webdriver的控制,您需要在截图之前切换到该窗口?这是一个实际的弹出窗口(警报?)或者一个新窗口?MSIE截图可以是这样的。我建议只使用awt截图整个屏幕@IvanPronin-问题只存在于模式窗口。我可以截图其他窗口。@BillHileman-我也是这么想的,所以我反复浏览了窗口句柄,并尝试发出截图命令但我得到了同样的结果
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] + "\\";
}