Take Screenshot in Appium with this Simple Code
Screenshots serves an important role of acting as an evidence of any tests performed. Therefore screenshot capture functionality is very useful in any automated testing activities. WebDriver provides TakeScreenshot interface to capture screenshot of a web page. We can leverage that same method to take screenshot in Appium tool as well. In this post, we will explore how we can capture screenshot of an Android app using Appium tool.
For our example, we will capture screenshot of a native Android app running on Android emulator. We hope you have already setup Appium to use with an Android app. The code that we will see in steps below is written in Java. OK, enough with the talks. Let’s delve deep into the actual implementation now.
- We will be using Augmenter class of WebDriver for screenshot capture functionality (Therefore, please make sure you have imported org.openqa.selenium.remote.Augmenter class in your test). To take screenshot in Appium, you just have to use below code in your tests.
123WebDriver driver1 = new Augmenter().augment(driver);File file = ((TakesScreenshot)driver1).getScreenshotAs(OutputType.FILE);FileUtils.copyFile(file, new File("Screenshot.jpg")); - As you can derive, the code uses the same method as WebDriver to take screenshot in Appium but with an additional help of Augmenter class.
- On running this code, you will find a Screenshot.jpg file in root folder of your WebDriver project. You can also specify directory of your choice as copyFile method’s parameter.
The example code we have shown above is tested on Android app. But as Appium has a flexibility to use same code for both iOS and Android apps, you should be able to use same code on iOS app as well.
We hope the above tip to capture screenshot in Appium tool is useful to you. Let us know if you run across any issue while getting this to work. We would love to have your feedback in comments.
Hi,
Can you I use the same code to take the screenshot of any webpage (in a browser within my device ( iOS and Android))?
Please let me know.
Thanks,
Prawin
yes you can as far as the capabilities are properly set; I have taken screenshot of iPad mini and nexus 7 real devices
Hi Niko,
I tried a lot to perform the appium test on real device but failed all the time, the same test cases are working fine on an emulator. Please can you help me with the capabilities or with any sample code for the same. I’m trying ruby,appium to do web browser automation in devices.
This code gives screenshot of visible part of the screen, how do you take screenshot of full webpage in Appium?
In my case it gives an error : (Original error: Command failed: cannot create ‘C:\Users\Q2882\AppData\Local\Temp\appium116515-2280-1ovnqm6.png’: No such file or directory ) (WARNING: The server did not provide any stacktrace information)
is there any solution for this problem??
IDE: eclipse