本文参考: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. web.xml中Filter的作用

    Servlet中的过滤器Filter是实现了javax.servlet.Filter接口的服务器端程序,主要的用途是过滤字符编码.做一些业务逻辑判断等.其工作原理是,只要你在web.xml文件配置好要 ...

  2. Mybatis 和Spring整合之原始dao开发

    F:\Aziliao\mybatis\代码\31.mybatis与spring整合-开发原始dao 1.1. SqlMapConfig.xml <?xml version="1.0&q ...

  3. JS中的prototype (转载)

    JS中的prototype   JS中的phototype是JS中比较难理解的一个部分 本文基于下面几个知识点: 1 原型法设计模式 在.Net中可以使用clone()来实现原型法 原型法的主要思想是 ...

  4. es6之数组方法

    //兼容插件 babel-polyfill values()等存在兼容问题,需要加载babel-polyfill插件 .keys()  获取数组的key值 .values()  获取数组的value值 ...

  5. UITableViewCell在重用ID时为何加上Static关键字

    UITableViewCell在重用ID时为何加上Static关键字 先回顾一下iOS各种变量作用域和生命周期相关知识: 1.方法中临时变量存储在栈区,出了该方法,临时变量会被自动销毁.但是如果给方法 ...

  6. scroll(),scrollTop(),scrollBy()无效问题的总结

    · 使用的浏览器:Chrome(67.0.3396.87)/火狐(60.0.2)/IE(ie7和ie8),均为PC端. · 代码如下 表现结果: Chrome:只有第一次打开标签页面是有效的(在当前标 ...

  7. About Me - 关于

    0x00 简介 97年生 计算机相关专业,无线电安全攻防方向. 涉猎较多,喜欢研究无线.硬件.软件.网络.攻击.检测.防御等各类安全技术 精通较少,主要擅长的还是硬件.渗透.无线攻击方面. 现阶段在研 ...

  8. 关于gitbash一直报:sh: __git_ps1: command not found的解决办法

    curl -o ~/.git-prompt.sh https://raw.githubusercontent.com/git/git/master/contrib/completion/git-pro ...

  9. 记一次MySQL中Waiting for table metadata lock问题的处理

    起因:由于需要,要把一张表的一个字段从不是 null 改成 可null,我用的Navicat Premium ,但是在保存的时候,工具无响应了,几个同事操作都是这样的,很奇怪,怀疑是不是由于表被锁了还 ...

  10. jQuery UI.Layout 参数

    applyDefaultStyles: true,//应用默认样式 scrollToBookmarkOnLoad:false,//页加载时滚动到标签 showOverflowOnHover:false ...