How to save capture images in to desired location in webdriver?

2,587 views
Skip to first unread message

Kishore K

unread,
Nov 26, 2012, 2:21:48 AM11/26/12
to webd...@googlegroups.com
I know how to capture images in webdriver .How to save those in desired location.I'm only able to save those in my workspace only.

Mark Collin

unread,
Nov 26, 2012, 5:29:10 PM11/26/12
to webd...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/J4SpDafZ0wgJ.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

Kishore K

unread,
Nov 27, 2012, 12:19:43 AM11/27/12
to webd...@googlegroups.com
Hi Mikkel Pedersen Kaas,

That really good but i want to know it in Java...Sorry i didn't mentioned in the post .

Kishore



On Monday, November 26, 2012 5:14:17 PM UTC+5:30, Mikkel Pedersen Kaas wrote:
Hi Kishore, 

The following code is c/p'ed out of a solution that works for me (c#):

Directory.CreateDirectory(path);
Screenshot ss = ((ITakesScreenshot)webdriver).GetScreenshot();
string screenshot = ss.AsBase64EncodedString;
byte[] ssBytes = ss.AsByteArray;
ss.SaveAsFile((string.Format("{0}\\{1}{2}", path, fileName, ".jpeg")), ImageFormat.Jpeg);

Where path is simply the path to the desired folder, e.g. 

private readonly string path = @"C:\MyWebdriverScreenshots"; 

Mike Riley

unread,
Nov 28, 2012, 2:15:16 PM11/28/12
to webd...@googlegroups.com
You can save the files anywhere you want.  Java by default will save them in the directory your code runs in, if no path is given.

I wrote mine so it used a relative path to place it where the TestNG reports would be put, but you can also use an absolute path if you wish.

Since I ran in a Grid setup, I had a common network mount that was the same on all systems so that absolute paths could be used if needed.

Here is my code to save screen shots.  You just need to change the directory to suit your needs:

    /**
     * This is used when trying to capture a screenshot at the time of failure.
     * This is based on a recommendation from the Selenium user group: <a
     * href="http://groups.google.com/group/selenium-users/browse_thread/thread/55066381d492c716#">
     * Automatic Screenshot on error</a>
     * @return WebDriver generated by Augmenter class.
     * @see <a
     * href="http://seleniumhq.org/docs/04_webdriver_advanced.html#taking-a-screenshot">
     * taking-a-screenshot</a>
     */
    private WebDriver getScreenshot()
    {
        return new Augmenter().augment(driver);
    }   // getScreenshot

    /**
     * This method is simply used to output a blank line to both the console and
     * the reporter log. The funny thing about Reporter.log() is that if you use
     * the method that goes to both then it will not handle XHTML tags and seems
     * to just send them out as-is to the console. So instead of doing two calls
     * every time I need a blank line (one for console and one for the log), I
     * just wrote this method to do it.
     */
    private void logBlankLine()
    {
        System.out.println(""); // Output a blank line on the console
        Reporter.log("<br />"); // Output a blank line in the log
    }   // logBlankLine

    /**
     * This is used to log any exception that happens during a test. It must be
     * called from the test, but it will not flag the test as having failed, as
     * the exception might be what was expected. The stack trace, exception
     * message string, and explanation are all written to the console as well as
     * the Reporter log (which is part of the TestNG Results page). In addition
     * a screenshot is captured to a file and an img tag is written out to the
     * Reporter log so it is visible on the web page for Reporter output and
     * Results web pages. A blank line will be output both before and after the
     * exception info, to help it standout from other output.
     * @param explanation This should be some string that explains what was
     *                    going on when the exception occurred. Something that
     *                    will make sense to the person reading the log. Can be
     *                    null if no explanation is provided.
     * @param ex          This is anything that is a subclass of Throwable, like
     *                    Exception. Can be null for cases where there is no
     *                    Throwable event to report.
     */
    @SuppressWarnings("UseSpecificCatch")
    public void logException(String explanation, Throwable ex)
    {
        String filename = reportDir + "/ScreenSnapshot" +
                          String.valueOf(shotCount++) + ".PNG";

        logBlankLine();
        if (explanation != null)
            Reporter.log(explanation, true);

        if (ex != null)
            Reporter.log(throwableToString(ex), true);

        if (driver == null)
        {   // We may not have been able to start the session
            if (selenium != null)
            {   // Use this if we are using Remote Control instead of WebDriver
                String str = System.getProperty("user.dir") + "/"; // Current dir

                selenium.captureScreenshot(filename);
                Reporter.log("<img src=\"file:///" + str + filename +
                             "\" alt=\"\"/><br />");
            }
        }   // if
        else
        {   // Not all Webdriver instances can take a screenshot
            WebDriver augmentedDriver = getScreenshot();
            try
            {
                File file;
                String str = System.getProperty("user.dir") + "/"; // Current dir

                file = ((TakesScreenshot) augmentedDriver).getScreenshotAs(
                        OutputType.FILE);
                FileUtils.copyFile(file, new File(filename));
                Reporter.log("<img src=\"file:///" + str + filename +
                             "\" alt=\"\"/><br />");
            }
            catch (Throwable ex1)
            {
                Reporter.log("Unable to capture screentshot to " + filename +
                             "<br/>Got error: " + ex1.getMessage());
            }   // try-catch
        }   // else

        logBlankLine();
    }   // logException


Mike

Kishore K

unread,
Nov 28, 2012, 11:53:42 PM11/28/12
to webd...@googlegroups.com
Thank you Mike...

Mike Riley

unread,
Nov 29, 2012, 12:27:50 PM11/29/12
to webd...@googlegroups.com
I was confused the first time I tried it, still being a bit new to Java, until I found out that with Java you can only count on your current working directory.

In my Netbeans IDE I have it change the CWD to be where they will run in production, so they go to the right directory, but I pass a command line argument that overrides my default directory so I don't stomp on the results of the nightly run by the production tests that run nightly.

That way I don't write the TestNG files with screen captures to my project directory.

Mike

Kishore K

unread,
Nov 30, 2012, 12:08:42 AM11/30/12
to webd...@googlegroups.com
That's how we learn...Once we start doing it ,we will get the right idea at right place....

I'm not using TestNG files ,i'm using this to capture screen shot option in my frame work.so that,we can capture all screens, which we wish to.

It won't be problem ,even it is saved in workspace..still i want to change to better useful.Now i can do that so...

Thank all of you for your inputs...

Kishore
Reply all
Reply to author
Forward
0 new messages