UI自动化框架总结

2019-06-27  本文已影响0人  Loistein

自动化框架

File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
filePath = "result/test-report/screenshot/"+ fileName + ".jpg";

UI自动化之图像对比

package UITest;

import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ImageCompare {
    public static Boolean imageCompare() {
    File fileInput = new File("D:\\2.png");
    File fileOutput = new File("D:\\1.png");
    try {
        BufferedImage bufferInput = ImageIO.read(fileInput);
        BufferedImage bufferOutput = ImageIO.read(fileOutput);
        
        DataBuffer dafileInput= bufferInput.getData().getDataBuffer();
        DataBuffer dafileOutput= bufferOutput.getData().getDataBuffer();
        
        int sizeFileInput = dafileInput.getSize();
        int sizeFileOutput = dafileOutput.getSize();
        
        Boolean matchFlag = false;
        
        if(sizeFileInput == sizeFileOutput) {
            for(int i = 0; i < sizeFileInput; i ++) {
                if(dafileInput.getElem(i) != dafileOutput.getElem(i)) {
                    matchFlag = false;
                    break;
                }
            }
             matchFlag = true;
        }       
        return matchFlag;       
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;   
    }   
    public static void main(String args[]) {
        System.out.println(imageCompare());
    }
}

UI自动化截图

File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.***FILE***);
public class TestResultListener extends TestListenerAdapter {

    private static Logger logger = Logger.getLogger(TestResultListener.class.getName());
    protected ITestContext testContext = null; 
    String  browser = null;

    @Override
    public void onStart(ITestContext testContext) { 
        this.testContext = testContext;
        browser = String.valueOf(testContext.getCurrentXmlTest().getParameter("browserName"));
        super.onStart(testContext);
    }

    @Override
    public void onTestFailure(ITestResult tr) {
        super.onTestFailure(tr);
        WebDriver webDriver = (WebDriver) testContext.getAttribute("SELENIUM_DRIVER"); 
        saveScreenShot(tr, webDriver,browser);
    }

    @Override
    public void onTestSkipped(ITestResult tr) {
        super.onTestSkipped(tr);
        WebDriver webDriver = (WebDriver) testContext.getAttribute("SELENIUM_DRIVER");
        saveScreenShot(tr, webDriver,browser);

    }

失败用例重新运行

public class TestngRetry implements IRetryAnalyzer {
    static {
        LogConfiguration.initLog("TestngRetryPage_");
    }
    private static Logger logger = Logger.getLogger(TestngRetry.class);
    private int retryCount = 1;
    private static int maxRetryCount;

    static {
        ConfigReader config = ConfigReader.getInstance();
        maxRetryCount = config.getRetryCount();
        logger.info("RetryCount=" + maxRetryCount);
        logger.info("SourceDir=" + config.getSourceCodeDir());
        logger.info("SourceEncoding=" + config.getSrouceCodeEncoding());
    }

    public boolean retry(ITestResult result) {
        if (retryCount <= maxRetryCount) {
            String message = "Retry for: [" + result.getName() + "] on class [" + result.getTestClass().getName() + "] retry " + retryCount + " times";
            logger.info(message);
            Reporter.setCurrentTestResult(result);
            Reporter.log("RunCount=" + (retryCount + 1));
            retryCount++;
            return true;
        }
        return false;
    }
上一篇下一篇

猜你喜欢

热点阅读