最近一直在研究appium,偶尔的机会发现断言后获取屏幕截图。觉得这个方法不错,分享给大家

这样以后在遇到断言,想截图错误屏幕的时候,能够用的上。

1.首先需要2个类,一个是测试类(TestDropList),另外一个是监听类(ScreenshotListener)

public class ScreenshotListener extends TestListenerAdapter{
@Override
public void onTestFailure(ITestResult tr){
AppiumDriver driver = Screenshot.getDriver();
File location = new File("screenshots1"); //在默认的工作目录下面创建一个名字叫screenshots1的文件夹,用来存放图片的
String screenShotName = location.getAbsolutePath()+File.separator+tr.getMethod().getMethodName()+".png"; // 
File screenShot = driver.getScreenshotAs(OutputType.FILE);
try{
FileUtils.copyFile(screenShot, new File(screenShotName));
}
catch(IOException e){
e.printStackTrace();
}

}

}

2. 创建测试类,并添加断言

@Test //本测试用来用来测试选项是否是France

//测试过程中选择的是India选择,期望结果是France

//这样测试完成后会把错误的信息记录在图片中
public void testExample() throws IOException{
WebElement spinner = driver.findElement(By.id("android:id/text1"));
spinner.click();
driver.scrollToExact("India");
WebElement optionindia = driver.findElement(By.name("India"));
optionindia.click();
WebElement result= optionindia;
//Check the calculated value on the edit box
assert result.getText().equals("France"):"Actual value is :"+result.getText()+" did not match with expected value: France";
}

这样在完成测试后会将错误的断言结果用图片的形式保存

完整代码如下:

package appTest;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import io.appium.java_client.AppiumDriver;
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;

import com.lib.ctrip.ScreenshotListener;

@Listeners({ScreenshotListener.class})
public class Screenshot {
static AppiumDriver driver;
@BeforeClass
public void setUp() throws MalformedURLException{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "AndroidUI");
capabilities.setCapability("platformVersion", "1.0");
capabilities.setCapability("appPackage", "com.android.androidui");
capabilities.setCapability("appActivity", "com.android.androidui.MainActivity");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}

@Test
public void testExample() throws IOException{
WebElement spinner = driver.findElement(By.id("android:id/text1"));
spinner.click();
driver.scrollToExact("India");
WebElement optionindia = driver.findElement(By.name("India"));
optionindia.click();
WebElement result= optionindia;
//Check the calculated value on the edit box
assert result.getText().equals("France"):"Actual value is :"+result.getText()+" did not match with expected value: France";
}

@AfterClass
public void tearDown(){
driver.closeApp();
}

public static AppiumDriver getDriver(){
return driver;
}

}

package com.lib.ctrip;

import io.appium.java_client.AppiumDriver;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

import appTest.Screenshot;

public class ScreenshotListener extends TestListenerAdapter{
@Override
public void onTestFailure(ITestResult tr){
AppiumDriver driver = Screenshot.getDriver();
File location = new File("screenshots1");
String screenShotName = location.getAbsolutePath()+File.separator+tr.getMethod().getMethodName()+".png";
File screenShot = driver.getScreenshotAs(OutputType.FILE);
try{
FileUtils.copyFile(screenShot, new File(screenShotName));
}
catch(IOException e){
e.printStackTrace();
}

}

}

