To take screenshot in Android and iOS

10,070 views
Skip to first unread message

Amit Rawat

unread,
Apr 26, 2013, 10:15:23 AM4/26/13
to appium-...@googlegroups.com
Can we take screenshots for iOS and Android app screens. I could see some comments in Git "Added support for screenshots for Android."   but I am not sure how to use it in my code.

I am using java for both Android and iOS automation. I would really appreciate if any one can help me on this.

bootstrap online

unread,
Apr 26, 2013, 10:19:43 AM4/26/13
to appium-...@googlegroups.com
Android screenshots are a work in progress.
https://github.com/appium/appium/pull/477

HungNguyen

unread,
Jun 5, 2013, 5:56:40 AM6/5/13
to appium-...@googlegroups.com
I also run into this problem.

Vào 21:19:43 UTC+7 Thứ sáu, ngày 26 tháng tư năm 2013, bootstrap online đã viết:

bootstrap online

unread,
Jun 5, 2013, 9:10:19 AM6/5/13
to HungNguyen, appium-...@googlegroups.com
Screenshots work on both iOS and Android.
> --
> http://appium.io
> ---
> You received this message because you are subscribed to the Google Groups
> "Appium-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to appium-discus...@googlegroups.com.
> Visit this group at http://groups.google.com/group/appium-discuss?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Brian

unread,
Jun 13, 2013, 6:32:31 PM6/13/13
to appium-...@googlegroups.com, HungNguyen
I am looking into using Appium to further my UI testing, are there demonstrations or tutorials that would show how to capture screens?
Or is the screencapture code part of the Appium codebase, and, if so would I use a Selenium screen capture function in my automation to take screens?

bootstrap online

unread,
Jun 13, 2013, 9:06:35 PM6/13/13
to Brian, appium-...@googlegroups.com, HungNguyen
The Selenium screencapture command should work. In Ruby it's
@driver.save_screenshot png_save_path
> Visit this group at http://groups.google.com/group/appium-discuss.

Swapna chennadi

unread,
Jun 14, 2013, 6:53:40 AM6/14/13
to appium-...@googlegroups.com, Brian, HungNguyen
I have tried taking the screenshot of android hybrid app with selendroid and ios native/hybrid apps which works well .
But, when I try to take screenshot of android native app by setting device as 'android' , the screenshot request doesn't even reach appium.
My test fails with the following message.

java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebDriver cannot be cast to org.openqa.selenium.TakesScreenshot


Please find below the java code which I used.

##################Java code#################################

DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability("device", "android");


 // Take the Screen shot and store it in a file

        org.openqa.selenium.WebDriver augmentedDriver = new Augmenter().augment(webDriver);

        File screenshot = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE);

        // the name of the file

        String filename = "screenshot.png";

        FileUtils.copyFile(screenshot, new File(filename));

############################################################################


Swapna


Jonathan Lipps

