简体   繁体   English

拍摄模态窗口的屏幕截图会产生黑色图像

[英]Taking a screenshot of a modal window produces a black image

I'm using IE11 and Java under Eclipse. 我在Eclipse下使用IE11和Java。 I'm not running the test on a remote system nor Am I using an RDC. 没有在远程系统上运行测试,也没有使用RDC。 This test is running on my local machine. 此测试正在我的本地计算机上运行。

When I try to take a snapshot of the following screen, Selenium seems to have a problem with it. 当我尝试拍摄以下屏幕快照时,Selenium似乎有问题。

Selenium无法对此模式进行截图

This is what it does when the following code is executed: 这是执行以下代码时的工作:

File scrFile = ((TakesScreenshot) webdr).getScreenshotAs(OutputType.FILE);
  1. It moves the modal window in steps up and to the left. 它将模式窗口向上和向左移动。 So if the original window position was (5,5) it moves it to (4,4) 因此,如果原始窗口位置为(5,5),则将其移至(4,4)
  2. It plays the "alarm" sound -- (ding!) 播放“警报”声音-(叮!)

It does this for about three times and then continues onto the next statement. 它执行大约三遍,然后继续执行下一条语句。

The result of screenshot process is a black image. 屏幕截图过程的结果是黑色图像。 在此处输入图片说明

Here is the actual method that I call when taking a screenshot: 这是我截屏时调用的实际方法:

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;

} }

The condition occurs right at line: 条件恰好发生在以下行:

File scrFile = ((TakesScreenshot) webdr).getScreenshotAs(OutputType.FILE);

GlobalVars is a singleton class that is mostly Setters and Getters. GlobalVars是一个单例类,主要是Setters和Getters。 Here is the code: 这是代码:

/**
 * 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;
    }
}

As for the FileUtils. 至于FileUtils。 This class was imported as part of the org.apache.commons.io.FileUtils 此类是作为org.apache.commons.io.FileUtils的一部分导入的

I was never able to solve that specific problem, but I found a solution that works even better. 我从未能够解决该特定问题,但是我找到了一个效果更好的解决方案。 I used Java's Robot class, so I replace Selenium's method: 我使用了Java的Robot类,所以我替换了Selenium的方法:

 File scrFile = ((TakesScreenshot) webdr).getScreenshotAs(OutputType.FILE);

with the following Robot code: 使用以下机器人代码:

        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] + "\\";
        }

That worked a lot better. 效果更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM