public class ScreenShotUtil {
public static void ScreenShoter(WebElement driver,String filePathName) {
File screenShotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenShotFile, new File(filePathName));
} catch (IOException e) {
e.printStackTrace();
}
}
}
如果想进行截屏,则在测试用例中调用即可。
为了能够在testng的用例结束时,设置自动截屏,或者在测试失败时截屏,可以使用org.testng.TestListenerAdapter来实现。
public class ScreenShotListener extends TestListenerAdapter {
protected Logger logger = LoggerFactory.getLogger(getClass());
protected ITestContext testContext = null;
public String getTestName(ITestResult it){
return it.getTestClass().getName() +"."+ it.getName()+it.getEndMillis();
}
@Override
public void onStart(ITestContext testContext) {
this.testContext = testContext;
super.onStart(testContext);
}
@Override
public void onFinish(ITestContext testContext) {
this.testContext = null;
super.onFinish(testContext);
}
@Override
public void onTestSuccess(ITestResult tr) {
logger.info("TestSuccess:" + getTestName(tr));
String path = ConfigurationSettings.SCREENSHOT_SUCESS_PATH + "/" + getTestName(tr)+".png";
WebDriver driver = (WebDriver) testContext.getAttribute(ConfigurationSettings.SELENIUM_DRIVER);
ScreenShotUtil.ScreenShoter(driver, path);
super.onTestSuccess(tr);
}
@Override
public void onTestFailure(ITestResult tr) {
logger.info("TestFailure:"+ getTestName(tr));
String path = ConfigurationSettings.SCREENSHOT_ERROR_PATH + "/" + getTestName(tr)+".png";
WebDriver driver = (WebDriver) testContext.getAttribute(ConfigurationSettings.SELENIUM_DRIVER);
ScreenShotUtil.ScreenShoter(driver, path);
super.onTestFailure(tr);
}
}
这时,需要在TestBase中,增加testContext:
protected ITestContext testContext = null;
@BeforeClass(alwaysRun = true)
public void setUpTestBase(ITestContext testContext) {
try{
driver = DriverFactory.getInstance().getDriver();
this.testContext = testContext;
testContext.setAttribute(ConfigurationSettings.SELENIUM_DRIVER, driver);
} catch(Exception e ) {
quitDriver();
Assert.fail("SetUp failed.", e);
}
}
在测试用例中,增加监听器注解:
@Listeners({ScreenShotListener.class})
public class BaseTestWithListener extends TestBase {
BaiduMainPage page = null;
@BeforeMethod
public void initPage() {
String path = "http://www.baidu.com/";
page = new BaiduMainPage(driver);
page.openAndWait(path);
PageFactory.initElements(driver, page);
}
@Test
public void testSearch(){
String text="apple";
page.SearchText(text);
sleep(1);
System.out.println(driver.getTitle());
Assert.assertTrue(driver.getTitle().contains(text));
System.out.println(page.getCurrentUrl());
}
}
当然,为了让截屏的路径可配置,需要修改properties配置文件和ConfigurationSetting:
selenium-vars.properties
#screenshot path if error occur
screenshot.error.path=error-screenshot
#screenshot path if success
screenshot.auto.path=auto-screenshot
ConfigurationSettings.java
public final static String SELENIUM_DRIVER="SELENIUM_DRIVER";
public static final String SCREENSHOT_ERROR_PATH = getProperty("screenshot.error.path").trim();
public static final String SCREENSHOT_SUCESS_PATH = getProperty("screenshot.auto.path").trim();