本文参考: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. NodeJS学习日记--VSCode下调试

    在vscode中打开项目文件夹 点击左侧的调试菜单,在打开的页面中点击下拉框并点击添加配置 在弹出框中选择 node.js vscode 会自动在项目文件夹下添加.vscode文件夹,并创建launc ...

  2. spring使用 hibernate jpa JpaRepository

    使用JpaRepository需要两个架包: <dependency> <groupId>org.springframework.data</groupId> &l ...

  3. Race UVA - 12034(dp+打表)

    Disky and Sooma, two of the biggest mega minds of Bangladesh went to a far country. They ate, coded ...

  4. 在WinCE上播放声音、设置透明图片、系统音量 C#

    记录一下用到的C#在WinCE平台上的相关技巧备查 1.C#在WinCE上实现透明图片 1 2 3 4 5 6 7 8 9 using System.Drawing.Imaging;       pu ...

  5. Razor

    什么是Razor 1.Razor是一种将基于服务器的代码添加到网页中的标记语法 2.Razor具有传统ASP.NET标记功能 3.Razor是一种服务器端的标记语法 4.Razor同时支持C#和VB ...

  6. JS异步编程 (1)

    JS异步编程 (1) 1.1 什么叫异步 异步(async)是相对于同步(sync)而言的,很好理解. 同步就是一件事一件事的执行.只有前一个任务执行完毕,才能执行后一个任务.而异步比如: setTi ...

  7. Ubantu 更新时间方法

    1.首先查看时区: swfsadmin@swfsubuntu:~$ date -RTue, 17 Dec 2013 18:23:01 +0800 如果要修改时区,执行sudo tzselect 2.选 ...

  8. Xcode9.2 添加iOS11.2以下旧版本模拟器

    问题起源 由于手边项目需要适配到iOS7, 但是手边的测试机都被更新到最新版本,所以有些潜在的bug,更不发现不了.最近就是有个用户提出一个bug,而且是致命的,app直接闪退.app闪退,最常见的无 ...

  9. 【sql server常用操作{增删改查}】

    use DB_x   go   drop database DB_y   create database DB_y --创建数据库   on primary --指定主数据文件   (   name= ...

  10. JavaScript深入之参数按值传递

    在<JavaScript高级程序设计>第三版 4.1.3,讲到传递参数: ECMAscript中所有函数的参数都是按值传递 按值传递 也就是,把函数外部的值复制给函数内部的参数,就和把值从 ...