Appium-测试失败后屏幕截图的

Posted 星星故乡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Appium-测试失败后屏幕截图的相关的知识,希望对你有一定的参考价值。

本文参考: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常用APi03

如何将失败的屏幕截图包含到代码接收报告中

Appium失败截图及重试机制封装

Appium自动化中截图的问题

Selenium 可以截取 JUnit 测试失败的屏幕截图吗?

移动测试之appium+python 入门代码