unread,
Jun 14, 2013, 1:45:13 PM6/14/13
to Swapna chennadi, appium-...@googlegroups.com, Brian, HungNguyen
That sounds like a Java-specific issue. Screenshots also work in the javascript bindings (see Appium's test suite for example).

Jason Carr

unread,
Jun 14, 2013, 2:47:49 PM6/14/13
to Jonathan Lipps, Swapna chennadi, appium-...@googlegroups.com, Brian, HungNguyen
Yeah the bindings don't all work *exactly* the same way. It's possible that we're not 100% compatible with the java bindings, or that the java bindings are broken, or that the code is wrong. Looking at the code, I think you're doing it the right way. Maybe someone else has some insight into how to do this with Java?

Brian

unread,
Jun 14, 2013, 3:28:37 PM6/14/13
to appium-...@googlegroups.com
Thanks for the help.

thes...@gmail.com

unread,
Jun 14, 2013, 6:05:11 PM6/14/13
to appium-...@googlegroups.com

Ritika Mishra

unread,
Jun 17, 2013, 3:11:27 AM6/17/13
to appium-...@googlegroups.com
Hi,

I have tried taking screen shots of android native app and it works fine, though i struggled a lot to get it work. I have done it in both java and C#. Here's the code in c#

 try
                    {
                        String base64 = rm.executescript(DriverCommand.Screenshot, null).Value.ToString();
                        byte[] data = Convert.FromBase64String(base64);
                        using (var stream = new MemoryStream(data, 0, data.Length))
                        {
                            Image image = Image.FromStream(stream);
                            image.Save(filePath + "\\" + imageName);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.Write(e.Message);
                    }


here "rm" is the object for class that extends RemoteWebDriver and overrides the method "executescript".

Hope it works for you :)

--Ritika

Swapna chennadi

unread,
Jun 17, 2013, 5:23:40 AM6/17/13
to appium-...@googlegroups.com
Hi Ritika,

Thanks a lot for your response.
I have tried a few based on your c# code and trying to work out similar in java.But still end up getting the same ClassCatException.
Would greatly appreciate if you could post your java code.

Swapna

Dan Cuellar

unread,
Jun 17, 2013, 3:54:04 PM6/17/13
to appium-...@googlegroups.com
Sounds like this might be an appium bug, can someone 100% confirm screenshot does not work with java?

bootstrap online

unread,
Jun 17, 2013, 4:00:27 PM6/17/13
to Dan Cuellar, appium-...@googlegroups.com
ClassCastException is a user error and not an Appium bug.

W

unread,
Jun 19, 2013, 11:37:24 AM6/19/13
to appium-...@googlegroups.com
Hi Ritika,

Glad to hear that someone has got capturing screenshots working in c#. I am using the ScreenShotRemoteWebDriver.cs implemented by ?? a while back that casts ITakeScreenShot to Remotedriver but I can't get screenshot capture to work. It works on mobile browsers but for some reason it doesn't work on Native Apps. I tried to incorporate your code but I still could get it to work, could you point out where I am doing it wrong;

        public Screenshot GetScreenshot()
        {
            // Get the screenshot as base64.
            Response screenshotResponse = this.Execute(DriverCommand.Screenshot, null);
            string base64 = screenshotResponse.Value.ToString();

            // ... and convert it.
            return new Screenshot(base64);
        }
    }
}

Where I am using the class

Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();
            string ss = screenshot.AsBase64EncodedString;
            byte[] screenshotAsByteArray = screenshot.AsByteArray;
             screenshot.SaveAsFile("//location+imagename", System.Drawing.Imaging.ImageFormat.Jpeg);

As i said this works on mobile browsers

Guilherme Tramontina

unread,
Jun 25, 2013, 2:41:38 PM6/25/13
to appium-...@googlegroups.com, Jonathan Lipps, Swapna chennadi, Brian, HungNguyen
I've just posted my findings on github (https://github.com/appium/appium/issues/708#issuecomment-19996799). Pasting my comment here:

Using the Java web driver, I was getting a ClassCastException as someone reported on google groups (https://groups.google.com/forum/#!searchin/appium-discuss/screenshot/appium-discuss/A6kAET0C7nc/_I7rszrh8YkJ) when augmenting the driver to be able to take screenshots. I dug a little bit and found that my remote web driver was not being augmented with the TakesScreenshot interface, and then I noticed that the capability for Android (app/android.js) is set to takesScreenshots: true, while the capability constant is takesScreenshot (no 's' at the end).
So I went ahead and changed it locally to see if I could make it work. I ended up with a different error. Now it augments my driver, but I get the following error: org.openqa.selenium.remote.SessionNotFoundException: Session ID is null
Has anyone ever got screenshots working with webdriver for Java and Android?

Ajith V L

unread,
Jun 25, 2013, 4:14:14 PM6/25/13
to appium-...@googlegroups.com, Jonathan Lipps, Swapna chennadi, Brian, HungNguyen
I am also having the same issue as Guilherme. Screenshot works on iOS but it does not work on Android for native apps. I am getting the exception "

bootstrap online

unread,
Jun 25, 2013, 4:15:05 PM6/25/13
to Ajith V L, appium-...@googlegroups.com, Jonathan Lipps, Swapna chennadi, Brian, HungNguyen
Screenshots are working for me in Ruby so I think it's an issue with
the Java bindings.

Dan Cuellar

unread,
Jun 25, 2013, 4:16:38 PM6/25/13
to appium-...@googlegroups.com, Ajith V L, Jonathan Lipps, Swapna chennadi, Brian, HungNguyen
Yeah, in .NET you have to roll your own Screenshot methods because for some reason they are not in the RemoteWebDriver class.

Dan Cuellar

unread,
Jun 25, 2013, 4:17:27 PM6/25/13
to appium-...@googlegroups.com, Ajith V L, Jonathan Lipps, Swapna chennadi, Brian, HungNguyen
I submitted a patch to the Selenium project about a year ago and no one has merged

Adriano Bonat

unread,
Jun 29, 2013, 3:44:06 PM6/29/13
to appium-...@googlegroups.com, Ajith V L, Jonathan Lipps, Swapna chennadi, Brian, HungNguyen
I've sent a pull request to fix this issue:

The correct capability name is "takesScreenshot", in the singular.

W

unread,
Jul 3, 2013, 10:15:49 AM7/3/13
to appium-...@googlegroups.com
Could you point in in the right direction, I am using the following but the function is not detecting a native app it works for mobile browsers. This is webdriver c#

  public Screenshot GetScreenshot()
        {
            // Get the screenshot as base64.
            Response screenshotResponse = this.Execute(DriverCommand.Screenshot, null);
            string base64 = screenshotResponse.Value.ToString();

            // ... and convert it.
            return new Screenshot(base64);
        }
    }
}

