Appium - Capture Screenshot On Failure Or Pass In Android Test Automation

Earlier In previous post, We learnt how to  capture screenshot of android mobile software app screen 
on  any stage In android appium software automation test. Now supposing I wants to  capture screenshot on test failure  or I wants to capture screenshot on software test pass . In this case,   ITestListener Interface  of TestNG can help us to capture screenshot on certain occurrence like test failure or software test pass.

ITestListener Interface
ITestListener Is an Interface of TestNG which can Invoke it's specific method on test failure or test pass or test skips, ect.. There are bellow given useful methods available In ITestListener Interface.
  • onFinish(ITestContext context) - Invoked after all the software tests have run.
  • onStart(ITestContext context) -  Invoked after the test class is instantiated.
  • onTestFailure(ITestResult result) - Invoked each time a test fails.
  • onTestSkipped(ITestResult result) - Invoked each time a test is skipped.
  • onTestStart(ITestResult result) -  Invoked each time before a test will be invoked.
  • onTestSuccess(ITestResult result) - Invoked each time a test succeeds.
You can view more detail on ITestListener Interface on  THIS PAGE. Earlier we have used It In data driven framework. View THIS PAGE for more detail.

App To Use Capture Screenshot Test
We will use android mobile's calculator app In this software automation test example.

Aim to Achieve
We wants to capture screenshot on test pass or failure occurrence and store file in related folder. If test Is fail then capture screenshot and store It In  Failures folder and If test Is pass then capture screenshot and store It In  Pass folder under screenshots folder of your project as shown in bellow Image.


How To Do
We will create separate class file to write logic of capture screenshot on test fail or pass. Inside It, We will create separate method captureScreenShot to write logic to capture screenshot. We will use testng assertion to verify our software test is fail or pass and use ITestListener Interface to Invoke  onTestFailure method on assertion failure and  onTestSuccess on assertion pass and then call captureScreenShot method to take screenshot.

In test class file, We will write two test methods TestForFailure() and TestForPass(). Test method TestForFailure() Is written In such a way to fail It Intentionally.

Screenshot of TestForFailure() method will be stored Inside Failure folder and screenshot of TestForPass() method will be stored Inside Success folder.

Create And Run Test
I have created very simple example to demonstrate you how to capture screenshot on test failure or pass. You need to create two class files under Android package of your project as bellow.

ScreenshotUtility.java file contains logic to capture screenshot on test failure or pass.

