Webdriver+Testng实现测试用例失败自动截图功能

       在自动化测试中,如果有测试用例运行失败,我们想到使用截图方式对页面出问题的现场进行“照相”,方便后继的问题排查。


1.定义一个截图类实现具体的截图操作:

package ec.qa.autotest.ui.testng.listener;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Date;

import javax.management.loading.PrivateClassLoader;

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

import ec.qa.autotest.ui.testbase.TestBase;

public class ScreenShotOnFailure {

	private final static String SCREEN_SHOT_PATH = "test-output/screen-shot";
	private static String SCREEN_SHOT_NAME = null;

	public static void takeScreentShot() throws IOException {
		File screenShotDir = new File(SCREEN_SHOT_PATH);
		if (!screenShotDir.exists()) {
			screenShotDir.mkdirs();
		}

		SCREEN_SHOT_NAME = String.valueOf(new Date().getTime()) + ".jpg";
		FileUtils.copyFile(TestBase.getWebDriver().getScreenshotAs(OutputType.FILE),
				new File(SCREEN_SHOT_PATH + "/" + SCREEN_SHOT_NAME));
	}

	public static String getScreenShotPath() {
		return SCREEN_SHOT_PATH;
	}

	public static String getScreenShotName() {
		return SCREEN_SHOT_NAME;
	}
}

2.实现testng的listener接口,在onTestFailure方法中调用上面的截图方法:

public class TestngRetryListener implements ITestListener {

	public void onTestFailure(ITestResult result) {
		try {
			ScreenShotOnFailure.takeScreentShot();
			System.out.println(result.getMethod().getMethodName() + " failed, the screenshot saved in "
					+ ScreenShotOnFailure.getScreenShotPath() + " screenshot name : "
					+ ScreenShotOnFailure.getScreenShotName());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void onTestStart(ITestResult result) {
		// TODO Auto-generated method stub

	}

	public void onTestSuccess(ITestResult result) {
		// TODO Auto-generated method stub
	}

	public void onTestSkipped(ITestResult result) {
		// TODO Auto-generated method stub

	}

	public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
		// TODO Auto-generated method stub

	}

	public void onStart(ITestContext context) {
		// TODO Auto-generated method stub

	}
}


在测试用例类添加上面的监听器:


@Listeners({ TestngRetryListener.class })
public class LoginAdminPortal extends AdminPortalTestBase{
	
	@AutoInject
	private NavigationMenu ctPage;
	
	@Test(invocationCount = 1)
	public void loginAdminPortal() throws Exception{
		AssertJUnit.assertEquals("欢迎您,",ctPage.getWelcomeContent());
                AssertJUnit.assertEquals("超级管理员1",ctPage.getCurLoginUser());
		ctPage.goToProdcutLibPage();
	}
}

运行结果示例:

Starting ChromeDriver 2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067) on port 28090
Only local connections are allowed.
loginAdminPortal failed, the screenshot saved in test-output/screen-shot screenshot name : 1449717867643.jpg
=====测试用例: loginAdminPortal 运行失败,原因: expected:<超级管理员1> but was:<超级管理员>=====
堆栈信息:
org.testng.AssertJUnit.assertEquals(AssertJUnit.java:101)


截图保存在了目标文件夹下:

Webdriver+Testng实现测试用例失败自动截图功能_第1张图片


你可能感兴趣的:(测试用例,自动化测试,接口测试,java脚本,测试开发)