本文参考:http://www.cnblogs.com/hexianl/p/4958556.html

使用testng测试框架进行管理测试

1.创建监听,代码如下:

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 com.globalegrow.test.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();
}
}
}

2.创建测试类,代码如下:

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.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; public class Screenshot { static AppiumDriver driver;
@BeforeClass
public void setUp() throws MalformedURLException{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName","Android");
capabilities.setCapability("deviceName","192.168.35.102:5555");
capabilities.setCapability("platformVersion", "5.1");
capabilities.setCapability("browserName", "Chrome");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
} @Test
public void testExample() throws InterruptedException{ driver.get("http://wap-sammydress.com.trunk.s1.egomsl.com/");
driver.findElement(By.id("js_top_cate")).click();
Thread.sleep(3000);
driver.findElement(By.cssSelector("#nav > div > ul > li:nth-child(2) > p")).click();
Thread.sleep(5000);
driver.findElement(By.cssSelector("#header > div.top.on > a.icon_tag.top_user.isnoLogin")).click();
Thread.sleep(2000);
driver.findElement(By.id("email")).sendKeys("xxx@163.com");
driver.findElement(By.id("passwordsignl")).sendKeys("xxxx");
driver.findElement(By.id("js_signInBtn")).click(); Thread.sleep(2000);
} @AfterClass
public void tearDown(){
driver.quit();;
} public static AppiumDriver getDriver(){
return driver;
} }

tesng的xml文件配置监听如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="test">
<!-- 配置testng监听器 -->
<listeners>
<listener class-name="com.xxx.ScreenshotListener" />
</listeners>
<test name="version" preserve-order="true">
<classes>
<class name="com.xxx.test.Screenshot">
<methods>
<include name="testExample" />
</methods>
</class>
</classes>
</test>
</suite>

运行测试,testExample运行失败后截图如下:

 
分类: appium

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

  1. Appium-测试失败后获取屏幕截图的方法

    最近一直在研究appium,偶尔的机会发现断言后获取屏幕截图.觉得这个方法不错,分享给大家 这样以后在遇到断言,想截图错误屏幕的时候,能够用的上. 1.首先需要2个类,一个是测试类(TestDropL ...

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

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

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

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

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

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

  5. testNG实现test失败后重复执行,

    test失败自动执行大大提高测试结果的准确性, 1.修改testNG源码实现test失败自动执行, 首先获取testng的源码, 获取源代码,构建过程: $ git clone git://githu ...

  6. jmeter接口测试-线程组设置(断言失败后用例停止执行)

    问题描述: jmeter跑接口用例的时候,其中一条用例的对断言失败后,后面的用例都不执行了! 解决思路1: 考虑应该有地方设置,在菜单栏找了半天没找到,百度也没有查到 解决思路2: jmeter源码导 ...

  7. SQL Server 2008 R2中配置作业失败后邮件发送通知

    SQL Server日常维护中难免会遇到作业失败的情况.失败后自然需要知道它失败了,除了例行检查可以发现出错以外,有一个较实时的监控还是很有必要的.比较专业的监控系统比如SCOM虽然可以监控作业执行情 ...

  8. Appium使用Python运行appium测试的实例

    Appium使用Python运行appium测试的实例 一.  Appium之介绍 https://testerhome.com/topics/8038 详情参考--https://testerhom ...

  9. Unittest 支持 case 失败后自动截图功能的另外两种方式

    原生的unittest框架是不支持case失败后自动截图的功能的,网上看了大家的解决办法,大体上分为两种:1.要么加装饰器2.也有人封装断言这里我们看看还有没有其他的更加方便的方法值得大家一起探讨一下 ...

随机推荐

  1. window使用结束进程

    在cmd中输入下面信息: 1. 查看所有进程占用的端口 :netstat -ano 2.查看占用指定端口的程序:netstat –ano|findstr 指定端口号 3.杀死相关的进程: 方法一:使用 ...

  2. 实例:接口并发限流RateLimiter

    需求:接口每秒最多只能相应1个请求 1.创建 全局类对象 import com.google.common.util.concurrent.RateLimiter; import org.spring ...

  3. python 在线生成文字云

    在线生成文字云 在线生成文字云地址  http://a.leechg.com:8080/wordcloud 效果图 大体步骤 1 接收请求中的文本,通过结巴分词处理文本. seg_generator ...

  4. http1.X与2.0

    HTTP HTTP 1.X HTTP是建立在TCP协议上的,HTTP协议的瓶颈及优化都是基于TCP协议本身的特性. TCP建立连接时有三次握手 会有1.5RTT的延迟,为了避免每次请求都经历握手待来的 ...

  5. REST解惑

    本文是「架构风格:你真的懂REST吗?」的补充! REST全称是Representational State Transfer,目前普遍接受的中文翻译为「表述性状态转移」! 即使翻译过来了,你依然有一 ...

  6. Servlet过滤器Filter和监听器

    一.Servlet过滤器的概念: *********************************************************************************** ...

  7. Context initialization failed org.springframework.beans.factory.BeanCreationException

    严重: Context initialization failed org.springframework.beans.factory.BeanCreationException: Error cre ...

  8. iOS视频处理

    在iOS中,apple提供了AVFoundation 用来处理音视频,基本能满足一些常用的音视频处理需求,而且能调用的硬件编解码接口,能提高不少效率,这是其它库所不能达到的.最近做过的Recnow S ...

  9. iOS开发学习资源

    最近想写点关于iOS开发的总结和心得.虽然网上资源一大堆,质量参差不齐,还是推荐一点干货吧! https://www.objc.io/issues/  这个网站的文章质量很高,很多干货,可惜今年已经停 ...

  10. Swift_类和结构体

    Swift_类和结构体 点击查看源码 struct Resolution { var width = 0 var height = 0 } class VideoMode { var resoluti ...