0

I'm using IE11 and Java under Eclipse. I'm not running the test on a remote system nor Am I using an 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 cannot take a screenshot of this modal

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)
  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. enter image description here

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. 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. This class was imported as part of the org.apache.commons.io.FileUtils

DeZaelez
  • 31
  • 10
  • Have you tried taking a screenshot in another browser, e.g. Chrome? This may be particular IE issue https://stackoverflow.com/questions/36889688/black-screen-when-taking-screenshot-with-internet-explorer-driver-on-windows-bui?rq=1 – Ivan Pronin May 25 '17 at 20:20
  • Is it possible that the modal window does not have the webdriver's control and you need to switch to the window before the screenshot? Is this an actual pop-up (alert?) or a new window? – Bill Hileman May 25 '17 at 20:33
  • MSIE screenshots can be like that. I suggest just using awt to take a screenshot of the entire screen https://stackoverflow.com/questions/58305/is-there-a-way-to-take-a-screenshot-using-java-and-save-it-to-some-sort-of-image – Mikhail Ramendik May 25 '17 at 23:11
  • @IvanPronin - The problem only exists with the modal window. I'm able to take screenshots of other windows. – DeZaelez May 26 '17 at 02:58
  • @BillHileman - That's what I thought too, so I iterated through the window handles and tried issuing the screenshot command there, but I got the same results – DeZaelez May 26 '17 at 02:59
  • @MikhailRamendik - Good suggestion, I'll give that a try. – DeZaelez May 26 '17 at 02:59
  • @DeZaelez Can you consider showing us your work please? Thanks – undetected Selenium May 26 '17 at 08:24
  • @DebanjanB, I updated the question and posted the method I'm using to take the screenshot. – DeZaelez Jun 02 '17 at 00:21
  • @DeZaelez Can you consider showing us the implementation of `GlobalVars` and `FileUtils` class please? Thanks – undetected Selenium Jun 02 '17 at 06:29
  • @DebanjanB - I uploaded the GlobalVars code. – DeZaelez Jun 02 '17 at 23:23
  • I have the similar issue when I am using the "Chrome" WebDriver and is try to access one of apple web page. – user3595231 Mar 22 '18 at 03:45

1 Answers1

0

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:

 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.

DeZaelez
  • 31
  • 10