ScreenshotUtility.java
package Android;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class ScreenshotUtility implements ITestListener {
 // This method will execute before starting of Test suite.
 public void onStart(ITestContext tr) {

 }

 // This method will execute, Once the Test suite is finished.
 public void onFinish(ITestContext tr) {

 }

 // This method will execute only when the test is pass.
 public void onTestSuccess(ITestResult tr) {
  captureScreenShot(tr, "pass");
 }

 // This method will execute only on the event of fail test.
 public void onTestFailure(ITestResult tr) {
  captureScreenShot(tr, "fail");
 }

 // This method will execute before the main test start (@Test)
 public void onTestStart(ITestResult tr) {

 }

 // This method will execute only if any of the main test(@Test) get skipped
 public void onTestSkipped(ITestResult tr) {
 }

 public void onTestFailedButWithinSuccessPercentage(ITestResult tr) {
 }

 // Function to capture screenshot.
 public void captureScreenShot(ITestResult result, String status) {
  // AndroidDriver driver=ScreenshotOnPassFail.getDriver();
  String destDir = "";
  String passfailMethod = result.getMethod().getRealClass().getSimpleName() + "." + result.getMethod().getMethodName();
  // To capture screenshot.
  File scrFile = ((TakesScreenshot) ScreenshotOnPassFail.driver).getScreenshotAs(OutputType.FILE);
  DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy__hh_mm_ssaa");
  // If status = fail then set folder name "screenshots/Failures"
  if (status.equalsIgnoreCase("fail")) {
   destDir = "screenshots/Failures";
  }
  // If status = pass then set folder name "screenshots/Success"
  else if (status.equalsIgnoreCase("pass")) {
   destDir = "screenshots/Success";
  }

  // To create folder to store screenshots
  new File(destDir).mkdirs();
  // Set file name with combination of test class name + date time.
  String destFile = passfailMethod + " - " + dateFormat.format(new Date()) + ".png";

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


ScreenshotOnPassFail.java file contain android cal app test. If you see In bellow given test, We have used @Listeners annotation. It mansion that testng listeners are Implemented In ScreenshotUtility class file to listen.

ScreenshotOnPassFail.java
package Android;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners({ ScreenshotUtility.class })
public class ScreenshotOnPassFail {
 static AndroidDriver driver;

 @BeforeClass
 public void initialize() throws MalformedURLException {
  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", "com.android.calculator2");
  capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");
  driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
 }

 // Method Is written In such a way to fail It Intentionally.
 // This method will fail as actual result 6 will not match with expected 7.
 @Test
 public void TestForFailure() throws IOException {
  // Click on DELETE/CLR button to clear result text box before running test.
  ((WebElement) driver.findElements(By.xpath("//android.widget.Button")).get(0)).click();
  // Click on 2, +, 4 and = buttons.
  driver.findElement(By.name("2")).click();
  driver.findElement(By.name("+")).click();
  driver.findElement(By.name("4")).click();
  driver.findElement(By.name("=")).click();
  // Get result from calc result textbox.
  String result = driver.findElement(By.className("android.widget.EditText")).getText();
  // Compare actual and expected result using testng assertion and mark test pass or fail based on result.
  // Assertion will fail. So It will call onTestFailure method from ScreenshotUtility.
  assert result.equals("7") : "Expected value : 7 did not match with Actual value: "+ result;
 }

 @Test
 public void TestForPass() throws IOException {
  // Click on DELETE/CLR button to clear result text box before running test.
  ((WebElement) driver.findElements(By.xpath("//android.widget.Button")).get(0)).click();
  // Click on 3, +, 4 and = buttons.
  driver.findElement(By.name("3")).click();
  driver.findElement(By.name("+")).click();
  driver.findElement(By.name("4")).click();
  driver.findElement(By.name("=")).click();
  // Get result from calc result textbox.
  String result = driver.findElement(By.className("android.widget.EditText")).getText();
  // Compare actual and expected result using testng assertion and mark test pass or fail based on result.
  // Assertion will pass. So It will call onTestSuccess method from ScreenshotUtility.
  assert result.equals("7") : "Expected value : 7 did not match with Actual value: " + result;
 }

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

Connect your device with PC and run above ScreenshotOnPassFail test using testng and appium.
  • TestForFailure() method : Here assertion will fail so onTestFailure method will be called from ScreenshotUtility.java. It will capture screenshot and store It in Failures folder under screenshots folder.
  • TestForPass() method : Here assertion will pass so onTestSuccess method will be called from ScreenshotUtility.java. It will capture screenshot and store It in Success folder under screenshots folder.
This Is the way to capture screenshot on failure or pass of any android appium software automation test.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: appium-python-client安装包可以通过pip命令进行安装,具体步骤如下: 1. 打开命令行工具(如Windows下的cmd或者Linux下的终端),输入以下命令安装pip: ``` sudo apt-get install python-pip ``` 2. 安装appium-python-client,输入以下命令: ``` pip install Appium-Python-Client ``` 3. 等待安装完成即可使用appium-python-client。 ### 回答2: appium-python-client是一个用于进行移动应用测试的Python库。它作为Appium测试框架的一个客户端,提供了许多功能和方法来编写和执行测试脚本。 要安装appium-python-client,需要先确保已经安装了Python环境。以下是安装appium-python-client的步骤: 1. 打开终端或命令提示符,并进入到所需的Python项目目录。 2. 使用pip命令安装appium-python-client。在终端或命令提示符中输入以下命令: pip install Appium-Python-Client 在执行命令后,pip会从Python Package Index(PyPI)上下载并安装appium-python-client。 安装完成后,您可以在Python脚本中导入appium-python-client并开始编写测试代码。 例如,您可以使用以下命令导入appium-python-client库: ```python from appium import webdriver ``` 然后,您可以使用appium-python-client提供的方法来启动Appium服务器、初始化移动设备连接、定位元素,并执行各种移动应用测试操作。 总结: appium-python-client是一个用于进行移动应用测试的Python库,可以通过pip命令进行安装。安装完成后,您可以在Python脚本中导入该库并使用其提供的方法来编写和执行移动应用测试脚本。 ### 回答3: appium-python-client是一个用于与Appium服务器进行通信的Python库。要安装appium-python-client,您可以按照以下步骤进行操作: 1. 确保您的电脑已经安装了Python解释器。您可以通过在命令行中输入“python --version”来验证Python是否已经安装。 2. 打开命令行或终端,并输入以下命令来安装appium-python-client: ``` pip install Appium-Python-Client ``` 注意:您需要确保您的电脑已经安装了pip,它是一个用于安装Python库的包管理工具。如果您的电脑上没有安装pip,您可以通过以下命令来安装它: ``` python -m ensurepip --upgrade ``` 3. 安装完成后,您可以在Python脚本中导入appium-python-client模块并使用它来与Appium服务器进行通信。例如,您可以使用以下代码示例来连接到Appium服务器: ```python from appium import webdriver desired_caps = { 'platformName': 'Android', 'platformVersion': '9', 'deviceName': 'Android Emulator', 'app': 'path/to/your/app.apk' } driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) ``` 这将创建一个WebDriver实例,用于控制连接到Appium服务器的设备。您可以使用WebDriver实例来执行各种测试操作,例如查找元素、发送输入等。 通过按照以上步骤安装appium-python-client,您就可以在Python中使用它来创建和执行Appium测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值