testng 失敗自動截圖

testng執行case failed ,testng Listener會捕獲執行失敗,若是要實現失敗自動截圖,須要重寫Listener的onTestFailure方法java

那麼首先新建一個Listener 類,繼承TestListenerAdaptergit

package com.dbyl.libarary.utils;

import org.openqa.selenium.WebDriver;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

/**
 * 
 * @author Young
 *
 */
public class TestNGListener extends TestListenerAdapter {
    Log log = new Log(this.getClass());

    @Override
    public void onTestSuccess(ITestResult tr) {
        log.info("Test Success");
        super.onTestSuccess(tr);
    }

    @Override
    public void onTestFailure(ITestResult tr) {
        log.error("Test Failure");
        super.onTestFailure(tr);
        takeScreenShot(tr);
    }

    private void takeScreenShot(ITestResult tr) {
        UITest b = (UITest) tr.getInstance();
        WebDriver currentDirver = b.getDriver();
        System.out.println(currentDirver.getTitle());
        b.takeScreenShot();

    }

    @Override
    public void onTestSkipped(ITestResult tr) {
        log.error("Test Skipped");
        super.onTestSkipped(tr);
    }

    @Override
    public void onTestStart(ITestResult result) {
        log.info("Test Finsh");
        super.onTestStart(result);
    }

    @Override
    public void onStart(ITestContext testContext) {
        log.info("Test Start");
        super.onStart(testContext);
    }

    @Override
    public void onFinish(ITestContext testContext) {
        log.info("Test Finish");
        super.onFinish(testContext);
    }

}

我這裏主要重寫OnTestFailure的方法github

添加了一個takeScreenShot的方法web

接下來在UITest類中添加截圖的具體實現方法apache

/**
 * 
 */
package com.dbyl.libarary.utils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

/**
 * @author Young
 *
 */
public class UITest {
    WebDriver driver;
    Log log = new Log(this.getClass());

    public WebDriver getDriver() {
        return driver;
    }

    /**
     * init test case
     * 
     * @param driver
     */
    public void setDriver(WebDriver driver) {
        this.driver = driver;
    }

    public void init(WebDriver driver) {
        log.info("Start WebDriver");
        setDriver(driver);
    }

    /**
     * stop webdriver
     * 
     * @param driver
     */
    public void stop() {
        log.info("Stop WebDriver");
        driver.quit();

    }

    /**
     * @author Young
     */
    public void takeScreenShot() {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        Calendar cal = Calendar.getInstance();
        Date date = cal.getTime();
        String dateStr = sf.format(date);
        String path = this.getClass().getSimpleName() + "_" + dateStr + ".png";
        takeScreenShot((TakesScreenshot) this.getDriver(), path);
    }

    /**
     * @author Young
     * @param drivername
     * @param path
     */
    public void takeScreenShot(TakesScreenshot drivername, String path) {
        // this method will take screen shot ,require two parameters ,one is
        // driver name, another is file name
        String currentPath = System.getProperty("user.dir"); // get current work
        log.info(currentPath);
        File scrFile = drivername.getScreenshotAs(OutputType.FILE);
        // Now you can do whatever you need to do with it, for example copy
        try {
            log.info("save snapshot path is:" + currentPath + path);
            FileUtils.copyFile(scrFile, new File(currentPath + "\\" + path));
        } catch (Exception e) {
            log.error("Can't save screenshot");
            e.printStackTrace();
        } finally {
            log.info("screen shot finished");
        }
    }

}

接下來在case中使用這個Listener,有兩種辦法, 第一種直接在case類中添加註解@Listeners({ TestNGListener.class })框架

case代碼:eclipse

package com.dbyl.tests;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import com.dbyl.libarary.action.ViewHomePage;
import com.dbyl.libarary.utils.DriverFactory;
import com.dbyl.libarary.utils.TestNGListener;
import com.dbyl.libarary.utils.UITest;
//@Listeners({ TestNGListener.class })
public class loginTest extends UITest {

    WebDriver driver = DriverFactory.getChromeDriver();

    @BeforeMethod(alwaysRun = true)
    public void init() {

        super.init(driver);
        ViewHomePage.setDriver(driver);
    }

    @Test(groups = "loginTest")
    public void loginByUerName() throws Exception {
        ViewHomePage.viewMyProfile();
    }

    @AfterMethod(alwaysRun = true)
    public void stop() {
        super.stop();
    }

}
View Code

第二種方法是在eclipse run config 添加以下參數-listener com.dbyl.libarary.utils.TestNGListeneride

 

這樣就能實現case失敗自動截圖ui

這樣,這個框架可以實現一些基本操做,下一步還須要實現失敗重試 ,配合虛擬機this

下載地址:https://github.com/tobecrazy/Demo

相關文章
相關標籤/搜索