SWTBot中运行失败截屏的控制.

            在swtbot中运行失败会生成失败的截屏图片会在项目的根目录下生成一个默认的screenshots目录存在失败的截屏图片.

关于截屏的ScreenshotCaptureListener实现了一个junit失败通知类接口RunListener.

package org.junit.runner.notification;

import org.junit.runner.Description;
import org.junit.runner.Result;

public class RunListener {
	public void testRunStarted(Description description) throws Exception {
	}

	public void testRunFinished(Result result) throws Exception {
	}

	public void testStarted(Description description) throws Exception {
	}

	public void testFinished(Description description) throws Exception {
	}

	public void testFailure(Failure failure) throws Exception {
	}

	public void testAssumptionFailure(Failure failure) {
	}

	public void testIgnored(Description description) throws Exception {
	}
}

 

截屏的通知监听类为ScreenshotCaptureListener:

package org.eclipse.swtbot.swt.finder.junit;

import org.apache.log4j.Logger;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;

/**
 * Captures screenshots on failure notifications.
 *
 * @author Hans Schwaebli (Bug 259787)
 * @version $Id$
 * @noinstantiate This class is not intended to be instantiated by clients.
 */
public final class ScreenshotCaptureListener extends RunListener {
	/** The logger. */
	private static Logger	log					= Logger.getLogger(SWTBotApplicationLauncherClassRunner.class);

	/** Counts the screenshots to determine if maximum number is reached. */
	private static int		screenshotCounter	= 0;

	public void testFailure(Failure failure) throws Exception {
		captureScreenshot(failure);
	}

	private void captureScreenshot(Failure failure) {
		try {
			int maximumScreenshots = SWTBotPreferences.MAX_ERROR_SCREENSHOT_COUNT;
			String fileName = SWTBotPreferences.SCREENSHOTS_DIR + "/" + failure.getTestHeader() + "." + SWTBotPreferences.SCREENSHOT_FORMAT.toLowerCase(); //$NON-NLS-1$
			if (++screenshotCounter <= maximumScreenshots) {
				captureScreenshot(fileName);
			} else {
				log.info("No screenshot captured for '" + failure.getTestHeader() + "' because maximum number of screenshots reached: " //$NON-NLS-1$ 
						+ maximumScreenshots);
			}
		} catch (Exception e) {
			log.warn("Could not capture screenshot", e); //$NON-NLS-1$
		}
	}

	private boolean captureScreenshot(String fileName) {
		return SWTUtils.captureScreenshot(fileName);
	}
	
	public int hashCode() {
		return 31;
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		return true;
	}
	
}

 

 截屏相关的常量:

MAX_ERROR_SCREENSHOT_COUNT:控制截屏的次数;

	/**
	 * The maximum number of screenshots that SWTBot should capture. Defaults to 100. To set another default use the
	 * system property
	 * {@value org.eclipse.swtbot.swt.finder.utils.SWTBotPreferenceConstants#KEY_MAX_ERROR_SCREENSHOT_COUNT}.
	 */
	public static int			MAX_ERROR_SCREENSHOT_COUNT	= toInt(System.getProperty(KEY_MAX_ERROR_SCREENSHOT_COUNT, "100"), 100);

 

SCREENSHOTS_DIR:控制截屏目录名称:

	/**
	 * The directory in which screenshots should be generated. Defaults to "screenshots". To set another default use the
	 * system property {@value org.eclipse.swtbot.swt.finder.utils.SWTBotPreferenceConstants#KEY_SCREENSHOTS_DIR}.
	 */
	public static String		SCREENSHOTS_DIR				= System.getProperty(KEY_SCREENSHOTS_DIR, "screenshots");

 

SCREENSHOT_FORMAT:控制截屏的图片的格式:

	/**
	 * The screenshot image format to be used. Defaults to "jpeg". To set another default use the system property
	 * {@value org.eclipse.swtbot.swt.finder.utils.SWTBotPreferenceConstants#KEY_SCREENSHOT_FORMAT}. The value must be
	 * one these: BMP, GIF, ICO, JPEG, JPG, PNG or TIFF.
	 */
	public static String		SCREENSHOT_FORMAT			= System.getProperty(KEY_SCREENSHOT_FORMAT, "jpeg");

 

 

 

你可能感兴趣的:(SWT,swtbot截屏)