Where I am using the class

Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();
            string ss = screenshot.AsBase64EncodedString;
            byte[] screenshotAsByteArray = screenshot.AsByteArray;
             screenshot.SaveAsFile("//location+imagename", System.Drawing.Imaging.ImageFormat.Jpeg);


On Monday, 17 June 2013 08:11:27 UTC+1, Ritika Mishra wrote:

Adriano Bonat

unread,
Jul 3, 2013, 10:35:51 AM7/3/13
to W, appium-...@googlegroups.com
My pull request got merged but there isn't a new release of appium with it, you can try out with appium from their git repo or wait the next release (current version in npm is 0.7.3).




--
http://appium.io
---
You received this message because you are subscribed to a topic in the Google Groups "Appium-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/appium-discuss/A6kAET0C7nc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to appium-discus...@googlegroups.com.

Daniel Puterman

unread,
Jul 14, 2013, 12:03:49 PM7/14/13
to appium-...@googlegroups.com
Hi, I'm also having problems with taking a screenshot using Appium on Android (I'm using binaries downloaded from appium.io on Win8 machine). 
Calling the "screenshot" wire protocol command using a browser to the Appium app, returns status "0" and an empty value. As you can probably guess, calling the getScreenshot command through some language bindings (I've tried Java and Python) returns the same result. Other actions such as finding elements and clicking seems to work well. 

Here is the on screen logs from the Appium app from a call to "screenshot":

GET /wd/hub/session/a3c713ca-ce7f-4eb0-8b88-cbbf707abb7f 200 5ms - 288
debug: Appium request initiated at /wd/hub/session/a3c713ca-ce7f-4eb0-8b88-cbbf707abb7f/screenshot
info: Responding to client with success: {"status":0,"value":"","sessionId":"a3c713ca-ce7f-4eb0-8b88-cbbf707abb7f"}

So, the call returns 200 OK, but no actual data. Needless to say, the Android app being automated is shown on screen as the foreground application. 

Any help would be greately appreciated :)

Dan Cuellar

unread,
Jul 14, 2013, 2:22:59 PM7/14/13
to appium-...@googlegroups.com
Due to a different bug I wasn't able to publish Appium 0.8.0 for Windows.

If you download the source from appium.io this will work. It will also work next time we cut binaries for Windows 8 which should be in a week or two I would guess.

Dan Cuellar

unread,
Jul 14, 2013, 2:23:45 PM7/14/13
to appium-...@googlegroups.com

Daniel Puterman

unread,
Jul 14, 2013, 5:47:16 PM7/14/13
to appium-...@googlegroups.com
I'll check it out. Thanks for the quick reply!

On Sunday, July 14, 2013 9:23:45 PM UTC+3, Dan Cuellar wrote:
I mean github.com/appium/appium

Daniel Puterman

unread,
Jul 15, 2013, 6:42:25 PM7/15/13
to appium-...@googlegroups.com
Works like a charm! (well, almost like a charm, there are some other issues, but less relevant to this thread). 

