Capture screenshot in webview context

I am using Appium 1.2.3 and trying to capture the screenshot from the device using the following command:

driver.getScreenshotAs(OutputType.FILE)

It works fine in Native context, but fails when in webview context with the following message:

org.openqa.selenium.WebDriverException: unknown error: unhandled inspector error: {“code”:-32603,“message”:“Unable to capture screenshot”}
(Session info: webview=)
(Driver info: chromedriver=2.10.267521,platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 209 milliseconds
Build info: version: ‘2.42.2’, revision: ‘6a6995d31c7c56c340d6f45a76976d43506cd6cc’, time: ‘2014-06-03 10:52:47’
System info: host: ‘IT95MXR’, ip: ‘10.219.23.198’, os.name: ‘Windows 7’, os.arch: ‘amd64’, os.version: ‘6.1’, java.version: ‘1.8.0_20’
Driver info: io.appium.java_client.AppiumDriver
Capabilities [{app=C:\Users\Administrator\Desktop\Kotobi_3.2.0_Server_173_TESTING.apk, appPackage=com.vis.kotob, networkConnectionEnabled=true, warnings={}, databaseEnabled=false, deviceName=Android, platform=LINUX, appActivity=.ui.activity.splash.SplashActivity, desired={app=C:\Users\Administrator\Desktop\Kotobi_3.2.0_Server_173_TESTING.apk, appPackage=com.vis.kotob, appActivity=.ui.activity.splash.SplashActivity, newCommandTimeout=6000, browserName=, platformName=Android, deviceName=Android}, newCommandTimeout=6000, platformVersion=4.1, webStorageEnabled=false, locationContextEnabled=false, browserName=, takesScreenshot=true, javascriptEnabled=true, platformName=Android}]
Session ID: 25369dfe-eff7-4be2-98c3-57c650970420
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:95)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:100)
at org.openqa.selenium.remote.RemoteWebDriver.getScreenshotAs(RemoteWebDriver.java:324)
at AppiumTest.loginTest(AppiumTest.java:103)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:55)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)

I have also tried:

((TakesScreenshot) driver.context(“contextName”)).getScreenshotAs(OutputType.FILE)

and it fails with the same error. Does anyone have any idea why this is happening??

1 Like

Turned out to be an issue with ChrormeDriver :frowning: https://code.google.com/p/chromedriver/issues/detail?id=792

1 Like

For anyone suffering from this issue, here is a possible workaround:

Switch to native context, take the screenshot and then switch back to the context you were in. Here is a code in Java that does that:

String contextName = driver.getContext();
driver.context(NATIVE_APP);
File screenShot = driver.getScreenshotAs(OutputType.FILE);
driver.context(contextName );

Hope it helps somebody until they solve the bug :smile:

2 Likes

Below code is working fine for me:

public String Screenshotpath = "Mention the folder Location";
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(Screenshotpath+"Any name".jpg"));

The captured screenshot will be available in mentioned location.

1 Like

I’m also using such way - do perfect!

I tried such code and it didn’t work in my case :frowning:

Hey Hassan_Radi,

Even i am facing the same issue,Unable to capture the screenshot.Has the bug been fixed?At the moment i will try the workaround u suggested.

The ChromeDriver team has solved the bug, but I haven’t tried it myself yet. You can give it a try and let me know how it works for you, but so far I am using the workaround I mentioned before and it is working fine with me :smile:

Using the Workaround you suggested that works fine,Need to try the new chromedriver.

@Mayuresh_Shirodkar, @Hassan_Radi - Were you able to take the screenshot of the entire webpage or only the visible part of the screen on iOS or Android phone? Is there any way to take entire webpage screenshot? As using the above code I was only successful in capturing the visible content.
Any help would be appreciated.

@jigtest
It only takes a screenshot of the visible part of the webview. I didn’t come across such issue before as I only needed the visible part of the website. Anyway a possible workaroud would be to create your own function that scrolls and takes screenshots and then you can append those screenshots to create one big screenshot of the entire website.

Good luck!
Hassan

1 Like

@Hassan_Radi - Thanks for replying. I’ll try that.

Thanks you Hassam Radi. This helps me