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

最近一直在研究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();
}

}

}

转载于:https://www.cnblogs.com/hexianl/p/4958556.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值