hsac-fitnesse-fixtures icon indicating copy to clipboard operation
hsac-fitnesse-fixtures copied to clipboard

Taking screen shots of the entire desktop

Open violinner opened this issue 4 years ago • 7 comments

Tests of web-application functions that download files requires interaction with desktop, as Selenium cannot touch a file-save pop-up dialog.

Interaction with a file-save pop-up dialog can be done with a java.awt (Robot) method, but requires knowledge of where the dialog control buttons are. Desktop screenshots help to determine this, especially in an automation server with a headless display driver.

Suggested code:

|show | take desktop screenshot | wheresmypopup |

public String takeDesktopScreenshot(String file) {
    String rtn = null;
    char slash = File.separatorChar;
    Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    try {
        Robot rb = createRobot();
        BufferedImage capture = rb.createScreenCapture(screenRect);
        String linkToForward = ("files" + slash + "screenshots" + slash + file + ".png").replace("\\", "/");
        String dstFile = "FitNesseRoot" + slash + linkToForward;
        File toWrite = new File(dstFile); // todo: move to FitNesseRoot
        ImageIO.write(capture, "png", toWrite);
        rtn = linkToForward;
    } catch (IOException | ReflectiveOperationException e) {
        logger.error("desktopCapture(" + file + ") failed.", e);
    }
    return rtn;
}

violinner avatar Feb 05 '20 14:02 violinner

Just wondering; Is the dialog appearing in specific browsers? I have never encountered a save file dialog, although I have lots of tests in different projects that do file downloads.

Could this be client or browser behaviour that can be influenced using Slenium's capabilities?

tcnh avatar Feb 05 '20 15:02 tcnh

@violinner indeed you get a file-save-popup if you have selenium click on a file download button or link and you can't manipulate such a popup with selenium. That's why BrowserTest offers the 'download...' methods, these allow you to directly download files based on the URLs present on a page without going through the file save popup. (Actually HttpTest is used to perform the downloads, with access to all cookies present on the browser).

Another approach is to configure the browser to skip the file save popup and just immediately download a file to a preconfigured location (e.g. 'Downloads' folder). This can be done (I believe) for both Firefox and Chrome.

I don't have any experience using the 2nd approach, but I created the 'download...' methods specifically for file downloads and they also work when using remote Selenium (e.g. browser in cloud like SauceLabs) where FitNesse does not have access to the file system of the machine running the browser. I believe the latter is not possible using the 2nd, direct save, approach or the approach you suggest where you want to interact with the OS specific window directly.

If you do want to take a screenshot and use it to find coordinates: I believe you don't need any specific method to get it. Just tell BrowserTest to maximise the browser window and use the 'take screenshot' method already present (it will create a .png).

fhoeben avatar Feb 05 '20 20:02 fhoeben

I have been able to set the download file location for Firefox, in fact, to the Wiki's files/downloads directory.

However, on Unix using xvfb headless display, there appears to be an unavoidable hiccup, that the "desktop" pops up a dialog that asks if you want to save a file to the given location, even if there is no existing file with the same name.

For that reason, I had to create a Fixture with java.awt functionality, fix the position of the browser, and take screen shots of the entire desktop. Selenium shows no trace of the pop-up dialog, but Red Hat EL7.5 "import" (ImageMagick) command and java.awt.Robot.createScreenCapture() both see it and will take screen shots of it.

I hope this is not too much of a niche problem? Maybe it should be in a stand-alone "RobotFixture" and not BrowserTest?

violinner avatar Feb 05 '20 21:02 violinner

I believe a standalone fixture to deal with OS windows seems like an excellent idea.

But can you elaborate on why you cannot just use the 'download ...' methods in BrowserTest to do your downloads? If we can improve those to cover more cases that would seem desirable...

fhoeben avatar Feb 06 '20 07:02 fhoeben

@violinner what are your experiences with BrowserTest's 'download...' methods? Can you elaborate why they do not allow you to download the files you need?

fhoeben avatar Feb 13 '20 19:02 fhoeben

I will try "download..." methods again. I had some problems when the "download" button starts some background JavaScript in a single-page (SPA) application, and did not directly download a file.

Will post on this thread when experiments are complete.

violinner avatar Feb 13 '20 21:02 violinner

Downloads generated by JavaScript and returned as a result of POSTing a form usually prevent the 'download ...' methods from working. They have to have a url that they can GET that's a limitation I'm aware of. But I found most applications (even SPAs) prefer to deliver downloads at normal URLs so most often for me it just worked.

fhoeben avatar Feb 13 '20 21:02 fhoeben