Appium - Capture Screenshot Android App Mobile Screen


Capturing screenshot in appium android automation Is main requirement for any software automation tool. During software automation testing process, It should allow you to  capture screenshot automatically to show bug or some design Issue to development/design team. For android software application automation testing using appium, We can use TakesScreenshot Interface of WebDriver to  capture screenshot of android app screen. I have prepared simple example on how to capture screenshot In android appium automation software test using TakesScreenshot Interface of WebDriver.

App To Use Capture Screenshot Test
We will use API Demos android software app for Capture Screenshot test. You can download it from  HERE.

Aim to Achieve In Capture Screenshot Test
We wants to capture screenshot of android mobile screen on some stage of appium automation test. And then we will store It Inside screenshots folder under project. We will prepare file name using current date time programmatically as shown In bellow Image.


Above Image shows screenshot which Is taken during android appium automation test.

Create And Run Capture Screenshot Test
Create new file CaptureScreenShot.java file under your Android package of your project and write bellow given test script In It.

package Android;

import io.appium.java_client.android.AndroidDriver;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class CaptureScreenShot {
 AndroidDriver driver;
 Dimension size;
 String destDir;
 DateFormat dateFormat;

 @BeforeTest
 public void setUp() throws Exception {
  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setCapability("deviceName", "ZX1B32FFXF");
  capabilities.setCapability("browserName", "Android");
  capabilities.setCapability("platformVersion", "4.4.2");
  capabilities.setCapability("platformName", "Android");
  capabilities.setCapability("appPackage", "io.appium.android.apis");
  capabilities.setCapability("appActivity", "io.appium.android.apis.ApiDemos");
  driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
 }

 @Test
 public void ScrollToTab() {
  // Scroll till element which contains "Views" text If It Is not visible on screen.
  driver.scrollTo("Views");
  // Click on Views.
  driver.findElement(By.name("Views")).click();
  // Scroll till element which contains "Tabs" text If It Is not visible on screen.
  driver.scrollTo("Tabs");
  //Call takeScreenShot() function to capture screenshot of android screen.
  takeScreenShot();

 }

 public void takeScreenShot() {
  // Set folder name to store screenshots.
  destDir = "screenshots";
  // Capture screenshot.
  File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
  // Set date format to set It as screenshot file name.
  dateFormat = new SimpleDateFormat("dd-MMM-yyyy__hh_mm_ssaa");
  // Create folder under project with name "screenshots" provided to destDir.
  new File(destDir).mkdirs();
  // Set file name using current date time.
  String destFile = dateFormat.format(new Date()) + ".png";

  try {
   // Copy paste file at destination folder location
   FileUtils.copyFile(scrFile, new File(destDir + "/" + destFile));
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 @AfterTest
 public void End() {
  driver.quit();
 }
}

Test Description
Above android appium software test will navigate to Views screen and scroll down till Tabs text on API Demos android app. Then It will call takeScreenShot(); method to capture and store android app screenshot. takeScreenShot() method will,
  • Capture screenshot using TakesScreenshot Interface of webdriver.
  • Get current date and time and store It in variable.
  • Create new folder with name "screenshots" under your project.
  • Set file name using current date and time.
  • Store file In "screenshots" folder.
When you run above test, It will create screenshots folder under your project and store screenshot file In It. Refresh your project folder after test execution to see new created folder and screenshot file.

This way you can capture and store screenshot of any android app.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值