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();
}

}

}

posted @ 2015-11-12 11:52  hexianling  阅读(7502)  评论(0编辑  收藏  举报