Appium-测试失败后获取屏幕截图的方法的更多相关文章

  1. 史上最全Linux提权后获取敏感信息方法

    http://www.freebuf.com/articles/system/23993.html 在本文开始之前,我想指出我不是专家.据我所知,在这个庞大的区域,没有一个“神奇”的答案.分享,共享( ...

  2. Fedora 21 安装 Nvidia 驱动以及失败后的补救方法

    在 Linux 桌面系统下玩了这么久,大部分时间都是使用 Ubuntu,偶尔使用一下 Fedora.我的电脑中安装有多个 Linux 发行版,见这里<在同一个硬盘上安装多个Linux发行版及Fe ...

  3. linux尝试登录失败后锁定用户账户的两种方法

    linux尝试登录失败后锁定用户账户的两种方法 更新时间:2017年06月23日 08:44:31   作者:Carey    我要评论   这篇文章主要给大家分享了linux尝试登录失败后锁定用户账 ...

  4. Appium-测试失败后屏幕截图的

    本文参考:http://www.cnblogs.com/hexianl/p/4958556.html 使用testng测试框架进行管理测试 1.创建监听,代码如下: import io.appium. ...

  5. spring项目启动后,获取bean的方法总结

    如果在web项目中,用到定时器的朋友可能会遇到使用spring注解的方式获取bean的时候报空指针的异常.这是就可以使用手工的方法获取spring容器中的bean了. 下面是具体的方法: 1.先说一个 ...

  6. Jmeter-接口测试参数化后循环断言不同内容的方法

    前言 各位小伙伴在做接口自动化有没遇到过这样的问题,CSV文件参数化测试数据后,只能通过人工的的方法去查看结果,不懂写代码去循环断言返回的结果.今天我们来学习一下,不用写代码,就用响应断言,怎么实现循 ...

  7. 【亲测】Appium测试Android混合应用时,第二次切换到WebView失败

    要解决的问题:Appium测试Android混合应用时,第二次切换到WebView时失败 原因分析:在用Appium测试Android混合应用时,当程序第一次切换到WebView时,可以正常进行自动化 ...

  8. springboot打成jar后获取classpath下文件失败

    原文链接:https://blog.csdn.net/qq_18748427/article/details/78606432 springboot打成jar后获取classpath下文件失败 使用如 ...

  9. 删除Windows中隐藏的物理网卡和网络虚拟化失败后的虚拟网卡

    Windows环境下,在更换硬件服务器主板和网卡等硬件.恢复操作系统或者网络虚拟化失败后,可能会出现网卡方面的问题.例如,设备管理器中多了不应该存在的网卡:因命名冲突无法重命名当前网络连接:IP地址冲 ...

随机推荐

  1. JavaScript 闭包浅析

    词法作用域 闭包 概念 作用 用法 待续 词法作用域 词法作用域是指一个变量在源码中声明的位置作为它的作用域.同时嵌套的函数可以访问到其外层作用域中声明的变量. 函数中的定义的局部变量只能由函数的内部 ...

  2. 关于vue :style 的几种使用方式

    :style的使用 一 ,最通用的写法 <p :style="{fontFamily:arr.conFontFamily,color:arr.conFontColor,backgrou ...

  3. python3练习100题——021

    题目很容易,只要理清了数学思想就可以解出来,所以本来不是很喜欢这种题. 后来看到有大神用递归解,觉得还是很值得学习的. 原题链接:http://www.runoob.com/python/python ...

  4. vue项目单页

    <template> <div> <div v-if="type === 'A'">A</div> <div v-else-i ...

  5. HTML 5的革新——语义化标签(一)

    HTML 5的革新之一:语义化标签一节元素标签. 在HTML 5出来之前,我们用div来表示页面章节,但是这些div都没有实际意义.(即使我们用css样式的id和class形容这块内容的意义).这些标 ...

  6. spring(六):事务

    事务特性ACID 原子性(Atomicity):即事务是不可分割的最小工作单元,事务内的操作要么全做,要么全不做: 一致性(Consistency):在事务执行前数据库的数据处于正确的状态,而事务执行 ...

  7. 路飞-后台home模块

    home模块 创建home模块 """ 前提:在 luffy 虚拟环境下 1.终端从项目根目录进入apps目录 >: cd luffyapi & cd ap ...

  8. 题解【loj6277】数列分块入门1

    题目描述 给出一个长为\(n\)的数列,以及\(n\)个操作,操作涉及区间加法,单点查值. 输入格式 第一行输入一个数字\(n\). 第二行输入\(n\)个数字,第\(i\)个数字为\(a_{i}\) ...

  9. 通过jquery将多选框变单选框

    多选框的name值为checkboxlist是复选框选中后更换其他复选框状态. function queryListByParam(e){ $("input[name='checkboxli ...

  10. 并发之atomic原子操作

    Atomic类 Atomic类是一个简单的高效的.线程安全的递增递减方案,在多线程或者并发环境中,我们常常会遇到这种情况 int i=0; i++ 稍有经验的同学都知道这种写法是线程不安全的.为了达到 ...