Thanks! 

defonus

unread,
Oct 16, 2013, 3:30:57 AM10/16/13
to appium-...@googlegroups.com
Hello. How to capture screens for native app? Can you provide java code?

Best Regards,
defonus

вторник, 16 июля 2013 г., 1:42:25 UTC+3 пользователь Daniel Puterman написал:

Saikat Maitra

unread,
Oct 20, 2013, 7:01:27 AM10/20/13
to appium-...@googlegroups.com
Hello,

Please try the below code sample for capturing screenshot in java.


        try {
        
            File classpathRoot = new File(System.getProperty("user.dir"));
            File appDir = new File(classpathRoot, "../calci-android-app/out/production/calci-android-app");
            File app = new File(appDir, "calci-android-app.apk");
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability("device","Android");
            capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
            capabilities.setCapability(CapabilityType.VERSION, "4.2");
            capabilities.setCapability(CapabilityType.PLATFORM, "MAC");
            capabilities.setCapability("app", app.getAbsolutePath());
            capabilities.setCapability("app-package", "com.example.calci");
            capabilities.setCapability("app-activity", ".MyActivity");
            capabilities.setJavascriptEnabled(true);
            driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

            WebDriver augmentedDriver = new Augmenter().augment(driver);
            File f  = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(f, new File("screenshot.jpg"));

        } catch (Exception e) {
            e.printStackTrace();
        }


Regards
Saikat

defonus

unread,
Oct 21, 2013, 2:35:11 AM10/21/13
to appium-...@googlegroups.com
Thx.



воскресенье, 20 октября 2013 г., 14:01:27 UTC+3 пользователь Saikat Maitra написал:

Patrick L

unread,
Dec 11, 2013, 11:04:03 AM12/11/13
to appium-...@googlegroups.com
Is this working for anyone?
I tried the code for java on the iOS simulator but always get the same exception when attempting to augment the driver:
     java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given

I'm using Junit and have a rule setup to always take a screenshot when an exception is thrown like so:

    @Rule
    public ScreenshotTestRule screenshotTestRule = new ScreenshotTestRule();

    private class ScreenshotTestRule implements MethodRule {
        public Statement apply(final Statement statement, final FrameworkMethod frameworkMethod, final Object o) {
            return new Statement() {
                @Override
                public void evaluate() throws Throwable {
                    try {
                        statement.evaluate();
                    } catch (Throwable t) {
                        captureScreenshot(frameworkMethod.getName());
                        throw t; // rethrow to allow the failure to be reported to JUnit
                    }
                }

                public void captureScreenshot(String testName) {
                    new File("target/surefire-reports/screenshot/").mkdirs(); // Insure directory is there
                    String filename = "target/surefire-reports/screenshot/" + testName + ".jpg";

                    try {
                        WebDriver augmentedDriver = new Augmenter().augment(driver);
                        File scrFile = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);
                        FileUtils.copyFile(scrFile, new File(filename), true);
                    } catch (Exception e) {
                        System.out.println("Error capturing screen shot of " + testName + " test failure.");
                    }
                }
            };
        }
    }


I did notice that the driver quits and closes the simulator before the rule for the takeScreenshot can take place but thats probably a different problem. 

The test code in the appium project has the same error it looks like
See method testScreenshot()

Igor Gladun

unread,
Dec 13, 2013, 10:33:26 AM12/13/13
to appium-...@googlegroups.com
Hello.
I had problems with 
Superclass has no null constructors but no arguments were given
Solved next way, adding static and default constructor to Swipeable
(driver is static)

    public static class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen {
        private RemoteTouchScreen touch;


        public SwipeableWebDriver() {
        }

        public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
            super(remoteAddress, desiredCapabilities);
            touch = new RemoteTouchScreen(getExecuteMethod());
        }

        public TouchScreen getTouch() {
            return touch;
        }
    }

public class TakeScreenshotListener extends TestListenerAdapter {

