zoukankan      html  css  js  c++  java
  • 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();
    }

    }

    }

  • 相关阅读:
    javascript 中加载图片大小与图片真是大小不一样解决方案
    连接数据库类
    jquery中“this”不同时刻的不同含义
    jquery中bind事件
    Sql中Output参数用法和分页存储过程
    C#中静态方法和静态变量的使用问题
    asp.net中javascript中json和C#对象之间的转换
    asp.net中加载自用户定义控件
    瀑布流布局——JS+绝对定位
    【笔记】——Javascript(1)
  • 原文地址:https://www.cnblogs.com/hexianl/p/4958556.html
Copyright © 2011-2022 走看看