zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    700. Search in a Binary Search Tree
    100. Same Tree
    543. Diameter of Binary Tree
    257. Binary Tree Paths
    572. Subtree of Another Tree
    226. Invert Binary Tree
    104. Maximum Depth of Binary Tree
    1、解决sublime打开文档,出现中文乱码问题
    移植seetafaceengine-master、opencv到ARM板
    ubuntu16.04-交叉编译-SeetaFaceEngine-master
  • 原文地址:https://www.cnblogs.com/chenlimei/p/10417889.html
Copyright © 2011-2022 走看看