    @Override
    public void onTestFailure(ITestResult tr) {
        try {
            takeScreenshot(tr.getName());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


  public static void takeScreenshot(String testName) throws IOException {
        System.out.println("Taking screenshot");


        new File("target/surefire-reports/screenshot/").mkdirs(); 
        String filename = "target/surefire-reports/screenshot/" + testName + ".jpg";

        try {
            WebDriver augmentedDriver = new Augmenter().augment(driver);
            File scrFile = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(scrFile, new File(filename), true);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error capturing screen shot of " + testName + " test failure.");
        }

        System.out.println("Took Screenshot"+testName);

    }




Patrick L

unread,
Dec 16, 2013, 11:53:05 AM12/16/13
to appium-...@googlegroups.com
Thanks Igor that fixed my problem!

Naru Segawa

unread,
Jan 30, 2014, 3:34:48 AM1/30/14
to appium-...@googlegroups.com
Just added
    public SwipeableWebDriver() {
    }

as Igor but without static

пятница, 26 апреля 2013 г., 18:15:23 UTC+4 пользователь Amit Rawat написал:

sandy

unread,
Mar 25, 2014, 5:09:52 AM3/25/14
to appium-...@googlegroups.com
Hi Sakit,

Can u pls provide the code for taking screenshot of iOS in c# using appium.
Thanks  in advance.

gobi

unread,
May 15, 2014, 6:16:24 AM5/15/14
to appium-...@googlegroups.com
Hi, i am facing trouble to take iOS device screenshot. Hereby i have given code snippet used

WebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:27745/wd/hub"), capabilities);
            driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
            driver.get("http://www.google.com/ncr");
            driver.getTitle();
            System.out.println("Page load completed");
            WebElement element = driver.findElement(By.name("q"));
            element.sendKeys("BrowserStack");
            element.submit();
//            Get the screenshot

            WebDriver adriver =  new Augmenter().augment(driver);
            File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
           
            try {
            File testScreenShot = new File("/Users/labuser/Downloads/test.jpg");
            //Copy the file to screenshot folder
            FileUtils.copyFile(scrFile, testScreenShot);

            } catch (IOException e) {
            e.printStackTrace();
            }
           
This works for me only on Simulator for real device it is not working. Do I need to setup any additional device settings for screen capture?
It keeps waiting at line
           File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
  Receiving message in console, it shows for simulator also for sometimes.
    May 15, 2014 11:50:41 AM org.apache.http.impl.client.DefaultRequestDirector tryExecute
INFO: I/O exception (org.apache.http.NoHttpResponseException) caught when processing request: The target server failed to respond
May 15, 2014 11:50:41 AM org.apache.http.impl.client.DefaultRequestDirector tryExecute
INFO: Retrying request

On Sunday, October 20, 2013 4:31:27 PM UTC+5:30, Saikat Maitra wrote:

Jonathan Lipps

unread,
May 15, 2014, 6:31:18 AM5/15/14
to gobi, appium-...@googlegroups.com
Screenshot on iOS real devices only works when automating native apps--when automating Safari it won't work unfortunately.

--
http://appium.io
---
You received this message because you are subscribed to the Google Groups "Appium-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to appium-discus...@googlegroups.com.
Visit this group at http://groups.google.com/group/appium-discuss.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

gobi

unread,
May 15, 2014, 7:21:38 AM5/15/14
to appium-...@googlegroups.com, gobi
Thanks for the update.

Is it possible to do the same with Hybrid Application?

gobi

unread,
May 15, 2014, 7:57:00 AM5/15/14
to appium-...@googlegroups.com, gobi
Do we have any other way to capture safari loading page?


On Thursday, May 15, 2014 4:01:18 PM UTC+5:30, Jonathan Lipps wrote:

gobi

unread,
May 29, 2014, 4:49:38 AM5/29/14
to appium-...@googlegroups.com, gobi
I have achieved the same with hybrid application! driver.context(c1urrent webview) allow me to switch webview mode and able to load url using driver.get("url")
. Now it is possible to take screenshot ! thanks for your assistance!

Paras Jain

unread,
Aug 25, 2014, 3:35:00 AM8/25/14
to appium-...@googlegroups.com, mgobi...@gmail.com
Hello Gobi,

I am stack in same issue(unable to take screenshot) which are you solved it.

please tell me, how to capture screenshot of Safari Browser on iOS Physical Device using Appium?

Thanks & Regards,
Paras Jain
Reply all
Reply to author
Forward
0 new messages